1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
30 | using HeuristicLab.Operators;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 |
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.Scheduling.MRCPSP {
|
---|
37 | [Item("Feasibility Improvement Method (FIM) Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
|
---|
38 | [StorableClass]
|
---|
39 | public abstract class BasicMrcpspImprover : SingleSuccessorOperator, IImprovementOperator, IStochasticOperator {
|
---|
40 |
|
---|
41 | public MultiModeResourceConstrainedProjectSchedulingProblem Problem { get; set; }
|
---|
42 | public ILookupParameter<IRandom> RandomParameter => (ILookupParameter<IRandom>)Parameters["Random"];
|
---|
43 |
|
---|
44 | public ILookupParameter<IEncoding> EncodingParameter => (ILookupParameter<IEncoding>)Parameters["Encoding"];
|
---|
45 |
|
---|
46 | public ILookupParameter<DoubleValue> QualityParameter => (ILookupParameter<DoubleValue>)Parameters["Quality"];
|
---|
47 |
|
---|
48 | public ILookupParameter<BoolValue> MaximizationParameter => (ILookupParameter<BoolValue>)Parameters["Maximization"];
|
---|
49 |
|
---|
50 | public IValueLookupParameter<IntValue> ImprovementAttemptsParameter => (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"];
|
---|
51 |
|
---|
52 | public IValueLookupParameter<IntValue> SampleSizeParameter => (IValueLookupParameter<IntValue>)Parameters["SampleSize"];
|
---|
53 |
|
---|
54 | public ILookupParameter<IntValue> LocalEvaluatedSolutionsParameter => (ILookupParameter<IntValue>)Parameters["LocalEvaluatedSolutions"];
|
---|
55 |
|
---|
56 | public Func<Individual, IRandom, double> EvaluateFunc { get; set; }
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected BasicMrcpspImprover(bool deserializing) : base(deserializing) { }
|
---|
60 |
|
---|
61 | protected BasicMrcpspImprover(BasicMrcpspImprover original, Cloner cloner) : base(original, cloner) {
|
---|
62 | Problem = cloner.Clone(original.Problem);
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected BasicMrcpspImprover() {
|
---|
66 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
67 | Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
|
---|
68 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
|
---|
69 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem should be minimized or maximized."));
|
---|
70 | Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
|
---|
71 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of samples to draw from the neighborhood function at maximum.", new IntValue(300)));
|
---|
72 | Parameters.Add(new LookupParameter<IntValue>("LocalEvaluatedSolutions", "The number of solution evaluations that have been performed."));
|
---|
73 | }
|
---|
74 |
|
---|
75 | public override IOperation Apply() {
|
---|
76 | var random = RandomParameter.ActualValue;
|
---|
77 | var encoding = EncodingParameter.ActualValue;
|
---|
78 | var maximize = MaximizationParameter.ActualValue.Value;
|
---|
79 | var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
|
---|
80 | var sampleSize = SampleSizeParameter.ActualValue.Value;
|
---|
81 | var individual = encoding.GetIndividual(ExecutionContext.Scope);
|
---|
82 | //var quality = QualityParameter.ActualValue?.Value ?? EvaluateFunc(individual, random);
|
---|
83 |
|
---|
84 | var count = 0;
|
---|
85 | //for (var i = 0; i < maxAttempts; i++) {
|
---|
86 | // Individual best = null;
|
---|
87 | // var bestQuality = quality;
|
---|
88 | // foreach (var neighbor in GetNeighborsFunc(individual, random).Take(sampleSize)) {
|
---|
89 | // var q = EvaluateFunc(neighbor, random);
|
---|
90 | // count++;
|
---|
91 | // if (maximize && bestQuality > q || !maximize && bestQuality < q) continue;
|
---|
92 | // best = neighbor;
|
---|
93 | // bestQuality = q;
|
---|
94 | // }
|
---|
95 | // if (best == null) break;
|
---|
96 | // individual = best;
|
---|
97 | // quality = bestQuality;
|
---|
98 | //}
|
---|
99 |
|
---|
100 | if (Problem.CalculateERR(individual) <= 0)
|
---|
101 | return base.Apply();
|
---|
102 |
|
---|
103 | var randomKey = individual.RealVector("RandomKey");
|
---|
104 | var modeList = individual.IntegerVector("ModeList");
|
---|
105 |
|
---|
106 | var activities = Problem.Activities.Skip(1).Reverse().Skip(1).Reverse().ToList();
|
---|
107 |
|
---|
108 | var delta = new Dictionary<int, Dictionary<int, int>>();
|
---|
109 |
|
---|
110 | var index = 0;
|
---|
111 | foreach (var a in activities) {
|
---|
112 | delta[a.Number] = new Dictionary<int, int>();
|
---|
113 |
|
---|
114 | foreach (var m in a.Modes) {
|
---|
115 | var ml = new IntegerVector(modeList) { [index] = m.Number };
|
---|
116 | delta[a.Number][m.Number] = Measure(randomKey, ml);
|
---|
117 | count++;
|
---|
118 | }
|
---|
119 |
|
---|
120 | index++;
|
---|
121 | }
|
---|
122 |
|
---|
123 | var totalDelta = delta.Sum(a => a.Value.Sum(m => m.Value));
|
---|
124 | var deltas = new List<Tuple<int, int, int>>();
|
---|
125 | foreach (var a in delta.Keys) {
|
---|
126 | foreach (var m in delta[a].Keys) {
|
---|
127 | deltas.Add(Tuple.Create(delta[a][m], a, m));
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | var selectedQuality = random.NextDouble() * totalDelta;
|
---|
132 | index = 0;
|
---|
133 | var currentQuality = deltas[index].Item1;
|
---|
134 | while (currentQuality < selectedQuality) {
|
---|
135 | index++;
|
---|
136 | currentQuality += deltas[index].Item1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | individual.IntegerVector("ModeList")[activities.FindIndex(a => a.Number == deltas[index].Item2)] = deltas[index].Item3;
|
---|
140 | var quality = Problem.Evaluate(individual, random);
|
---|
141 |
|
---|
142 | LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
|
---|
143 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
144 | //individual.CopyToScope(ExecutionContext.Scope);
|
---|
145 | foreach (var val in individual.Values)
|
---|
146 | SetScopeValue(val.Key, ExecutionContext.Scope, val.Value);
|
---|
147 |
|
---|
148 | return base.Apply();
|
---|
149 | }
|
---|
150 |
|
---|
151 | protected abstract int Measure(RealVector randomKey, IntegerVector modeList);
|
---|
152 |
|
---|
153 | private static void SetScopeValue(string name, IScope scope, IItem value) {
|
---|
154 | if (scope == null) throw new ArgumentNullException(nameof(scope));
|
---|
155 | if (value == null) throw new ArgumentNullException(nameof(value));
|
---|
156 |
|
---|
157 | if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
|
---|
158 | else scope.Variables[name].Value = value;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|