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