Free cookie consent management tool by TermsFeed Policy Generator

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

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

Readded DistanceMatrix parameter to the VRP problem instance (#1177)

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