Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added initial version - WIP (#1177)

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