Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPProblemInstance.cs @ 4376

Last change on this file since 4376 was 4376, checked in by svonolfe, 14 years ago

Added Potvin encoding (#1177)

File size: 7.5 KB
Line 
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.Linq;
25using System.Text;
26using HeuristicLab.Problems.VehicleRouting.Interfaces;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Core;
29using HeuristicLab.Parameters;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
35  [Item("VRPProblemInstance", "Represents a VRP instance.")]
36  [StorableClass]
37  public abstract class VRPProblemInstance: ParameterizedNamedItem, IVRPProblemInstance {
38    public IValueParameter<IVRPEvaluator> EvaluatorParameter {
39      get { return (ValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
40    }
41
42    public IValueParameter<IVRPCreator> SolutionCreatorParameter {
43      get { return (ValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
44    }
45
46    protected abstract IEnumerable<IOperator> GetOperators();
47    protected abstract IEnumerable<IOperator> GetAnalyzers();
48
49    public IEnumerable<IOperator> Operators {
50      get {
51        return GetOperators().Union(GetAnalyzers());
52      }
53    }
54   
55    protected ValueParameter<DoubleMatrix> CoordinatesParameter {
56      get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
57    }
58    protected OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter {
59      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
60    }
61    protected ValueParameter<BoolValue> UseDistanceMatrixParameter {
62      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
63    }
64    protected ValueParameter<IntValue> VehiclesParameter {
65      get { return (ValueParameter<IntValue>)Parameters["Vehicles"]; }
66    }
67    protected ValueParameter<DoubleArray> DemandParameter {
68      get { return (ValueParameter<DoubleArray>)Parameters["Demand"]; }
69    }
70
71    protected IValueParameter<DoubleValue> FleetUsageFactorParameter {
72      get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
73    }
74    protected IValueParameter<DoubleValue> DistanceFactorParameter {
75      get { return (IValueParameter<DoubleValue>)Parameters["EvalDistanceFactor"]; }
76    }
77
78    public DoubleMatrix Coordinates {
79      get { return CoordinatesParameter.Value; }
80      set { CoordinatesParameter.Value = value; }
81    }
82    public DoubleMatrix DistanceMatrix {
83      get { return DistanceMatrixParameter.Value; }
84      set { DistanceMatrixParameter.Value = value; }
85    }
86    public BoolValue UseDistanceMatrix {
87      get { return UseDistanceMatrixParameter.Value; }
88      set { UseDistanceMatrixParameter.Value = value; }
89    }
90    public IntValue Vehicles {
91      get { return VehiclesParameter.Value; }
92      set { VehiclesParameter.Value = value; }
93    }
94    public DoubleArray Demand {
95      get { return DemandParameter.Value; }
96      set { DemandParameter.Value = value; }
97    }
98    public virtual IntValue Cities {
99      get { return new IntValue(Demand.Length); }
100    }
101
102    public DoubleValue FleetUsageFactor {
103      get { return FleetUsageFactorParameter.Value; }
104      set { FleetUsageFactorParameter.Value = value; }
105    }
106    public DoubleValue DistanceFactor {
107      get { return DistanceFactorParameter.Value; }
108      set { DistanceFactorParameter.Value = value; }
109    }
110
111    private double CalculateDistance(int start, int end) {
112      double distance = 0.0;
113
114      distance =
115          Math.Sqrt(
116            Math.Pow(Coordinates[start, 0] - Coordinates[end, 0], 2) +
117            Math.Pow(Coordinates[start, 1] - Coordinates[end, 1], 2));
118
119      return distance;
120    }
121
122    private DoubleMatrix CreateDistanceMatrix() {
123      DoubleMatrix distanceMatrix = new DoubleMatrix(Coordinates.Rows, Coordinates.Rows);
124
125      for (int i = 0; i < distanceMatrix.Rows; i++) {
126        for (int j = i; j < distanceMatrix.Columns; j++) {
127          double distance = CalculateDistance(i, j);
128
129          distanceMatrix[i, j] = distance;
130          distanceMatrix[j, i] = distance;
131        }
132      }
133
134      return distanceMatrix;
135    }
136   
137    public double GetDistance(int start, int end) {
138      double distance = 0.0;
139
140      if (UseDistanceMatrix.Value) {
141        if (DistanceMatrixParameter.Value != null) {
142          distance = DistanceMatrixParameter.Value[start, end];
143        } else {
144          if (DistanceMatrixParameter.ActualValue == null) {
145            DistanceMatrixParameter.ActualValue = CreateDistanceMatrix();
146          }
147
148          distance = DistanceMatrix[start, end];
149        }
150      } else {
151        distance = CalculateDistance(start, end);
152      }
153
154      return distance;
155    }
156
157    public bool Feasible(IVRPEncoding solution) {
158      return EvaluatorParameter.Value.Feasible(this, solution);
159    }
160
161    public bool Feasible(Tour tour) {
162      return EvaluatorParameter.Value.Feasible(this, tour);
163    }
164
165    public double Evaluate(IVRPEncoding solution) {
166      return EvaluatorParameter.Value.Evaluate(this, solution);
167    }
168
169    public double Evaluate(Tour tour) {
170      return EvaluatorParameter.Value.Evaluate(this, tour);
171    }
172
173    protected abstract IVRPEvaluator Evaluator { get; }
174    protected abstract IVRPCreator Creator { get; }
175   
176    [StorableConstructor]
177    protected VRPProblemInstance(bool deserializing) : base(deserializing) { }
178
179    public VRPProblemInstance()
180      : base() {
181      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
182      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
183      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
184      Parameters.Add(new ValueParameter<IntValue>("Vehicles", "The number of vehicles.", new IntValue(0)));
185      Parameters.Add(new ValueParameter<DoubleArray>("Demand", "The demand of each customer.", new DoubleArray()));
186
187      Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(100)));
188      Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
189
190      Parameters.Add(new ValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions.", Creator));
191      Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions.", Evaluator));
192    }
193  }
194}
Note: See TracBrowser for help on using the repository browser.