Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSolverNet/GQAPListSolver.cs

Last change on this file was 16728, checked in by abeham, 5 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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
22using System;
23using System.Threading;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Optimization;
30using localsolver;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSolverNet {
33  [Item("LocalSolver List (GQAP)", "LocalSolver algorithm solving the GQAP using list decision variables")]
34  [StorableType("BBB27FAA-DCFD-4DB4-A465-4A02E1A0EDA9")]
35  [Creatable(CreatableAttribute.Categories.Algorithms)]
36  public sealed class GQAPListSolver : ContextAlgorithm<LocalSolverContext, IntegerVectorEncoding> {
37    public override bool SupportsPause {
38      get { return false; }
39    }
40
41    public override Type ProblemType {
42      get { return typeof(GQAP); }
43    }
44
45    public new GQAP Problem {
46      get { return (GQAP)base.Problem; }
47      set { base.Problem = value; }
48    }
49
50    // LS Program variables
51    private LSExpression[] x;
52    private LSExpression obj;
53    private LocalSolver localSolver;
54
55
56    [StorableConstructor]
57    private GQAPListSolver(StorableConstructorFlag _) : base(_) { }
58    private GQAPListSolver(GQAPListSolver original, Cloner cloner)
59    : base(original, cloner) {
60    }
61    public GQAPListSolver() {
62      Problem = new GQAP();
63      MaximumEvaluationsParameter.Hidden = true;
64      MaximumIterationsParameter.Hidden = true;
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new GQAPListSolver(this, cloner);
69    }
70
71    private double prevObj;
72    private DateTime lastUpdate;
73    private CancellationToken token;
74
75    private void LocalSolverOnIterationTicked(LocalSolver ls, LSCallbackType type) {
76      IResult result;
77      Context.Iterations++;
78      if ((DateTime.UtcNow - lastUpdate) > TimeSpan.FromSeconds(1)) {
79        if (Results.TryGetValue("Iterations", out result))
80          ((IntValue)result.Value).Value = Context.Iterations;
81        else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
82        lastUpdate = DateTime.UtcNow;
83      }
84
85      if (token.IsCancellationRequested) localSolver.Stop();
86
87      var curObj = obj.GetDoubleValue();
88
89      if (curObj >= prevObj) return;
90      UpdateSolution(curObj);
91
92      Context.RunOperator(Analyzer, CancellationToken.None);
93
94      if (StoppingCriterion()) localSolver.Stop();
95    }
96
97    private void UpdateSolution(double obj) {
98      IResult result;
99      prevObj = obj;
100      Context.BestQuality = obj;
101
102      if (Results.TryGetValue("BestQuality", out result))
103        ((DoubleValue)result.Value).Value = Context.BestQuality;
104      else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
105
106      var locations = Problem.ProblemInstance.Capacities.Length;
107      var best = new int[Problem.ProblemInstance.Demands.Length];
108      for (var j = 0; j < locations; j++) {
109        var xcol = x[j].GetCollectionValue();
110        for (var i = 0; i < xcol.Count(); i++) {
111          var equip = xcol.Get(i);
112          best[equip] = j;
113        }
114      }
115      var bestVec = new IntegerVector(best);
116      var eval = Problem.ProblemInstance.Evaluate(bestVec);
117      Context.BestSolution = new GQAPSolution(bestVec, eval);
118
119      var scope = Context.ToScope(new GQAPSolution(new IntegerVector(best), (Evaluation)eval.Clone()), Problem.ProblemInstance.ToSingleObjective(eval));
120      Context.ReplaceIncumbent(scope);
121
122      if (Results.TryGetValue("BestSolution", out result))
123        result.Value = Context.BestSolution;
124      else Results.Add(new Result("BestSolution", Context.BestSolution));
125    }
126
127    protected override void Initialize(CancellationToken cancellationToken) {
128      base.Initialize(cancellationToken);
129
130      prevObj = double.MaxValue;
131    }
132
133    protected override void Run(CancellationToken cancellationToken) {
134      base.Run(cancellationToken);
135      token = cancellationToken;
136      lastUpdate = DateTime.UtcNow.AddSeconds(-1);
137      localSolver = new LocalSolver();
138
139      // Declares the optimization model
140      var model = localSolver.GetModel();
141
142      var data = Problem.ProblemInstance;
143
144      x = new LSExpression[data.Capacities.Length];
145      // x[f,l] = 1 if equipments f is on location l, 0 otherwise
146      for (int r = 0; r < data.Capacities.Length; r++) {
147        x[r] = model.List(data.Demands.Length);
148      }
149
150      // All equipments are installed in exactly 1 location
151      model.Constraint(model.Partition(x));
152
153      var demandsArray = model.Array(data.Demands);
154
155      // Create distances as an array to be accessed by an at operator
156      var weightsJagged = new double[data.Demands.Length][];
157      for (var i = 0; i < data.Demands.Length; i++) {
158        weightsJagged[i] = new double[data.Demands.Length];
159        for (var j = 0; j < data.Demands.Length; j++) {
160          weightsJagged[i][j] = data.Weights[i, j];
161        }
162      }
163      var distancesJagged = new double[data.Capacities.Length][];
164      for (var i = 0; i < data.Capacities.Length; i++) {
165        distancesJagged[i] = new double[data.Capacities.Length];
166        for (var j = 0; j < data.Capacities.Length; j++)
167          distancesJagged[i][j] = data.Distances[i, j];
168      }
169      var installJagged = new double[data.Demands.Length][];
170      for (var i = 0; i < data.Demands.Length; i++) {
171        installJagged[i] = new double[data.Capacities.Length];
172        for (var j = 0; j < data.Capacities.Length; j++)
173          installJagged[i][j] = data.InstallationCosts[i, j];
174      }
175      var weightsArray = model.Array(weightsJagged);
176      var distancesArray = model.Array(distancesJagged);
177      var installCostsArray = model.Array(installJagged);
178
179      obj = model.Sum();
180      // All locations contain not more equipments than there is capacity for
181      for (int l = 0; l < data.Capacities.Length; l++) {
182        var c = model.Count(x[l]);
183        var demandSelector = model.Function(i => demandsArray[x[l][i]]);
184        var assignedDemand = model.Sum(model.Range(0, c), demandSelector);
185        model.Constraint(assignedDemand <= data.Capacities[l]);
186
187        var installCostSelector = model.Function(i => installCostsArray[x[l][i], l]);
188        var installCosts = model.Sum(model.Range(0, c), installCostSelector);
189        obj.AddOperand(installCosts);
190       
191        // Flow to other equipments
192        for (int k = 0; k < data.Capacities.Length; k++) {
193          var kc = model.Count(x[k]);
194          var flowCostSelector = model.Function(j => model.Sum(model.Range(0, c), model.Function(i => weightsArray[x[l][i], x[k][j]] * distancesArray[l, k] * data.TransportationCosts)));
195          obj.AddOperand(model.Sum(model.Range(0, kc), flowCostSelector));
196        }
197      }
198
199      model.Minimize(this.obj);
200
201      try {
202        model.Close();
203
204        // Parameterizes the solver.
205        LSPhase phase = localSolver.CreatePhase();
206        phase.SetTimeLimit((int)Math.Ceiling(MaximumRuntime.TotalSeconds));
207
208        localSolver.AddCallback(LSCallbackType.IterationTicked, LocalSolverOnIterationTicked);
209
210        localSolver.Solve();
211
212        var curObj = this.obj.GetDoubleValue();
213        if (curObj < prevObj)
214          UpdateSolution(curObj);
215
216        localSolver.RemoveCallback(LSCallbackType.IterationTicked, LocalSolverOnIterationTicked);
217      } finally {
218        localSolver.Dispose();
219      }
220
221      Context.RunOperator(Analyzer, CancellationToken.None);
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.