Free cookie consent management tool by TermsFeed Policy Generator

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

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

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