@@ -135,50 +135,60 @@ private async void btnFindSolution_Click(object sender, EventArgs e)
135
135
return ;
136
136
}
137
137
138
+ await FindSolution ( false ) ;
139
+ }
140
+
141
+ Organism [ ] bestOrganisms ;
142
+
143
+ private async Task FindSolution ( bool useLastBest )
144
+ {
138
145
try
139
146
{
140
147
btnFindSolution . Enabled = false ;
148
+ btnRetryWithBest . Enabled = false ;
141
149
btnStop . Enabled = true ;
142
150
143
151
UseWaitCursor = true ;
144
152
145
153
var problem =
146
154
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 )
153
161
. 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 ) ;
162
170
163
171
var availableInspirationals = lvInspirationals . CheckedItems . OfType < ListViewItem > ( ) . Select ( i => ( ( Inspirational ) i . Tag ) . Clone ( ) ) . ToArray ( ) ;
164
172
165
173
cancellationTokenSource = new CancellationTokenSource ( ) ;
166
174
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 ( ) ;
168
176
169
177
var best = await Task . WhenAll ( solvers ) ;
170
178
171
179
// Aggregate the best results and fill them into the results control
172
- var bestOfAll =
180
+ var bestOfAll =
173
181
best
174
182
. 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 } )
176
184
. GroupBy ( i => i . Name )
177
185
. Select ( i => i . First ( ) )
178
186
. OrderBy ( i => i . Solution . CostTotal )
179
187
. Take ( 5 )
180
188
. ToArray ( ) ;
181
189
190
+ bestOrganisms = bestOfAll . Select ( i => i . Organism . Clone ( ) ) . ToArray ( ) ;
191
+
182
192
lvSolutions . Items . Clear ( ) ;
183
193
184
194
foreach ( var organism in bestOfAll )
@@ -193,6 +203,7 @@ private async void btnFindSolution_Click(object sender, EventArgs e)
193
203
finally
194
204
{
195
205
btnFindSolution . Enabled = true ;
206
+ btnRetryWithBest . Enabled = bestOrganisms != null ;
196
207
btnStop . Enabled = false ;
197
208
198
209
UseWaitCursor = false ;
@@ -264,5 +275,17 @@ private void btnResetAll_Click(object sender, EventArgs e)
264
275
nudSugarAndSpice . Value = 0 ;
265
276
nudThreadAndNeedle . Value = 0 ;
266
277
}
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
+ }
267
290
}
268
291
}
0 commit comments