1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022017 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.Threading;


24  using HeuristicLab.Common;


25  using HeuristicLab.Core;


26  using HeuristicLab.Data;


27  using HeuristicLab.Encodings.IntegerVectorEncoding;


28  using HeuristicLab.Optimization;


29  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


30  using localsolver;


31 


32  namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSolverNet {


33  [Item("LocalSolver Integer (GQAP)", "LocalSolver algorithm solving the GQAP using integer decision variables")]


34  [StorableClass]


35  [Creatable(CreatableAttribute.Categories.Algorithms)]


36  public sealed class GQAPIntegerSolver : 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 GQAPIntegerSolver(bool deserializing) : base(deserializing) { }


58  private GQAPIntegerSolver(GQAPIntegerSolver original, Cloner cloner)


59  : base(original, cloner) {


60  }


61  public GQAPIntegerSolver() {


62  Problem = new GQAP();


63  MaximumEvaluationsParameter.Hidden = true;


64  MaximumIterationsParameter.Hidden = true;


65  }


66 


67  public override IDeepCloneable Clone(Cloner cloner) {


68  return new GQAPIntegerSolver(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  prevObj = curObj;


91  Context.BestQuality = curObj;


92 


93  if (Results.TryGetValue("BestQuality", out result))


94  ((DoubleValue)result.Value).Value = Context.BestQuality;


95  else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));


96 


97  var locations = Problem.ProblemInstance.Capacities.Length;


98  var best = new int[Problem.ProblemInstance.Demands.Length];


99  for (var i = 0; i < best.Length; i++) {


100  best[i] = (int)x[i].GetIntValue();


101  }


102  var bestVec = new IntegerVector(best);


103  var eval = Problem.ProblemInstance.Evaluate(bestVec);


104  Context.BestSolution = new GQAPSolution(bestVec, eval);


105 


106  var scope = Context.ToScope(new GQAPSolution(new IntegerVector(best), (Evaluation)eval.Clone()), Problem.ProblemInstance.ToSingleObjective(eval));


107  Context.ReplaceIncumbent(scope);


108 


109  if (Results.TryGetValue("BestSolution", out result))


110  result.Value = Context.BestSolution;


111  else Results.Add(new Result("BestSolution", Context.BestSolution));


112 


113  Context.RunOperator(Analyzer, CancellationToken.None);


114 


115  if (StoppingCriterion()) localSolver.Stop();


116  }


117 


118  protected override void Initialize(CancellationToken cancellationToken) {


119  base.Initialize(cancellationToken);


120 


121  prevObj = double.MaxValue;


122  }


123 


124  protected override void Run(CancellationToken cancellationToken) {


125  base.Run(cancellationToken);


126  token = cancellationToken;


127  lastUpdate = DateTime.UtcNow.AddSeconds(1);


128  localSolver = new LocalSolver();


129 


130  // Declares the optimization model


131  LSModel model = localSolver.GetModel();


132 


133  var data = Problem.ProblemInstance;


134 


135  // x[f] = location l, f ... facility/equipment


136  x = new LSExpression[data.Demands.Length];


137  for (int f = 0; f < data.Demands.Length; f++) {


138  x[f] = model.Int(0, data.Capacities.Length  1);


139  }


140 


141  // All locations contain not more equipments than there is capacity for


142  for (int l = 0; l < data.Capacities.Length; l++) {


143  var util = model.Sum();


144  for (var f = 0; f < data.Demands.Length; f++)


145  util.AddOperand((x[f] == l) * data.Demands[f]);


146  model.Constraint(util <= data.Capacities[l]);


147  }


148 


149  // Create distances as an array to be accessed by an at operator


150  var distancesJagged = new double[data.Capacities.Length][];


151  for (var i = 0; i < data.Capacities.Length; i++) {


152  distancesJagged[i] = new double[data.Capacities.Length];


153  for (var j = 0; j < data.Capacities.Length; j++)


154  distancesJagged[i][j] = data.Distances[i, j];


155  }


156  var installJagged = new double[data.Demands.Length][];


157  for (var i = 0; i < data.Demands.Length; i++) {


158  installJagged[i] = new double[data.Capacities.Length];


159  for (var j = 0; j < data.Capacities.Length; j++)


160  installJagged[i][j] = data.InstallationCosts[i, j];


161  }


162  LSExpression distancesArray = model.Array(distancesJagged);


163  LSExpression installCostsArray = model.Array(installJagged);


164 


165  // Minimize the sum of product distance*flow


166  obj = model.Sum();


167  for (int f1 = 0; f1 < data.Demands.Length; f1++) {


168  for (int f2 = 0; f2 < data.Demands.Length; f2++) {


169  obj.AddOperand(data.TransportationCosts * data.Weights[f1, f2] * distancesArray[x[f1], x[f2]]);


170  }


171  obj.AddOperand(installCostsArray[f1, x[f1]]);


172  }


173 


174  model.Minimize(obj);


175 


176  try {


177  model.Close();


178 


179  // Parameterizes the solver.


180  LSPhase phase = localSolver.CreatePhase();


181  phase.SetTimeLimit((int)Math.Ceiling(MaximumRuntime.TotalSeconds));


182 


183  localSolver.AddCallback(LSCallbackType.IterationTicked, LocalSolverOnIterationTicked);


184 


185  localSolver.Solve();


186  } finally {


187  localSolver.Dispose();


188  }


189 


190  Context.RunOperator(Analyzer, CancellationToken.None);


191  }


192  }


193  }

