1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Threading;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Common.Resources;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
11 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
12 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
13 | using HeuristicLab.Optimization;
|
---|
14 | using HeuristicLab.Parameters;
|
---|
15 | using HeuristicLab.PluginInfrastructure;
|
---|
16 | using HEAL.Attic;
|
---|
17 |
|
---|
18 | namespace HeuristicLab.Analysis.FitnessLandscape.MultiTrajectory {
|
---|
19 |
|
---|
20 | [Item("Multi Trajectory Analysis", "Host for performing and aggregating several algorithm runs for an overarching analysis.")]
|
---|
21 | [Creatable("Analysis")]
|
---|
22 | [StorableType("DC98F804-66D9-44C2-9FC5-0FAB799D20A7")]
|
---|
23 | public class MultiTrajectoryAnalysis : ParameterizedNamedItem, IAlgorithm, IStorableContent {
|
---|
24 |
|
---|
25 | public IEnumerable<IOptimizer> NestedOptimizers {
|
---|
26 | get {
|
---|
27 | if (Algorithm != null)
|
---|
28 | yield return Algorithm;
|
---|
29 | if (SampleGenerationAlgorithm != null)
|
---|
30 | yield return SampleGenerationAlgorithm;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | #region Parameters
|
---|
35 | public OperatorParameter PreassignedSolutionCreatorParameter {
|
---|
36 | get { return (OperatorParameter)Parameters["PreassignedSolutionCreator"]; }
|
---|
37 | }
|
---|
38 | public ValueParameter<IntValue> NrOfSamplesParameter {
|
---|
39 | get { return (ValueParameter<IntValue>)Parameters["NrOfSamples"]; }
|
---|
40 | }
|
---|
41 | public ValueParameter<BoolValue> SaveIndividualTrailsParameter {
|
---|
42 | get { return (ValueParameter<BoolValue>)Parameters["SaveIndividualTrails"]; }
|
---|
43 | }
|
---|
44 | public ValueParameter<BoolValue> PrepareAfterStopParameter {
|
---|
45 | get { return (ValueParameter<BoolValue>)Parameters["PrepareAfterStop"]; }
|
---|
46 | }
|
---|
47 | public ValueParameter<CheckedItemList<IAggregator>> AggregatorsParameter {
|
---|
48 | get { return (ValueParameter<CheckedItemList<IAggregator>>)Parameters["Aggregators"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | #endregion
|
---|
53 |
|
---|
54 | #region Parameter Values
|
---|
55 | public PreassignedSolutionCreator PreassignedSolutionCreator {
|
---|
56 | get { return (PreassignedSolutionCreator)PreassignedSolutionCreatorParameter.Value; }
|
---|
57 | set { PreassignedSolutionCreatorParameter.Value = value; }
|
---|
58 | }
|
---|
59 | public IntValue NrOfSamples {
|
---|
60 | get { return NrOfSamplesParameter.Value; }
|
---|
61 | }
|
---|
62 | public bool SaveIndividualTrails {
|
---|
63 | get { return SaveIndividualTrailsParameter.Value.Value; }
|
---|
64 | }
|
---|
65 | public bool PrepareAfterStop {
|
---|
66 | get { return PrepareAfterStopParameter.Value.Value; }
|
---|
67 | }
|
---|
68 | public CheckedItemList<IAggregator> Aggregators {
|
---|
69 | get { return AggregatorsParameter.Value; }
|
---|
70 | }
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | #region Construction and Cloning
|
---|
74 | [StorableConstructor]
|
---|
75 | protected MultiTrajectoryAnalysis(StorableConstructorFlag _) : base(_) {
|
---|
76 | RegisterEvents();
|
---|
77 | }
|
---|
78 | protected MultiTrajectoryAnalysis(MultiTrajectoryAnalysis original, Cloner cloner)
|
---|
79 | : base(original, cloner) {
|
---|
80 | algorithm = cloner.Clone(original.algorithm);
|
---|
81 | currentAlgorithm = cloner.Clone(original.currentAlgorithm);
|
---|
82 | executionBaseTime = new TimeSpan(original.executionBaseTime.Ticks);
|
---|
83 | executionTime = new TimeSpan(original.executionTime.Ticks);
|
---|
84 | results = cloner.Clone(original.results);
|
---|
85 | running = original.running;
|
---|
86 | preparing = original.preparing;
|
---|
87 | runs = cloner.Clone(original.runs);
|
---|
88 | sampleGenerationAlgorithm = cloner.Clone(original.sampleGenerationAlgorithm);
|
---|
89 | StepNr = original.StepNr;
|
---|
90 | storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
|
---|
91 | subAlgorithmRuns = cloner.Clone(original.subAlgorithmRuns);
|
---|
92 | RegisterEvents();
|
---|
93 | }
|
---|
94 | public MultiTrajectoryAnalysis() {
|
---|
95 | Parameters.Add(new OperatorParameter("PreassignedSolutionCreator", "The operator that creates new solutions"));
|
---|
96 | Parameters.Add(new ValueParameter<IntValue>("NrOfSamples", "The number of samples to generate.", new IntValue(10)));
|
---|
97 | Parameters.Add(new ValueParameter<BoolValue>("SaveIndividualTrails", "Include all sub algorithm runs in the final run", new BoolValue(true)));
|
---|
98 | Parameters.Add(new ValueParameter<BoolValue>("PrepareAfterStop", "Automatically prepare algorithm after stopping to clean global scope and results (for large experiments)", new BoolValue(false)));
|
---|
99 | Parameters.Add(new ValueParameter<CheckedItemList<IAggregator>>("Aggregators", "List of aggretators for combining trajectory results.", new CheckedItemList<IAggregator>()));
|
---|
100 | currentAlgorithm = null;
|
---|
101 | algorithm = null;
|
---|
102 | executionBaseTime = new TimeSpan();
|
---|
103 | executionTime = new TimeSpan();
|
---|
104 | results = new ResultCollection();
|
---|
105 | running = false;
|
---|
106 | runs = new RunCollection();
|
---|
107 | sampleGenerationAlgorithm = null;
|
---|
108 | StepNr = -1;
|
---|
109 | storeAlgorithmInEachRun = false;
|
---|
110 | Aggregators.AddRange(ApplicationManager.Manager.GetInstances<IAggregator>());
|
---|
111 | RegisterEvents();
|
---|
112 | Prepare();
|
---|
113 | }
|
---|
114 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
115 | return new MultiTrajectoryAnalysis(this, cloner);
|
---|
116 | }
|
---|
117 | [StorableHook(HookType.AfterDeserialization)]
|
---|
118 | private void AfterDeserialization() {
|
---|
119 | if (algorithm != null && sampleGenerationAlgorithm != null) {
|
---|
120 | if (sampleGenerationAlgorithm.Problem == null) {
|
---|
121 | sampleGenerationAlgorithm.Problem = algorithm.Problem;
|
---|
122 | } else {
|
---|
123 | algorithm.Problem = sampleGenerationAlgorithm.Problem;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | if (!Parameters.ContainsKey("Aggregators"))
|
---|
127 | Parameters.Add(new ValueParameter<CheckedItemList<IAggregator>>(
|
---|
128 | "Aggregators", "List of aggregators for combining trajectory results.",
|
---|
129 | new CheckedItemList<IAggregator>(ApplicationManager.Manager.GetInstances<IAggregator>())));
|
---|
130 | RegisterEvents();
|
---|
131 | }
|
---|
132 | #endregion
|
---|
133 |
|
---|
134 | #region Fields & Properties
|
---|
135 |
|
---|
136 | public new static Image StaticItemImage { get { return VSImageLibrary.Event; } }
|
---|
137 |
|
---|
138 | public override Image ItemImage {
|
---|
139 | get {
|
---|
140 | switch (ExecutionState) {
|
---|
141 | case ExecutionState.Prepared: return VSImageLibrary.ExecutablePrepared;
|
---|
142 | case ExecutionState.Started: return VSImageLibrary.ExecutableStarted;
|
---|
143 | case ExecutionState.Paused: return VSImageLibrary.ExecutablePaused;
|
---|
144 | case ExecutionState.Stopped: return VSImageLibrary.ExecutableStopped;
|
---|
145 | default: return base.ItemImage;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | [Storable]
|
---|
151 | private RunCollection subAlgorithmRuns;
|
---|
152 |
|
---|
153 | public new ParameterCollection Parameters {
|
---|
154 | get { return base.Parameters; }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public Type ProblemType { get { return typeof(IHeuristicOptimizationProblem); } }
|
---|
158 | public string Filename { get; set; }
|
---|
159 |
|
---|
160 | [Storable]
|
---|
161 | private IProblem preliminaryProblem;
|
---|
162 | public IProblem Problem {
|
---|
163 | get { return preliminaryProblem ?? algorithm.Problem; }
|
---|
164 | set {
|
---|
165 | if (ExecutionState != ExecutionState.Prepared && ExecutionState != ExecutionState.Stopped)
|
---|
166 | throw new InvalidOperationException("Cannot change algorithm while running or paused.");
|
---|
167 | if (algorithm == null)
|
---|
168 | preliminaryProblem = value;
|
---|
169 | else
|
---|
170 | algorithm.Problem = value;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | [Storable]
|
---|
175 | private IAlgorithm algorithm;
|
---|
176 | public IAlgorithm Algorithm {
|
---|
177 | get { return algorithm; }
|
---|
178 | set {
|
---|
179 | if (algorithm == value)
|
---|
180 | return;
|
---|
181 | if (algorithm != null) {
|
---|
182 | DeregisterAlgorithmEvents();
|
---|
183 | ReuseOldProblem(algorithm, value);
|
---|
184 | }
|
---|
185 | algorithm = value;
|
---|
186 | if (algorithm != null) {
|
---|
187 | SyncAlgorithmProblems(algorithm, sampleGenerationAlgorithm);
|
---|
188 | RegisterAlgorithmEvents();
|
---|
189 | }
|
---|
190 | OnAlgorithmChanged();
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | [Storable]
|
---|
195 | private IAlgorithm sampleGenerationAlgorithm;
|
---|
196 | public IAlgorithm SampleGenerationAlgorithm {
|
---|
197 | get { return sampleGenerationAlgorithm; }
|
---|
198 | set {
|
---|
199 | if (sampleGenerationAlgorithm == value)
|
---|
200 | return;
|
---|
201 | if (sampleGenerationAlgorithm != null) {
|
---|
202 | DeregisterSampleGenerationAlgorithmEvents();
|
---|
203 | ReuseOldProblem(sampleGenerationAlgorithm, value);
|
---|
204 | }
|
---|
205 | sampleGenerationAlgorithm = value;
|
---|
206 | if (sampleGenerationAlgorithm != null) {
|
---|
207 | SyncAlgorithmProblems(sampleGenerationAlgorithm, algorithm);
|
---|
208 | RegisterSampleGenerationAlgorithmEvents();
|
---|
209 | }
|
---|
210 | OnSampleGenerationAlgorithmChanged();
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | private static void ReuseOldProblem(IAlgorithm oldAlgorithm, IAlgorithm newValue) {
|
---|
215 | if (oldAlgorithm.Problem != null && newValue != null && newValue.Problem == null)
|
---|
216 | newValue.Problem = oldAlgorithm.Problem;
|
---|
217 | }
|
---|
218 |
|
---|
219 | private void SyncAlgorithmProblems(IAlgorithm newAlgorithm, IAlgorithm otherAlgorithm) {
|
---|
220 | if (otherAlgorithm != null) {
|
---|
221 | if (otherAlgorithm.Problem != null) {
|
---|
222 | newAlgorithm.Problem = otherAlgorithm.Problem;
|
---|
223 | } else {
|
---|
224 | otherAlgorithm.Problem = newAlgorithm.Problem;
|
---|
225 | }
|
---|
226 | } else {
|
---|
227 | if (preliminaryProblem != null) {
|
---|
228 | newAlgorithm.Problem = preliminaryProblem;
|
---|
229 | preliminaryProblem = null;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | [Storable]
|
---|
235 | private RunCollection runs;
|
---|
236 | public RunCollection Runs {
|
---|
237 | get { return runs; }
|
---|
238 | }
|
---|
239 |
|
---|
240 | [Storable]
|
---|
241 | private ResultCollection results;
|
---|
242 | public ResultCollection Results {
|
---|
243 | get { return results; }
|
---|
244 | }
|
---|
245 |
|
---|
246 | [Storable]
|
---|
247 | private bool storeAlgorithmInEachRun;
|
---|
248 | public bool StoreAlgorithmInEachRun {
|
---|
249 | get { return storeAlgorithmInEachRun; }
|
---|
250 | set {
|
---|
251 | if (storeAlgorithmInEachRun == value)
|
---|
252 | return;
|
---|
253 | storeAlgorithmInEachRun = value;
|
---|
254 | OnStoreAlgorithmInEachRunChanged();
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | [Storable]
|
---|
259 | private ExecutionState executionState;
|
---|
260 | public ExecutionState ExecutionState {
|
---|
261 | get { return executionState; }
|
---|
262 | set {
|
---|
263 | if (executionState == value)
|
---|
264 | return;
|
---|
265 | executionState = value;
|
---|
266 | OnExecutionStateChanged();
|
---|
267 | OnItemImageChanged();
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | [Storable]
|
---|
272 | private TimeSpan executionBaseTime;
|
---|
273 | [Storable]
|
---|
274 | private TimeSpan executionTime;
|
---|
275 | public TimeSpan ExecutionTime {
|
---|
276 | get { return executionTime; }
|
---|
277 | set {
|
---|
278 | if (executionTime == value)
|
---|
279 | return;
|
---|
280 | executionTime = value;
|
---|
281 | OnExecutionTimeChanged();
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | #endregion
|
---|
286 |
|
---|
287 | #region Events
|
---|
288 | public event EventHandler AlgorithmChanged;
|
---|
289 | public event EventHandler SampleGenerationAlgorithmChanged;
|
---|
290 | public event EventHandler ProblemChanged;
|
---|
291 | public event EventHandler StoreAlgorithmInEachRunChanged;
|
---|
292 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
293 | public event EventHandler ExecutionStateChanged;
|
---|
294 | public event EventHandler ExecutionTimeChanged;
|
---|
295 | public event EventHandler Paused;
|
---|
296 | public event EventHandler Prepared;
|
---|
297 | public event EventHandler Started;
|
---|
298 | public event EventHandler Stopped;
|
---|
299 |
|
---|
300 | #region firing
|
---|
301 | protected virtual void OnAlgorithmChanged() {
|
---|
302 | EventHandler handler = AlgorithmChanged;
|
---|
303 | if (handler != null)
|
---|
304 | handler(this, EventArgs.Empty);
|
---|
305 | }
|
---|
306 | protected virtual void OnSampleGenerationAlgorithmChanged() {
|
---|
307 | EventHandler handler = SampleGenerationAlgorithmChanged;
|
---|
308 | if (handler != null)
|
---|
309 | handler(this, EventArgs.Empty);
|
---|
310 | }
|
---|
311 | protected virtual void OnProblemChanged() {
|
---|
312 | SelectPreassignedSolutionCreator();
|
---|
313 | EventHandler handler = ProblemChanged;
|
---|
314 | if (handler != null)
|
---|
315 | handler(this, EventArgs.Empty);
|
---|
316 | }
|
---|
317 | protected virtual void OnStoreAlgorithmInEachRunChanged() {
|
---|
318 | EventHandler handler = StoreAlgorithmInEachRunChanged;
|
---|
319 | if (handler != null)
|
---|
320 | handler(this, EventArgs.Empty);
|
---|
321 | }
|
---|
322 | protected virtual void OnExceptionOccurred(Exception args) {
|
---|
323 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
324 | if (handler != null)
|
---|
325 | handler(this, new EventArgs<Exception>(args));
|
---|
326 | }
|
---|
327 | protected virtual void OnExecutionStateChanged() {
|
---|
328 | EventHandler handler = ExecutionStateChanged;
|
---|
329 | if (handler != null)
|
---|
330 | handler(this, EventArgs.Empty);
|
---|
331 | switch (ExecutionState) {
|
---|
332 | case ExecutionState.Prepared: OnPrepared(); break;
|
---|
333 | case ExecutionState.Started: OnStarted(); break;
|
---|
334 | case ExecutionState.Paused: OnPaused(); break;
|
---|
335 | case ExecutionState.Stopped: OnStopped(); break;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | protected virtual void OnExecutionTimeChanged() {
|
---|
339 | EventHandler handler = ExecutionTimeChanged;
|
---|
340 | if (handler != null)
|
---|
341 | handler(this, EventArgs.Empty);
|
---|
342 | }
|
---|
343 | protected virtual void OnPaused() {
|
---|
344 | EventHandler handler = Paused;
|
---|
345 | if (handler != null)
|
---|
346 | handler(this, EventArgs.Empty);
|
---|
347 | }
|
---|
348 | protected virtual void OnPrepared() {
|
---|
349 | EventHandler handler = Prepared;
|
---|
350 | if (handler != null)
|
---|
351 | handler(this, EventArgs.Empty);
|
---|
352 | }
|
---|
353 | protected virtual void OnStarted() {
|
---|
354 | EventHandler handler = Started;
|
---|
355 | if (handler != null)
|
---|
356 | handler(this, EventArgs.Empty);
|
---|
357 | }
|
---|
358 | protected virtual void OnStopped() {
|
---|
359 | AggregateResults();
|
---|
360 | ClearSubAlgorithms();
|
---|
361 | CreateRun();
|
---|
362 | EventHandler handler = Stopped;
|
---|
363 | if (handler != null)
|
---|
364 | handler(this, EventArgs.Empty);
|
---|
365 | if (PrepareAfterStop) {
|
---|
366 | Prepare();
|
---|
367 | executionState = ExecutionState.Stopped;
|
---|
368 | }
|
---|
369 | }
|
---|
370 | #endregion
|
---|
371 | #endregion
|
---|
372 |
|
---|
373 | public void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
374 | values.Add("Execution Time", new TimeSpanValue(ExecutionTime));
|
---|
375 | values.Add("Time of Execution", new DateTimeValue(DateTime.Now));
|
---|
376 | CollectInnerResults(values, "", results);
|
---|
377 | }
|
---|
378 |
|
---|
379 | private static void CollectInnerResults(IDictionary<string, IItem> values, string prefix, IEnumerable<IResult> results) {
|
---|
380 | foreach (var result in results) {
|
---|
381 | var innerResults = result.Value as IEnumerable<IResult>;
|
---|
382 | if (innerResults == null)
|
---|
383 | values.Add(string.Format("{0}{1}", prefix, result.Name), result.Value);
|
---|
384 | else
|
---|
385 | CollectInnerResults(values, string.Format("{0}{1}.", prefix, result.Name), innerResults);
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | [Storable]
|
---|
390 | bool running;
|
---|
391 |
|
---|
392 | [Storable]
|
---|
393 | bool preparing;
|
---|
394 |
|
---|
395 | [Storable]
|
---|
396 | private int StepNr;
|
---|
397 |
|
---|
398 | [Storable]
|
---|
399 | IAlgorithm currentAlgorithm;
|
---|
400 |
|
---|
401 | public void Prepare(bool clearRuns) {
|
---|
402 | if (clearRuns)
|
---|
403 | Runs.Clear();
|
---|
404 | Prepare();
|
---|
405 | }
|
---|
406 |
|
---|
407 | public void Prepare() {
|
---|
408 | try {
|
---|
409 | preparing = true;
|
---|
410 | try {
|
---|
411 | if (Algorithm != null) {
|
---|
412 | Algorithm.Prepare();
|
---|
413 | Algorithm.StoreAlgorithmInEachRun = false;
|
---|
414 | }
|
---|
415 | if (SampleGenerationAlgorithm != null) {
|
---|
416 | SampleGenerationAlgorithm.Prepare();
|
---|
417 | SampleGenerationAlgorithm.StoreAlgorithmInEachRun = false;
|
---|
418 | }
|
---|
419 | ClearSubAlgorithms();
|
---|
420 | StepNr = -1;
|
---|
421 | ExecutionTime = new TimeSpan();
|
---|
422 | executionBaseTime = new TimeSpan();
|
---|
423 | results.Clear();
|
---|
424 | subAlgorithmRuns = new RunCollection();
|
---|
425 | results.Add(new Result("Sub Algorithm Runs", subAlgorithmRuns));
|
---|
426 | if (PreassignedSolutionCreator != null) {
|
---|
427 | if (PreassignedSolutionCreator.SolutionCollection != null)
|
---|
428 | PreassignedSolutionCreator.SolutionCollection.Clear();
|
---|
429 | PreassignedSolutionCreator.CurrentSolutionIndex = -1;
|
---|
430 | }
|
---|
431 | ExecutionState = ExecutionState.Prepared;
|
---|
432 | } catch (Exception) { }
|
---|
433 | } finally {
|
---|
434 | preparing = false;
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | public void Pause() {
|
---|
439 | try {
|
---|
440 | running = false;
|
---|
441 | if (currentAlgorithm != null) currentAlgorithm.Pause();
|
---|
442 | ExecutionState = ExecutionState.Paused;
|
---|
443 | } catch (Exception) { }
|
---|
444 | }
|
---|
445 |
|
---|
446 | public void Start() {
|
---|
447 | Start(CancellationToken.None);
|
---|
448 | }
|
---|
449 | public void Start(CancellationToken cancellationToken) {
|
---|
450 | running = true;
|
---|
451 | ExecutionState = ExecutionState.Started;
|
---|
452 | PerformNextStep();
|
---|
453 | }
|
---|
454 | public async Task StartAsync() { await StartAsync(CancellationToken.None); }
|
---|
455 | public async Task StartAsync(CancellationToken cancellationToken) {
|
---|
456 | await AsyncHelper.DoAsync(Start, cancellationToken);
|
---|
457 | }
|
---|
458 |
|
---|
459 | public void Stop() {
|
---|
460 | running = false;
|
---|
461 | try {
|
---|
462 | if (currentAlgorithm != null) currentAlgorithm.Stop();
|
---|
463 | } catch (Exception) { }
|
---|
464 | }
|
---|
465 |
|
---|
466 | protected virtual void PerformNextStep() {
|
---|
467 | PreassignedSolutionCreator.CurrentSolutionIndex = StepNr;
|
---|
468 | if (StepNr == -1) {
|
---|
469 | currentAlgorithm = SampleGenerationAlgorithm;
|
---|
470 | currentAlgorithm.Start();
|
---|
471 | } else if (StepNr < NrOfSamples.Value) {
|
---|
472 | currentAlgorithm = Algorithm;
|
---|
473 | IHeuristicOptimizationProblem problem = Algorithm.Problem as IHeuristicOptimizationProblem;
|
---|
474 | if (problem != null) {
|
---|
475 | SynchronizeParameters(problem.SolutionCreator, PreassignedSolutionCreator as ISolutionCreator);
|
---|
476 | if (PreassignedSolutionCreator != problem.SolutionCreator)
|
---|
477 | PreassignedSolutionCreator.OriginalSolutionCreator = problem.SolutionCreator;
|
---|
478 | problem.SolutionCreatorParameter.ActualValue = PreassignedSolutionCreator;
|
---|
479 | }
|
---|
480 | Algorithm.Prepare();
|
---|
481 | Algorithm.Runs.Clear();
|
---|
482 | Algorithm.Start();
|
---|
483 | } else {
|
---|
484 | ExecutionState = ExecutionState.Stopped;
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | private void ClearSubAlgorithms() {
|
---|
489 | if (sampleGenerationAlgorithm != null) {
|
---|
490 | sampleGenerationAlgorithm.Results.Clear();
|
---|
491 | sampleGenerationAlgorithm.Runs.Clear();
|
---|
492 | }
|
---|
493 | if (algorithm != null) {
|
---|
494 | algorithm.Results.Clear();
|
---|
495 | algorithm.Runs.Clear();
|
---|
496 | }
|
---|
497 | }
|
---|
498 |
|
---|
499 | private void AggregateResults() {
|
---|
500 | if (subAlgorithmRuns == null)
|
---|
501 | return;
|
---|
502 | foreach (var aggregator in Aggregators)
|
---|
503 | aggregator.Reset();
|
---|
504 | foreach (IRun run in subAlgorithmRuns) {
|
---|
505 | foreach (var result in run.Results) {
|
---|
506 | foreach (var item in Aggregators.CheckedItems)
|
---|
507 | item.Value.MaybeAddResult(new Result(result.Key, result.Value));
|
---|
508 | }
|
---|
509 | }
|
---|
510 | foreach (var item in Aggregators.CheckedItems)
|
---|
511 | results.Add(item.Value.CreateResult());
|
---|
512 | }
|
---|
513 |
|
---|
514 | public override void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
515 | base.CollectParameterValues(values);
|
---|
516 | values.Add("Algorithm Name", new StringValue(Name));
|
---|
517 | values.Add("Algorithm Type", new StringValue(this.GetType().GetPrettyName()));
|
---|
518 | if (Problem != null) {
|
---|
519 | Problem.CollectParameterValues(values);
|
---|
520 | values.Add("Problem Name", new StringValue(Problem.Name));
|
---|
521 | values.Add("Problem Type", new StringValue(Problem.GetType().GetPrettyName()));
|
---|
522 | }
|
---|
523 | CollectPrefixedParameterValues("Sampling", SampleGenerationAlgorithm, values);
|
---|
524 | CollectPrefixedParameterValues("Analysis", Algorithm, values);
|
---|
525 | }
|
---|
526 |
|
---|
527 | private void CollectPrefixedParameterValues(string prefix, IAlgorithm algorithm, IDictionary<string, IItem> values) {
|
---|
528 | var innerValues = new Dictionary<string, IItem>();
|
---|
529 | algorithm.CollectParameterValues(innerValues);
|
---|
530 | foreach (var value in innerValues) {
|
---|
531 | values.Add(string.Format("{0}.{1}", prefix, value.Key), value.Value);
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | protected virtual void CreateRun() {
|
---|
536 | Run run = new Run(Name, Description, this);
|
---|
537 | if (!SaveIndividualTrails)
|
---|
538 | run.Results.Remove("Sub Algorithm Runs");
|
---|
539 | Runs.Add(run);
|
---|
540 | }
|
---|
541 |
|
---|
542 | #region Event registry
|
---|
543 | private void RegisterEvents() {
|
---|
544 | if (algorithm != null)
|
---|
545 | RegisterAlgorithmEvents();
|
---|
546 | if (sampleGenerationAlgorithm != null)
|
---|
547 | RegisterSampleGenerationAlgorithmEvents();
|
---|
548 | }
|
---|
549 | private void RegisterAlgorithmEvents() {
|
---|
550 | algorithm.ExceptionOccurred += algorithm_ExceptionOccurred;
|
---|
551 | algorithm.Started += algorithm_Started;
|
---|
552 | algorithm.Paused += algorithm_Paused;
|
---|
553 | algorithm.Prepared += algorithm_Prepared;
|
---|
554 | algorithm.Stopped += algorithm_Stopped;
|
---|
555 | algorithm.ExecutionTimeChanged += algorithm_ExecutionTimeChanged;
|
---|
556 | algorithm.ExecutionStateChanged += algorithm_ExecutionStateChanged;
|
---|
557 | algorithm.Runs.ItemsAdded += Algorithm_Runs_ItemsAdded;
|
---|
558 | algorithm.ProblemChanged += algorithm_ProblemChanged;
|
---|
559 | }
|
---|
560 | private void DeregisterAlgorithmEvents() {
|
---|
561 | algorithm.ExceptionOccurred -= algorithm_ExceptionOccurred;
|
---|
562 | algorithm.Started -= algorithm_Started;
|
---|
563 | algorithm.Paused -= algorithm_Paused;
|
---|
564 | algorithm.Prepared -= algorithm_Prepared;
|
---|
565 | algorithm.Stopped -= algorithm_Stopped;
|
---|
566 | algorithm.ExecutionTimeChanged -= algorithm_ExecutionTimeChanged;
|
---|
567 | algorithm.ExecutionStateChanged -= algorithm_ExecutionStateChanged;
|
---|
568 | algorithm.Runs.ItemsAdded -= Algorithm_Runs_ItemsAdded;
|
---|
569 | algorithm.ProblemChanged -= algorithm_ProblemChanged;
|
---|
570 | }
|
---|
571 | private void RegisterSampleGenerationAlgorithmEvents() {
|
---|
572 | sampleGenerationAlgorithm.ExceptionOccurred += algorithm_ExceptionOccurred;
|
---|
573 | sampleGenerationAlgorithm.Started += algorithm_Started;
|
---|
574 | sampleGenerationAlgorithm.Paused += algorithm_Paused;
|
---|
575 | sampleGenerationAlgorithm.Prepared += algorithm_Prepared;
|
---|
576 | sampleGenerationAlgorithm.Stopped += sampleGenerationAlgorithm_Stopped;
|
---|
577 | sampleGenerationAlgorithm.ExecutionTimeChanged += sampleGenerationAlgorithm_ExecutionTimeChanged;
|
---|
578 | sampleGenerationAlgorithm.ProblemChanged += algorithm_ProblemChanged;
|
---|
579 | }
|
---|
580 | private void DeregisterSampleGenerationAlgorithmEvents() {
|
---|
581 | sampleGenerationAlgorithm.ExceptionOccurred -= algorithm_ExceptionOccurred;
|
---|
582 | sampleGenerationAlgorithm.Started -= algorithm_Started;
|
---|
583 | sampleGenerationAlgorithm.Paused -= algorithm_Paused;
|
---|
584 | sampleGenerationAlgorithm.Prepared -= algorithm_Prepared;
|
---|
585 | sampleGenerationAlgorithm.Stopped -= sampleGenerationAlgorithm_Stopped;
|
---|
586 | sampleGenerationAlgorithm.ExecutionTimeChanged -= sampleGenerationAlgorithm_ExecutionTimeChanged;
|
---|
587 | sampleGenerationAlgorithm.ProblemChanged -= algorithm_ProblemChanged;
|
---|
588 | }
|
---|
589 | #endregion
|
---|
590 |
|
---|
591 | #region Sub Algorithm Event Handling
|
---|
592 | void algorithm_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
593 | IHeuristicOptimizationProblem problem = Algorithm.Problem as IHeuristicOptimizationProblem;
|
---|
594 | if (algorithm.ExecutionState == ExecutionState.Stopped && problem != null && problem.SolutionCreator is PreassignedSolutionCreator)
|
---|
595 | problem.SolutionCreatorParameter.ActualValue = ((PreassignedSolutionCreator)problem.SolutionCreator).OriginalSolutionCreator;
|
---|
596 | }
|
---|
597 | void algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
598 | ExecutionTime = executionBaseTime + algorithm.ExecutionTime;
|
---|
599 | }
|
---|
600 | void sampleGenerationAlgorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
601 | ExecutionTime = executionBaseTime + sampleGenerationAlgorithm.ExecutionTime;
|
---|
602 | }
|
---|
603 | void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
604 | OnExceptionOccurred(e.Value);
|
---|
605 | ExecutionState = ExecutionState.Paused;
|
---|
606 | }
|
---|
607 | void algorithm_Started(object sender, EventArgs e) {
|
---|
608 | running = true;
|
---|
609 | ExecutionState = ExecutionState.Started;
|
---|
610 | }
|
---|
611 | void algorithm_Paused(object sender, EventArgs e) {
|
---|
612 | ExecutionState = ExecutionState.Paused;
|
---|
613 | }
|
---|
614 | void algorithm_Prepared(object sender, EventArgs e) {
|
---|
615 | if (executionState == ExecutionState.Stopped && !preparing)
|
---|
616 | Prepare();
|
---|
617 | }
|
---|
618 | void algorithm_Stopped(object sender, EventArgs e) {
|
---|
619 | if (running) {
|
---|
620 | executionBaseTime = executionTime + algorithm.ExecutionTime;
|
---|
621 | ExecutionTime = executionBaseTime;
|
---|
622 | StepNr++;
|
---|
623 | PerformNextStep();
|
---|
624 | } else {
|
---|
625 | ExecutionState = ExecutionState.Stopped;
|
---|
626 | }
|
---|
627 | }
|
---|
628 | void sampleGenerationAlgorithm_Stopped(object sender, EventArgs e) {
|
---|
629 | if (running) {
|
---|
630 | if (sampleGenerationAlgorithm.Results.ContainsKey("Samples")) {
|
---|
631 | IScope scope = sampleGenerationAlgorithm.Results["Samples"].Value as IScope;
|
---|
632 | if (scope != null)
|
---|
633 | PreassignedSolutionCreator.SolutionCollection = (IScope)scope.Clone();
|
---|
634 | }
|
---|
635 | executionBaseTime = executionTime + sampleGenerationAlgorithm.ExecutionTime;
|
---|
636 | ExecutionTime = executionBaseTime;
|
---|
637 | PreassignedSolutionCreator.CurrentSolutionIndex = 0;
|
---|
638 | StepNr++;
|
---|
639 | PerformNextStep();
|
---|
640 | } else {
|
---|
641 | ExecutionState = ExecutionState.Stopped;
|
---|
642 | }
|
---|
643 | }
|
---|
644 | void algorithm_ProblemChanged(object sender, EventArgs e) {
|
---|
645 | if (sender == algorithm && sampleGenerationAlgorithm != null)
|
---|
646 | sampleGenerationAlgorithm.Problem = algorithm.Problem;
|
---|
647 | if (sender == sampleGenerationAlgorithm && algorithm != null)
|
---|
648 | algorithm.Problem = sampleGenerationAlgorithm.Problem;
|
---|
649 | OnProblemChanged();
|
---|
650 | }
|
---|
651 | void Algorithm_Runs_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
652 | if (subAlgorithmRuns != null)
|
---|
653 | foreach (var run in e.Items) {
|
---|
654 | var slimRun = new Run();
|
---|
655 | foreach (var result in run.Results)
|
---|
656 | slimRun.Results.Add(result);
|
---|
657 | subAlgorithmRuns.Add(slimRun);
|
---|
658 | slimRun.Name = string.Format("Sub Algorithm Run {0}", subAlgorithmRuns.Count);
|
---|
659 | }
|
---|
660 | }
|
---|
661 | #endregion
|
---|
662 |
|
---|
663 | #region Auxiliary Functions
|
---|
664 | private static void SynchronizeParameters(IParameterizedItem original, IParameterizedItem copy) {
|
---|
665 | foreach (IParameter origParam in original.Parameters) {
|
---|
666 | IParameter copyParam = null;
|
---|
667 | if (copy.Parameters.TryGetValue(origParam.Name, out copyParam))
|
---|
668 | SynchronizeParameters(origParam, copyParam);
|
---|
669 | }
|
---|
670 | }
|
---|
671 | private static void SynchronizeParameters(IParameter origParam, IParameter copyParam) {
|
---|
672 | if (origParam == null || copyParam == null)
|
---|
673 | return;
|
---|
674 | IValueParameter origValueParam = origParam as IValueParameter;
|
---|
675 | ILookupParameter origLookupParam = origParam as ILookupParameter;
|
---|
676 | IValueParameter copyValueParam = copyParam as IValueParameter;
|
---|
677 | ILookupParameter copyLookupParam = copyParam as ILookupParameter;
|
---|
678 | if (origValueParam != null && copyValueParam != null && copyValueParam.DataType.IsAssignableFrom(origValueParam.DataType))
|
---|
679 | copyValueParam.Value = origValueParam.Value;
|
---|
680 | if (origLookupParam != null && copyLookupParam != null)
|
---|
681 | copyLookupParam.ActualName = origLookupParam.ActualName;
|
---|
682 | }
|
---|
683 | private void SelectPreassignedSolutionCreator() {
|
---|
684 | var alg = Algorithm ?? SampleGenerationAlgorithm;
|
---|
685 | if (alg == null)
|
---|
686 | return;
|
---|
687 | IHeuristicOptimizationProblem problem = alg.Problem as IHeuristicOptimizationProblem;
|
---|
688 | if (problem != null) {
|
---|
689 | if (problem.SolutionCreator is IBinaryVectorCreator &&
|
---|
690 | (PreassignedSolutionCreator == null || !(PreassignedSolutionCreator is IBinaryVectorCreator)))
|
---|
691 | PreassignedSolutionCreator = new PreassignedBinaryVectorCreator();
|
---|
692 | else if (problem.SolutionCreator is IRealVectorCreator &&
|
---|
693 | (PreassignedSolutionCreator == null || !(PreassignedSolutionCreator is IRealVectorCreator)))
|
---|
694 | PreassignedSolutionCreator = new PreassignedRealVectorCreator();
|
---|
695 | else if (problem.SolutionCreator is IPermutationCreator &&
|
---|
696 | (PreassignedSolutionCreator == null || !(PreassignedSolutionCreator is IPermutationCreator)))
|
---|
697 | PreassignedSolutionCreator = new PreassignePermutationCreator();
|
---|
698 | }
|
---|
699 | }
|
---|
700 | #endregion
|
---|
701 |
|
---|
702 | }
|
---|
703 | }
|
---|