Skip to content

Commit 5ffe6bf

Browse files
committed
Added the option to retry the search for solutions using the top 5 organisms from the last search in the initial organism list.
1 parent dfe0532 commit 5ffe6bf

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

SalemOptimizer/MainForm.Designer.cs

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SalemOptimizer/MainForm.cs

+40-17
Original file line numberDiff line numberDiff line change
@@ -135,50 +135,60 @@ private async void btnFindSolution_Click(object sender, EventArgs e)
135135
return;
136136
}
137137

138+
await FindSolution(false);
139+
}
140+
141+
Organism[] bestOrganisms;
142+
143+
private async Task FindSolution(bool useLastBest)
144+
{
138145
try
139146
{
140147
btnFindSolution.Enabled = false;
148+
btnRetryWithBest.Enabled = false;
141149
btnStop.Enabled = true;
142150

143151
UseWaitCursor = true;
144152

145153
var problem =
146154
new Problem()
147-
.Add(ProficiencyKind.ArtsAndCrafts, nudArtsAndCrafts.Value)
148-
.Add(ProficiencyKind.CloakAndDagger, nudCloakAndDagger.Value)
149-
.Add(ProficiencyKind.FaithAndWisdom, nudFaithAndWisdom.Value)
150-
.Add(ProficiencyKind.FloraAndFauna, nudFloraAndFauna.Value)
151-
.Add(ProficiencyKind.HammerAndNail, nudHammerAndNail.Value)
152-
.Add(ProficiencyKind.HerbsAndSprouts, nudHerbsAndSprouts.Value)
155+
.Add(ProficiencyKind.ArtsAndCrafts, nudArtsAndCrafts.Value)
156+
.Add(ProficiencyKind.CloakAndDagger, nudCloakAndDagger.Value)
157+
.Add(ProficiencyKind.FaithAndWisdom, nudFaithAndWisdom.Value)
158+
.Add(ProficiencyKind.FloraAndFauna, nudFloraAndFauna.Value)
159+
.Add(ProficiencyKind.HammerAndNail, nudHammerAndNail.Value)
160+
.Add(ProficiencyKind.HerbsAndSprouts, nudHerbsAndSprouts.Value)
153161
.Add(ProficiencyKind.HuntingAndHideworking, nudHuntingAndHideworking.Value)
154-
.Add(ProficiencyKind.LawAndLore, nudLawAndLore.Value)
155-
.Add(ProficiencyKind.MinesAndMountains, nudMinesAndMountains.Value)
156-
.Add(ProficiencyKind.NaturalPhilosophy, nudNaturalPhilosophy.Value)
157-
.Add(ProficiencyKind.PerenialPhilosophy, nudPerenialPhilosophy.Value)
158-
.Add(ProficiencyKind.SparksAndEmbers, nudSparksAndEmbers.Value)
159-
.Add(ProficiencyKind.StocksAndCultivars, nudStocksAndCultivars.Value)
160-
.Add(ProficiencyKind.SugarAndSpice, nudSugarAndSpice.Value)
161-
.Add(ProficiencyKind.ThreadAndNeedle, nudThreadAndNeedle.Value);
162+
.Add(ProficiencyKind.LawAndLore, nudLawAndLore.Value)
163+
.Add(ProficiencyKind.MinesAndMountains, nudMinesAndMountains.Value)
164+
.Add(ProficiencyKind.NaturalPhilosophy, nudNaturalPhilosophy.Value)
165+
.Add(ProficiencyKind.PerenialPhilosophy, nudPerenialPhilosophy.Value)
166+
.Add(ProficiencyKind.SparksAndEmbers, nudSparksAndEmbers.Value)
167+
.Add(ProficiencyKind.StocksAndCultivars, nudStocksAndCultivars.Value)
168+
.Add(ProficiencyKind.SugarAndSpice, nudSugarAndSpice.Value)
169+
.Add(ProficiencyKind.ThreadAndNeedle, nudThreadAndNeedle.Value);
162170

163171
var availableInspirationals = lvInspirationals.CheckedItems.OfType<ListViewItem>().Select(i => ((Inspirational)i.Tag).Clone()).ToArray();
164172

165173
cancellationTokenSource = new CancellationTokenSource();
166174

167-
var solvers = Enumerable.Range(1, 4).Select(i => Task.Run(() => new Solver(problem, availableInspirationals, cancellationTokenSource.Token).Solve())).ToArray();
175+
var solvers = Enumerable.Range(1, 4).Select(i => Task.Run(() => new Solver(problem, availableInspirationals, cancellationTokenSource.Token, useLastBest ? bestOrganisms : null).Solve())).ToArray();
168176

169177
var best = await Task.WhenAll(solvers);
170178

171179
// Aggregate the best results and fill them into the results control
172-
var bestOfAll =
180+
var bestOfAll =
173181
best
174182
.SelectMany(i => i)
175-
.Select(i => new { Name = i.ToString(), Solution = i.Solution })
183+
.Select(i => new { Name = i.ToString(), Organism = i, Solution = i.Solution })
176184
.GroupBy(i => i.Name)
177185
.Select(i => i.First())
178186
.OrderBy(i => i.Solution.CostTotal)
179187
.Take(5)
180188
.ToArray();
181189

190+
bestOrganisms = bestOfAll.Select(i => i.Organism.Clone()).ToArray();
191+
182192
lvSolutions.Items.Clear();
183193

184194
foreach (var organism in bestOfAll)
@@ -193,6 +203,7 @@ private async void btnFindSolution_Click(object sender, EventArgs e)
193203
finally
194204
{
195205
btnFindSolution.Enabled = true;
206+
btnRetryWithBest.Enabled = bestOrganisms != null;
196207
btnStop.Enabled = false;
197208

198209
UseWaitCursor = false;
@@ -264,5 +275,17 @@ private void btnResetAll_Click(object sender, EventArgs e)
264275
nudSugarAndSpice.Value = 0;
265276
nudThreadAndNeedle.Value = 0;
266277
}
278+
279+
private async void btnRetryWithBest_Click(object sender, EventArgs e)
280+
{
281+
if (lvInspirationals.CheckedItems.Count == 0)
282+
{
283+
toolTipError.Show("You have to select at least one available inspirational.", lblInspirationals, 1000);
284+
285+
return;
286+
}
287+
288+
await FindSolution(true);
289+
}
267290
}
268291
}

SalemOptimizer/Properties/AssemblyInfo.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.2.2.0")]
36-
[assembly: AssemblyFileVersion("1.2.2.0")]
37-
[assembly: AssemblyInformationalVersion("22. 1. 2015")]
35+
[assembly: AssemblyVersion("1.3.0.0")]
36+
[assembly: AssemblyFileVersion("1.3.0.0")]
37+
[assembly: AssemblyInformationalVersion("23. 1. 2015")]

SalemOptimizer/Solver.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class Solver
1212
private readonly Problem problem;
1313
private readonly Inspirational[] availableInspirationals;
1414
private readonly CancellationToken cancellationToken;
15+
private readonly Organism[] includedOrganisms;
1516

16-
public Solver(Problem problem, Inspirational[] availableInspirationals, CancellationToken cancellationToken)
17+
public Solver(Problem problem, Inspirational[] availableInspirationals, CancellationToken cancellationToken, Organism[] includedOrganisms)
1718
{
1819
this.problem = problem;
1920
this.availableInspirationals = availableInspirationals;
2021
this.cancellationToken = cancellationToken;
22+
this.includedOrganisms = includedOrganisms;
2123
}
2224

2325
public Inspirational[] AvailableInspirationals { get { return availableInspirationals; } }
@@ -36,6 +38,8 @@ public Organism[] Solve()
3638
leaderboard.AddOrganism(organism);
3739
}
3840

41+
if (includedOrganisms != null) organisms.AddRange(includedOrganisms.Select(i => i.Clone()));
42+
3943
var best = default(Organism);
4044
var worst = default(Organism);
4145
var bestCost = double.MaxValue;

0 commit comments

Comments
 (0)