Changeset 17711 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPProblemInstance.cs
- Timestamp:
- 08/03/20 18:06:16 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPProblemInstance.cs
r17698 r17711 28 28 using HeuristicLab.Data; 29 29 using HeuristicLab.Parameters; 30 using HeuristicLab.Problems.VehicleRouting.Encodings.General;31 30 using HeuristicLab.Problems.VehicleRouting.Interfaces; 32 31 … … 35 34 [StorableType("9A6CCE89-A4B6-4FA3-A150-181FC315B713")] 36 35 public abstract class VRPProblemInstance : ParameterizedNamedItem, IVRPProblemInstance, IStatefulItem { 37 IVRPEvaluator moveEvaluator;38 39 private object locker = new object();40 41 public IVRPEvaluator MoveEvaluator {42 get {43 lock (locker) {44 if (evaluator == null)45 return null;46 else {47 if (moveEvaluator == null) {48 moveEvaluator = evaluator.Clone() as IVRPEvaluator;49 50 foreach (IParameter parameter in moveEvaluator.Parameters) {51 if (parameter is ILookupParameter52 && parameter != moveEvaluator.ProblemInstanceParameter53 && parameter != moveEvaluator.VRPToursParameter) {54 (parameter as ILookupParameter).ActualName =55 VRPMoveEvaluator.MovePrefix +56 (parameter as ILookupParameter).ActualName;57 }58 }59 }60 61 return moveEvaluator;62 }63 }64 }65 }66 67 protected abstract IEnumerable<IOperator> GetOperators();68 protected abstract IEnumerable<IOperator> GetAnalyzers();69 70 public IEnumerable<IOperator> Operators {71 get {72 return GetOperators().Union(GetAnalyzers());73 }74 }75 36 76 37 protected ValueParameter<DoubleMatrix> CoordinatesParameter { … … 131 92 } 132 93 94 public virtual IEnumerable<IOperator> FilterOperators(IEnumerable<IOperator> operators) { 95 return operators.Where(x => x is IVRPOperator); 96 } 97 133 98 protected virtual double CalculateDistance(int start, int end) { 134 double distance = 0.0; 135 136 distance = 99 var distance = 137 100 Math.Sqrt( 138 101 Math.Pow(Coordinates[start, 0] - Coordinates[end, 0], 2) + … … 172 135 //cache for performance improvement 173 136 private DoubleMatrix distanceMatrix = null; 174 private IVRPEvaluator evaluator = null; 175 176 public IVRPEvaluator SolutionEvaluator { 177 get { 178 return evaluator; 179 } 180 set { 181 lock (locker) { 182 moveEvaluator = null; 183 evaluator = value; 184 EvalBestKnownSolution(); 185 } 186 } 187 } 137 188 138 189 139 public virtual double GetDistance(int start, int end, IVRPEncodedSolution solution) { … … 208 158 } 209 159 210 public bool Feasible(IVRPEncodedSolution solution) { 211 return evaluator.Feasible( 212 evaluator.Evaluate( 213 this, solution)); 214 } 215 216 public bool TourFeasible(Tour tour, IVRPEncodedSolution solution) { 217 return evaluator.Feasible( 218 evaluator.EvaluateTour( 219 this, tour, solution)); 160 protected virtual VRPEvaluation CreateTourEvaluation() { 161 return new VRPEvaluation(); 220 162 } 221 163 222 164 public VRPEvaluation Evaluate(IVRPEncodedSolution solution) { 223 return evaluator.Evaluate(this, solution); 165 VRPEvaluation evaluation = CreateTourEvaluation(); 166 evaluation.IsFeasible = true; 167 foreach (Tour tour in solution.GetTours()) { 168 EvaluateTour(evaluation, tour, solution); 169 } 170 return evaluation; 224 171 } 225 172 226 173 public VRPEvaluation EvaluateTour(Tour tour, IVRPEncodedSolution solution) { 227 return evaluator.EvaluateTour(this, tour, solution); 228 } 229 230 public bool Feasible(VRPEvaluation eval) { 231 return evaluator.Feasible(eval); 174 VRPEvaluation evaluation = CreateTourEvaluation(); 175 evaluation.IsFeasible = true; 176 EvaluateTour(evaluation, tour, solution); 177 return evaluation; 232 178 } 233 179 234 180 public double GetInsertionCosts(VRPEvaluation eval, IVRPEncodedSolution solution, int customer, int tour, int index, out bool feasible) { 235 return evaluator.GetInsertionCosts(this, solution, eval, customer, tour, index, out feasible); 236 } 181 bool tourFeasible; 182 double costs = GetTourInsertionCosts( 183 solution, 184 eval.InsertionInfo.GetTourInsertionInfo(tour), 185 index, 186 customer, out tourFeasible); 187 188 feasible = tourFeasible; 189 190 return costs; 191 } 192 protected abstract double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer, out bool feasible); 193 194 protected abstract void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution); 237 195 238 196 … … 241 199 protected void EvalBestKnownSolution() { 242 200 EventHandler tmp = EvaluationChanged; 243 if (tmp != null) 244 tmp(this, null); 245 } 246 247 protected abstract IVRPEvaluator Evaluator { get; } 248 protected abstract IVRPCreator Creator { get; } 201 if (tmp != null) tmp(this, null); 202 } 249 203 250 204 [StorableConstructor] … … 262 216 Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1))); 263 217 264 evaluator = Evaluator;265 218 AttachEventHandlers(); 266 219 } … … 268 221 protected VRPProblemInstance(VRPProblemInstance original, Cloner cloner) 269 222 : base(original, cloner) { 270 evaluator = Evaluator;271 223 AttachEventHandlers(); 272 224 } … … 274 226 [StorableHook(HookType.AfterDeserialization)] 275 227 private void AfterDeserialization() { 276 evaluator = Evaluator;277 228 AttachEventHandlers(); 278 229 }
Note: See TracChangeset
for help on using the changeset viewer.