Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs @ 12440

Last change on this file since 12440 was 8007, checked in by svonolfe, 13 years ago

Merged changes from trunk into branch (#1177)

File size: 13.5 KB
RevLine 
[4360]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
[7958]33using HeuristicLab.Problems.Instances;
[4362]34using HeuristicLab.Problems.VehicleRouting.Interfaces;
[7958]35using HeuristicLab.Problems.VehicleRouting.Interpreters;
[4362]36using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
[4365]37using HeuristicLab.Problems.VehicleRouting.Variants;
[4360]38
39namespace HeuristicLab.Problems.VehicleRouting {
40  [Item("Vehicle Routing Problem", "Represents a Vehicle Routing Problem.")]
41  [Creatable("Problems")]
42  [StorableClass]
[8007]43  public sealed class VehicleRoutingProblem : Problem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent, IProblemInstanceConsumer<IVRPData> {
[4444]44    public string Filename { get; set; }
[7203]45
46    public static new Image StaticItemImage {
[5867]47      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
[4360]48    }
49
[4362]50    #region Parameter Properties
51    public ValueParameter<BoolValue> MaximizationParameter {
52      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
[4360]53    }
[5867]54    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
[4362]55      get { return MaximizationParameter; }
[4360]56    }
[4362]57    public ValueParameter<IVRPProblemInstance> ProblemInstanceParameter {
58      get { return (ValueParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
59    }
60    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
61      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
62    }
[5867]63    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
[4362]64      get { return BestKnownQualityParameter; }
65    }
[4860]66    public OptionalValueParameter<VRPSolution> BestKnownSolutionParameter {
67      get { return (OptionalValueParameter<VRPSolution>)Parameters["BestKnownSolution"]; }
68    }
[4365]69    public IValueParameter<IVRPCreator> SolutionCreatorParameter {
70      get { return (IValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
[4360]71    }
[5867]72    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
[4365]73      get { return SolutionCreatorParameter; }
[4362]74    }
[4365]75    public IValueParameter<IVRPEvaluator> EvaluatorParameter {
76      get { return (IValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
77    }
[5867]78    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
[4365]79      get { return EvaluatorParameter; }
80    }
[4362]81    #endregion
[4360]82
[4362]83    #region Properties
84    public IVRPProblemInstance ProblemInstance {
85      get { return ProblemInstanceParameter.Value; }
86      set { ProblemInstanceParameter.Value = value; }
[4360]87    }
88
[4860]89    public VRPSolution BestKnownSolution {
90      get { return BestKnownSolutionParameter.Value; }
91      set { BestKnownSolutionParameter.Value = value; }
92    }
93
[7852]94    public DoubleValue BestKnownQuality {
95      get { return BestKnownQualityParameter.Value; }
96      set { BestKnownQualityParameter.Value = value; }
97    }
98
[4362]99    public ISingleObjectiveEvaluator Evaluator {
[7852]100      get { return EvaluatorParameter.Value; }
[4360]101    }
102
[5867]103    IEvaluator IHeuristicOptimizationProblem.Evaluator {
[4362]104      get { return this.Evaluator; }
[4360]105    }
106
[4362]107    public ISolutionCreator SolutionCreator {
[7852]108      get { return SolutionCreatorParameter.Value; }
[4360]109    }
110    #endregion
111
112    [StorableConstructor]
113    private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
114    public VehicleRoutingProblem()
115      : base() {
116      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
[4362]117      Parameters.Add(new ValueParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
118      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
[4860]119      Parameters.Add(new OptionalValueParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
[4365]120
[7852]121      Parameters.Add(new ConstrainedValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions."));
122      Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions."));
123
124      EvaluatorParameter.Hidden = true;
125
[4360]126      InitializeRandomVRPInstance();
[4365]127      InitializeOperators();
[4360]128
129      AttachEventHandlers();
[4362]130      AttachProblemInstanceEventHandlers();
[4360]131    }
132
133    public override IDeepCloneable Clone(Cloner cloner) {
[7907]134      cloner.Clone(ProblemInstance);
[4752]135      return new VehicleRoutingProblem(this, cloner);
[4360]136    }
137
[4752]138    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
139      : base(original, cloner) {
140      this.AttachEventHandlers();
141    }
142
[4360]143    #region Events
144    public event EventHandler SolutionCreatorChanged;
145    private void OnSolutionCreatorChanged() {
146      EventHandler handler = SolutionCreatorChanged;
147      if (handler != null) handler(this, EventArgs.Empty);
148    }
149    public event EventHandler EvaluatorChanged;
150    private void OnEvaluatorChanged() {
151      EventHandler handler = EvaluatorChanged;
152      if (handler != null) handler(this, EventArgs.Empty);
153    }
154    #endregion
155
156    #region Helpers
157    [StorableHook(HookType.AfterDeserialization)]
[7958]158    private void AfterDeserialization() {
[4360]159      AttachEventHandlers();
[4362]160      AttachProblemInstanceEventHandlers();
[4360]161    }
162
[8007]163    [Storable(Name = "operators", AllowOneWay = true)]
164    private List<IOperator> StorableOperators {
165      set { Operators.AddRange(value); }
166    }
167
[4360]168    private void AttachEventHandlers() {
[4362]169      ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
[7861]170      BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
[8007]171      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
172      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
[4360]173    }
[4362]174
175    private void AttachProblemInstanceEventHandlers() {
[7852]176      var solutionCreatorParameter = SolutionCreatorParameter as ConstrainedValueParameter<IVRPCreator>;
177      solutionCreatorParameter.ValidValues.Clear();
[4365]178
[4362]179      if (ProblemInstance != null) {
[7852]180        EvaluatorParameter.Value = ProblemInstance.SolutionEvaluator;
[7881]181        IVRPCreator defaultCreator = null;
[8007]182        foreach (IVRPCreator creator in Operators.Where(o => o is IVRPCreator)) {
[7852]183          solutionCreatorParameter.ValidValues.Add(creator);
[7881]184          if (creator is Encodings.Alba.RandomCreator)
185            defaultCreator = creator;
[7852]186        }
[7881]187        if (defaultCreator != null)
188          solutionCreatorParameter.Value = defaultCreator;
189
[7852]190        ProblemInstance.EvaluationChanged += new EventHandler(ProblemInstance_EvaluationChanged);
[7958]191      }
[7852]192    }
[4860]193
[7861]194    private void EvalBestKnownSolution() {
[7852]195      if (BestKnownSolution != null) {
196        //call evaluator
197        BestKnownQuality = new DoubleValue(ProblemInstance.Evaluate(BestKnownSolution.Solution).Quality);
198        BestKnownSolution.Quality = BestKnownQuality;
199      } else {
200        BestKnownQuality = null;
[4362]201      }
[4360]202    }
[4362]203
[7861]204    void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
205      EvalBestKnownSolution();
206    }
207
208    void ProblemInstance_EvaluationChanged(object sender, EventArgs e) {
[8007]209      EvaluatorParameter.Value = ProblemInstance.SolutionEvaluator;
[7861]210      EvalBestKnownSolution();
211    }
212
[4362]213    void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
[7853]214      InitializeOperators();
[4362]215      AttachProblemInstanceEventHandlers();
[4365]216
217      OnSolutionCreatorChanged();
218      OnEvaluatorChanged();
219      OnOperatorsChanged();
[4360]220    }
[6907]221
222    public void SetProblemInstance(IVRPProblemInstance instance) {
223      ProblemInstanceParameter.ValueChanged -= new EventHandler(ProblemInstanceParameter_ValueChanged);
224
225      ProblemInstance = instance;
226      AttachProblemInstanceEventHandlers();
227
228      OnSolutionCreatorChanged();
229      OnEvaluatorChanged();
230
231      ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
232    }
233
[4362]234    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
235      OnSolutionCreatorChanged();
[4360]236    }
[4362]237    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
[8007]238      if (ProblemInstance != null)
239        ProblemInstance.SolutionEvaluator = EvaluatorParameter.Value;
[4362]240      OnEvaluatorChanged();
[4360]241    }
[4365]242
243    private void InitializeOperators() {
[8007]244      Operators.Clear();
[4365]245
246      if (ProblemInstance != null) {
[8007]247        Operators.AddRange(
[4365]248        ProblemInstance.Operators.Concat(
249          ApplicationManager.Manager.GetInstances<IGeneralVRPOperator>().Cast<IOperator>()).OrderBy(op => op.Name));
250      }
251
252      ParameterizeOperators();
253    }
254
255    private void ParameterizeOperators() {
[8007]256      foreach (IOperator op in Operators.OfType<IOperator>()) {
[4365]257        if (op is IMultiVRPOperator) {
[8003]258          (op as IMultiVRPOperator).SetOperators(Operators.OfType<IOperator>());
[4365]259        }
260      }
261    }
[4360]262    #endregion
263
264    private void InitializeRandomVRPInstance() {
265      System.Random rand = new System.Random();
266
[4362]267      CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
[4360]268      int cities = 100;
[4362]269
270      problem.Coordinates = new DoubleMatrix(cities + 1, 2);
271      problem.Demand = new DoubleArray(cities + 1);
272      problem.DueTime = new DoubleArray(cities + 1);
273      problem.ReadyTime = new DoubleArray(cities + 1);
274      problem.ServiceTime = new DoubleArray(cities + 1);
275
276      problem.Vehicles.Value = 100;
277      problem.Capacity.Value = 200;
278
279      for (int i = 0; i <= cities; i++) {
280        problem.Coordinates[i, 0] = rand.Next(0, 100);
281        problem.Coordinates[i, 1] = rand.Next(0, 100);
282
283        if (i == 0) {
284          problem.Demand[i] = 0;
285          problem.DueTime[i] = Int16.MaxValue;
286          problem.ReadyTime[i] = 0;
287          problem.ServiceTime[i] = 0;
288        } else {
289          problem.Demand[i] = rand.Next(10, 50);
[6851]290          problem.DueTime[i] = rand.Next((int)Math.Ceiling(problem.GetDistance(0, i, null)), 1200);
[4362]291          problem.ReadyTime[i] = problem.DueTime[i] - rand.Next(0, 100);
292          problem.ServiceTime[i] = 90;
293        }
294      }
295
296      this.ProblemInstance = problem;
[4360]297    }
[4860]298
299    public void ImportSolution(string solutionFileName) {
300      SolutionParser parser = new SolutionParser(solutionFileName);
301      parser.Parse();
302
303      HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding(ProblemInstance);
304
305      int cities = 0;
306      foreach (List<int> route in parser.Routes) {
307        Tour tour = new Tour();
308        tour.Stops.AddRange(route);
309        cities += tour.Stops.Count;
310
311        encoding.Tours.Add(tour);
312      }
313
314      if (cities != ProblemInstance.Coordinates.Rows - 1)
315        ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond with the problem data"));
316      else {
317        VRPSolution solution = new VRPSolution(ProblemInstance, encoding, new DoubleValue(0));
[7852]318        BestKnownSolutionParameter.Value = solution;
[4860]319      }
320    }
[7871]321
322    public void Load(IVRPData data) {
323      Type interpreterType = typeof(IVRPDataInterpreter<>).MakeGenericType(data.GetType());
324      var interpreters = ApplicationManager.Manager.GetInstances(interpreterType);
325      if (interpreters.Count() > 0) {
326        IVRPDataInterpreter interpreter = interpreters.First() as IVRPDataInterpreter;
327        VRPInstanceDescription instance = interpreter.Interpret(data);
328
329        Name = instance.Name;
330        Description = instance.Description;
[7894]331        if (ProblemInstance != null && instance.ProblemInstance != null &&
332          instance.ProblemInstance.GetType() == ProblemInstance.GetType())
333          SetProblemInstance(instance.ProblemInstance);
334        else
335          ProblemInstance = instance.ProblemInstance;
[7871]336
337        OnReset();
[7881]338        BestKnownQuality = null;
339        BestKnownSolution = null;
[7871]340
341        if (instance.BestKnownQuality != null) {
342          BestKnownQuality = new DoubleValue((double)instance.BestKnownQuality);
343        }
344
345        if (instance.BestKnownSolution != null) {
346          VRPSolution solution = new VRPSolution(ProblemInstance, instance.BestKnownSolution, new DoubleValue(0));
[7881]347          BestKnownSolution = solution;
[7871]348        }
349      } else {
350        throw new Exception("Cannot find an interpreter for " + data.GetType());
[7958]351      }
[7871]352    }
[4360]353  }
354}
Note: See TracBrowser for help on using the repository browser.