1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Threading;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.Scheduling.CFSAP {
|
---|
13 | [Item("MultiNest CFSAP Solver", "Solving strategy that applies an algorithm instace to the worst nest.")]
|
---|
14 | [StorableClass]
|
---|
15 | [Creatable(CreatableAttribute.Categories.Algorithms)]
|
---|
16 | public class MultiNestCFSAPSolvingStrategy : BasicAlgorithm {
|
---|
17 | public override bool SupportsPause => true;
|
---|
18 |
|
---|
19 | public override Type ProblemType => typeof(MultiNestCFSAP);
|
---|
20 |
|
---|
21 | public new MultiNestCFSAP Problem {
|
---|
22 | get { return (MultiNestCFSAP)base.Problem; }
|
---|
23 | set { base.Problem = value; }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public IValueParameter<IAlgorithm> SolverParameter {
|
---|
27 | get { return (IValueParameter<IAlgorithm>)Parameters["Solver"]; }
|
---|
28 | }
|
---|
29 |
|
---|
30 | public IFixedValueParameter<TimeSpanValue> MaximumRuntimeParameter {
|
---|
31 | get { return (IFixedValueParameter<TimeSpanValue>)Parameters["MaximumRuntime"]; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public TimeSpan MaximumRuntime {
|
---|
35 | get { return MaximumRuntimeParameter.Value.Value; }
|
---|
36 | set { MaximumRuntimeParameter.Value.Value = value; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | protected MultiNestCFSAPSolvingStrategy(bool deserializing) : base(deserializing) { }
|
---|
41 | protected MultiNestCFSAPSolvingStrategy(MultiNestCFSAPSolvingStrategy original, Cloner cloner)
|
---|
42 | : base(original, cloner) {
|
---|
43 | if (original.algorithms != null)
|
---|
44 | algorithms = original.algorithms.Select(x => cloner.Clone(x)).ToList();
|
---|
45 | if (original.qualities != null)
|
---|
46 | qualities = original.qualities.ToArray();
|
---|
47 | if (original.algorithmsResults != null)
|
---|
48 | algorithmsResults = cloner.Clone(original.algorithmsResults);
|
---|
49 | }
|
---|
50 | public MultiNestCFSAPSolvingStrategy() {
|
---|
51 | Parameters.Add(new ValueParameter<IAlgorithm>("Solver", "The actual solver template."));
|
---|
52 | Parameters.Add(new FixedValueParameter<TimeSpanValue>("MaximumRuntime", "The maximum time that the strategy should run.", new TimeSpanValue(TimeSpan.FromSeconds(60))));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new MultiNestCFSAPSolvingStrategy(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | [Storable]
|
---|
60 | private List<IAlgorithm> algorithms;
|
---|
61 | [Storable]
|
---|
62 | private int[] qualities;
|
---|
63 | [Storable]
|
---|
64 | private ResultCollection algorithmsResults;
|
---|
65 |
|
---|
66 | protected override void OnPrepared() {
|
---|
67 | base.OnPrepared();
|
---|
68 | algorithms = null;
|
---|
69 | qualities = null;
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
73 | var nests = Problem.ProcessingTimesParameter.Value.Count;
|
---|
74 | algorithms = new List<IAlgorithm>(nests);
|
---|
75 | algorithmsResults = new ResultCollection();
|
---|
76 | for (var n = 0; n < nests; n++) {
|
---|
77 | var clone = (IAlgorithm)SolverParameter.Value.Clone();
|
---|
78 | clone.Prepare();
|
---|
79 | var cfsap = (ICFSAP)clone.Problem;
|
---|
80 | cfsap.ProcessingTimes = Problem.ProcessingTimes[n];
|
---|
81 | cfsap.SetupTimes = Problem.SetupTimes[n];
|
---|
82 | cfsap.UpdateEncoding();
|
---|
83 | algorithms.Add(clone);
|
---|
84 | algorithmsResults.Add(new Result("Nest " + (n + 1), clone.Results));
|
---|
85 | clone.Start();
|
---|
86 | }
|
---|
87 | qualities = algorithms.Select(x => (int)((DoubleValue)x.Results["BestQuality"].Value).Value).ToArray();
|
---|
88 | var worst = qualities.Select((v, i) => new { Index = i, Quality = v }).MaxItems(x => x.Quality).First();
|
---|
89 | var min = worst.Quality;
|
---|
90 |
|
---|
91 | Results.Add(new Result("Nest with maximum T", new IntValue(worst.Index + 1)));
|
---|
92 | Results.Add(new Result("Maximum T", new IntValue(min)));
|
---|
93 | Results.Add(new Result("Best Solution Found At", new TimeSpanValue(ExecutionTime)));
|
---|
94 | Results.Add(new Result("Delta T", new PercentValue((min - Problem.BestKnownQuality) / Problem.BestKnownQuality)));
|
---|
95 | Results.Add(new Result("Nest Results", algorithmsResults));
|
---|
96 |
|
---|
97 | base.Initialize(cancellationToken);
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected override void Run(CancellationToken cancellationToken) {
|
---|
101 | var worst = qualities.Select((v, i) => new { Index = i, Quality = v }).MaxItems(x => x.Quality).First();
|
---|
102 | var min = worst.Quality;
|
---|
103 |
|
---|
104 | while (ExecutionTime < MaximumRuntime) {
|
---|
105 | algorithms[worst.Index].Start(cancellationToken);
|
---|
106 | qualities[worst.Index] = (int)((DoubleValue)algorithms[worst.Index].Results["BestQuality"].Value).Value;
|
---|
107 | worst = qualities.Select((v, i) => new { Index = i, Quality = v }).MaxItems(x => x.Quality).First();
|
---|
108 | if (worst.Quality < min) {
|
---|
109 | min = worst.Quality;
|
---|
110 | Results["Nest with maximum T"].Value = new IntValue(worst.Index + 1);
|
---|
111 | Results["Maximum T"].Value = new IntValue(min);
|
---|
112 | Results["Best Solution Found At"].Value = new TimeSpanValue(ExecutionTime);
|
---|
113 | Results["Delta T"].Value = new PercentValue((min - Problem.BestKnownQuality) / Problem.BestKnownQuality);
|
---|
114 | }
|
---|
115 | if (cancellationToken.IsCancellationRequested) return;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|