#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Problems.VehicleRouting.Interfaces; namespace HeuristicLab.Problems.VehicleRouting.Encodings.General { [Item("VRPMoveEvaluator", "Evaluates a VRP move.")] [StorableClass] public abstract class VRPMoveEvaluator : VRPMoveOperator, ISingleObjectiveMoveEvaluator { public const string MovePrefix = "Move"; public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } public ILookupParameter MoveQualityParameter { get { return (ILookupParameter)Parameters["MoveQuality"]; } } [StorableConstructor] protected VRPMoveEvaluator(bool deserializing) : base(deserializing) { } public VRPMoveEvaluator() : base() { Parameters.Add(new LookupParameter("Quality", "The quality of the solution.")); Parameters.Add(new LookupParameter("MoveQuality", "The relative quality of the move.")); } //helper method to evaluate an updated individual protected void UpdateEvaluation(IVRPEncoding updatedTours) { IVRPEvaluator evaluator = ProblemInstance.EvaluatorParameter.Value.Clone() as IVRPEvaluator; Dictionary originalName = new Dictionary(); foreach (IParameter parameter in evaluator.Parameters) { if (parameter is ILookupParameter && parameter != evaluator.ProblemInstanceParameter && parameter != evaluator.VRPToursParameter) { originalName[parameter] = (parameter as ILookupParameter).ActualName; (parameter as ILookupParameter).ActualName = MovePrefix + (parameter as ILookupParameter).ActualName; } } try { this.ExecutionContext.Scope.Variables.Add(new Variable(evaluator.VRPToursParameter.ActualName, updatedTours)); IAtomicOperation op = this.ExecutionContext.CreateChildOperation(evaluator); op.Operator.Execute((IExecutionContext)op); } finally { foreach (IParameter parameter in originalName.Keys) { (parameter as ILookupParameter).ActualName = originalName[parameter]; } this.ExecutionContext.Scope.Variables.Remove(evaluator.VRPToursParameter.ActualName); } } protected abstract void EvaluateMove(); public override IOperation Apply() { EvaluateMove(); return base.Apply(); } } }