Skip to content

Commit 92dab86

Browse files
committed
ScrollViewer for results added
1 parent 3889f27 commit 92dab86

9 files changed

+252
-38
lines changed

OptimizationIssues/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:local="clr-namespace:OptimizationIssues"
77
xmlns:views="clr-namespace:OptimizationIssues.Views"
88
mc:Ignorable="d"
9-
Title="Problemy Optymalizacyjne" Height="450" Width="1000"
9+
Title="Problemy Optymalizacyjne" Height="550" Width="1000"
1010
Icon="/Icon.png" ResizeMode="NoResize">
1111

1212
<Window.Resources>

OptimizationIssues/Models/KnapsackProblem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public KnapsackProblem(int capacity, List<int> indexes, List<int> weights, List<
1515
Indexes = Enumerable.Range(0, weights.Count).ToList();
1616
}
1717

18-
public override int Solve()
18+
public override int Solve(int a = 0, int b = 0, int c = 0)
1919
{
2020
return 0;
2121
}

OptimizationIssues/Models/ProblemBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public abstract class ProblemBase
44
{
5-
public abstract int Solve();
5+
public abstract int Solve(int a = 0, int b = 0, int c = 0);
66
}
77
}
Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,147 @@
1-
namespace OptimizationIssues.Models
1+
using System.Diagnostics;
2+
3+
namespace OptimizationIssues.Models
24
{
35
public class TaskAllocationProblem : ProblemBase
46
{
7+
public readonly double[] ProcessorMultipliers = { 1.0, 1.25, 1.5, 1.75 };
8+
59
public int NumberOfResources { get; set; }
610
public int NumberOfTasks { get; set; }
7-
public int[,] TaskCosts { get; set; }
11+
public int[] TaskCosts { get; set; }
812

9-
public TaskAllocationProblem(int numberOfResources, int numberOfTasks, int[,] taskCosts)
13+
public TaskAllocationProblem(int numberOfResources, int numberOfTasks, int[] taskCosts)
1014
{
1115
NumberOfResources = numberOfResources;
1216
NumberOfTasks = numberOfTasks;
1317
TaskCosts = taskCosts;
1418
}
1519

16-
public override int Solve()
20+
public override int Solve(int maxTrials, int maxGenerationsWithoutImprovement, int maxTime)
1721
{
18-
return 0;
22+
var populationSize = Math.Max(100, NumberOfTasks * 2);
23+
var maxGenerations = Math.Max(10000, NumberOfTasks * 100);
24+
var mutationRate = 0.01;
25+
var eliteSize = 20;
26+
var generationsWithoutImprovement = 0;
27+
28+
List<int[]> population = InitializePopulation(populationSize);
29+
int[] bestSolution = population.First();
30+
int bestFitness = CalculateFitness(bestSolution);
31+
32+
var stopwatch = new Stopwatch();
33+
stopwatch.Start();
34+
35+
for (int generation = 0; generation < maxGenerations && generationsWithoutImprovement < maxGenerationsWithoutImprovement; generation++)
36+
{
37+
population = Evolve(population, eliteSize, mutationRate);
38+
39+
int[] currentBest = population.First();
40+
int currentBestFitness = CalculateFitness(currentBest);
41+
42+
if (currentBestFitness < bestFitness)
43+
{
44+
bestFitness = currentBestFitness;
45+
bestSolution = currentBest;
46+
generationsWithoutImprovement = 0;
47+
}
48+
else
49+
generationsWithoutImprovement++;
50+
51+
if (stopwatch.ElapsedMilliseconds >= maxTime)
52+
break;
53+
54+
if (generation >= maxTrials)
55+
break;
56+
}
57+
58+
stopwatch.Stop();
59+
60+
return bestFitness;
61+
}
62+
63+
private List<int[]> InitializePopulation(int populationSize)
64+
{
65+
var population = new List<int[]>();
66+
67+
Random random = new Random();
68+
69+
for (int i = 0; i < populationSize; i++)
70+
{
71+
int[] individual = new int[NumberOfTasks];
72+
73+
for (int j = 0; j < NumberOfTasks; j++)
74+
individual[j] = random.Next(0, NumberOfResources);
75+
76+
population.Add(individual);
77+
}
78+
79+
return population;
80+
}
81+
82+
private List<int[]> Evolve(List<int[]> population, int eliteSize, double mutationRate)
83+
{
84+
List<int[]> newPopulation = new List<int[]>();
85+
List<int[]> elite = population.OrderBy(ind => CalculateFitness(ind)).Take(eliteSize).ToList();
86+
newPopulation.AddRange(elite);
87+
88+
Random random = new Random();
89+
90+
while (newPopulation.Count < population.Count)
91+
{
92+
int[] parent1 = SelectParent(population);
93+
int[] parent2 = SelectParent(population);
94+
95+
int[] child = Crossover(parent1, parent2);
96+
97+
if (random.NextDouble() < mutationRate)
98+
Mutate(child);
99+
100+
newPopulation.Add(child);
101+
}
102+
103+
return newPopulation;
104+
}
105+
106+
private int[] SelectParent(List<int[]> population)
107+
{
108+
Random random = new Random();
109+
int tournamentSize = 5;
110+
var tournament = population.OrderBy(x => CalculateFitness(x)).Take(tournamentSize).ToList();
111+
112+
return tournament[random.Next(tournamentSize)];
113+
}
114+
115+
private int[] Crossover(int[] parent1, int[] parent2)
116+
{
117+
Random random = new Random();
118+
int crossoverPoint = random.Next(1, NumberOfTasks - 1);
119+
120+
int[] child = new int[NumberOfTasks];
121+
Array.Copy(parent1, 0, child, 0, crossoverPoint);
122+
Array.Copy(parent2, crossoverPoint, child, crossoverPoint, NumberOfTasks - crossoverPoint);
123+
124+
return child;
125+
}
126+
127+
private void Mutate(int[] individual)
128+
{
129+
Random random = new Random();
130+
int mutationPoint = random.Next(0, NumberOfTasks);
131+
individual[mutationPoint] = random.Next(0, NumberOfResources);
132+
}
133+
134+
private int CalculateFitness(int[] solution)
135+
{
136+
int[] processorCompletionTime = new int[NumberOfResources];
137+
138+
for (int i = 0; i < NumberOfTasks; i++)
139+
{
140+
int processor = solution[i];
141+
processorCompletionTime[processor] += (int)(TaskCosts[i] * ProcessorMultipliers[processor]);
142+
}
143+
144+
return processorCompletionTime.Max();
19145
}
20146
}
21-
}
147+
}

OptimizationIssues/ViewModels/TaskAllocationViewModel.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
namespace OptimizationIssues.ViewModels
1+
using OptimizationIssues.Models;
2+
3+
namespace OptimizationIssues.ViewModels
24
{
35
public class TaskAllocationViewModel
46
{
57
public int NumberOfResources { get; set; }
68
public int NumberOfTasks { get; set; }
79
public List<int> TaskCosts { get; set; }
10+
public int MaxTrials { get; set; }
11+
public int MaxGenerationsWithoutImprovement { get; set; }
12+
public int MaxTime { get; set; }
813

914
public TaskAllocationViewModel()
1015
{
1116
TaskCosts = new List<int>();
1217
}
1318

14-
public (List<string> TaskAssignments, List<string> ProcessorCompletionTimes, List<int> ProcessorLoadHistogram) SolveTaskAllocation()
19+
public (List<string> TaskAssignments, List<string> ProcessorCompletionTimes, List<int> ProcessorLoadHistogram, int LastProcessorCompletionTime) SolveTaskAllocation()
1520
{
16-
double[] processorMultipliers = new double[] { 1.0, 1.25, 1.5, 1.75 };
21+
var problem = new TaskAllocationProblem(NumberOfResources, NumberOfTasks, TaskCosts.ToArray());
22+
int bestFitness = problem.Solve(MaxTrials, MaxGenerationsWithoutImprovement, MaxTime);
1723

1824
List<string> taskAssignments = new List<string>();
1925
List<string> processorCompletionTimes = new List<string>();
2026
List<int> processorLoadHistogram = new List<int>();
2127

2228
int[] processorCompletionTime = new int[NumberOfResources];
23-
2429
for (int i = 0; i < NumberOfResources; i++)
2530
processorCompletionTime[i] = 0;
2631

@@ -31,7 +36,7 @@ public TaskAllocationViewModel()
3136

3237
for (int resource = 0; resource < NumberOfResources; resource++)
3338
{
34-
int taskTime = (int)(TaskCosts[task] * processorMultipliers[resource]);
39+
int taskTime = (int)(TaskCosts[task] * problem.ProcessorMultipliers[resource]);
3540

3641
if (processorCompletionTime[resource] + taskTime < minCompletionTime)
3742
{
@@ -40,8 +45,8 @@ public TaskAllocationViewModel()
4045
}
4146
}
4247

43-
taskAssignments.Add($"Zadanie {task + 1}: Procesor P{minTimeProcessor}, Czas: {TaskCosts[task] * processorMultipliers[minTimeProcessor]} ms");
44-
processorCompletionTime[minTimeProcessor] += (int)(TaskCosts[task] * processorMultipliers[minTimeProcessor]);
48+
taskAssignments.Add($"Zadanie {task + 1} - Procesor P{minTimeProcessor}, Czas: {TaskCosts[task] * problem.ProcessorMultipliers[minTimeProcessor]} ms");
49+
processorCompletionTime[minTimeProcessor] += (int)(TaskCosts[task] * problem.ProcessorMultipliers[minTimeProcessor]);
4550
}
4651

4752
for (int i = 0; i < NumberOfResources; i++)
@@ -55,7 +60,7 @@ public TaskAllocationViewModel()
5560
processorLoadHistogram.Add(loadPercentage);
5661
}
5762

58-
return (taskAssignments, processorCompletionTimes, processorLoadHistogram);
63+
return (taskAssignments, processorCompletionTimes, processorLoadHistogram, maxCompletionTime);
5964
}
6065
}
6166
}

OptimizationIssues/Views/KnapsackView.xaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
</Setter.Value>
5454
</Setter>
5555
</Style>
56-
56+
5757
<Style TargetType="Button" x:Key="DataButtonStyle">
5858
<Setter Property="FontSize" Value="16"/>
5959
<Setter Property="Padding" Value="10"/>
@@ -190,7 +190,9 @@
190190
</Button>
191191
</StackPanel>
192192
<TextBlock Text="Wyniki:" FontWeight="Bold" FontSize="16" VerticalAlignment="Top"/>
193-
<TextBlock x:Name="ResultTextBlock" Text="Tutaj pojawią się wyniki..." VerticalAlignment="Top" FontSize="14" Margin="0,5,0,0"/>
193+
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" MaxHeight="250">
194+
<TextBlock x:Name="ResultTextBlock" Text="Tutaj pojawią się wyniki..." VerticalAlignment="Top" FontSize="14" Margin="0,5,0,0"/>
195+
</ScrollViewer>
194196
</StackPanel>
195197

196198
<StackPanel Grid.Column="2" Margin="10" VerticalAlignment="Top">

OptimizationIssues/Views/KnapsackView.xaml.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using OxyPlot.Series;
1010
using OxyPlot.Axes;
1111
using OxyPlot.Annotations;
12-
using static OptimizationIssues.Views.KnapsackView;
1312

1413
namespace OptimizationIssues.Views
1514
{
@@ -242,17 +241,17 @@ private void GenerateSampleDataButton_Click(object sender, RoutedEventArgs e)
242241
{
243242
Random random = new Random();
244243

245-
int capacity = random.Next(10, 101);
244+
int capacity = random.Next(50, 2501);
246245
CapacityTextBox.Text = capacity.ToString();
247246

248-
int itemCount = random.Next(3, 8);
247+
int itemCount = random.Next(1, 101);
249248
List<int> weights = new List<int>();
250249
List<int> values = new List<int>();
251250

252251
for (int i = 0; i < itemCount; i++)
253252
{
254253
weights.Add(random.Next(1, capacity / 2));
255-
values.Add(random.Next(10, 101));
254+
values.Add(random.Next(10, 91));
256255
}
257256

258257
WeightsTextBox.Text = string.Join(", ", weights);
@@ -274,7 +273,7 @@ private void UpdateChart(List<KnapsackItem> selectedItems)
274273
{
275274
var plotModel = new PlotModel
276275
{
277-
Title = "Wyniki optymalizacji",
276+
Title = "Wykres",
278277
Background = OxyColor.FromRgb(46, 46, 46)
279278
};
280279

0 commit comments

Comments
 (0)