#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { [Item("N-Move Evaluator", "Evaluates an N-Move.")] [StorableClass] public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator, IAssignmentAwareGQAPOperator, IQualityAwareGQAPOperator, ITransportationCostsAwareGQAPOperator, IExpectedRandomQualityAwareGQAPOperator, IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator, IInstallationCostsAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, IEvaluatorAwareGQAPOperator { #region Parameter Descriptions public static readonly string MoveQualityDescription = "The quality of the move if it would be applied."; public static readonly string MoveFlowDistanceQualityDescription = "The quality of the move regarding the flow-distance criteria."; public static readonly string MoveInstallationQualityDescription = "The quality of the move regarding the installation costs."; public static readonly string MoveOverbookedCapacityDescription = "The sum of the overbooked capacities of the move relative to the capacity of each location."; #endregion #region Parameter Properties ILookupParameter IQualityAwareGQAPOperator.MaximizationParameter { get { return MaximizationParameter; } } ILookupParameter IGQAPMoveEvaluator.MaximizationParameter { get { return MaximizationParameter; } } public ILookupParameter MaximizationParameter { get { return (ILookupParameter)Parameters["Maximization"]; } } public ILookupParameter MoveQualityParameter { get { return (ILookupParameter)Parameters["MoveQuality"]; } } public ILookupParameter MoveFlowDistanceQualityParameter { get { return (ILookupParameter)Parameters["MoveFlowDistanceQuality"]; } } public ILookupParameter MoveInstallationQualityParameter { get { return (ILookupParameter)Parameters["MoveInstallationQuality"]; } } public ILookupParameter MoveOverbookedCapacityParameter { get { return (ILookupParameter)Parameters["MoveOverbookedCapacity"]; } } public ILookupParameter MoveParameter { get { return (ILookupParameter)Parameters["Move"]; } } public ILookupParameter AssignmentParameter { get { return (ILookupParameter)Parameters["Assignment"]; } } ILookupParameter ISingleObjectiveMoveEvaluator.QualityParameter { get { return QualityParameter; } } ILookupParameter IQualityAwareGQAPOperator.QualityParameter { get { return QualityParameter; } } public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } public ILookupParameter FlowDistanceQualityParameter { get { return (ILookupParameter)Parameters["FlowDistanceQuality"]; } } public ILookupParameter InstallationQualityParameter { get { return (ILookupParameter)Parameters["InstallationQuality"]; } } public ILookupParameter OverbookedCapacityParameter { get { return (ILookupParameter)Parameters["OverbookedCapacity"]; } } public IValueLookupParameter TransportationCostsParameter { get { return (IValueLookupParameter)Parameters["TransportationCosts"]; } } public IValueLookupParameter ExpectedRandomQualityParameter { get { return (IValueLookupParameter)Parameters["ExpectedRandomQuality"]; } } public ILookupParameter WeightsParameter { get { return (ILookupParameter)Parameters["Weights"]; } } public ILookupParameter DistancesParameter { get { return (ILookupParameter)Parameters["Distances"]; } } public ILookupParameter InstallationCostsParameter { get { return (ILookupParameter)Parameters["InstallationCosts"]; } } public ILookupParameter DemandsParameter { get { return (ILookupParameter)Parameters["Demands"]; } } public ILookupParameter CapacitiesParameter { get { return (ILookupParameter)Parameters["Capacities"]; } } public IValueLookupParameter EvaluatorParameter { get { return (IValueLookupParameter)Parameters["Evaluator"]; } } #endregion [StorableConstructor] protected GQAPNMoveEvaluator(bool deserializing) : base(deserializing) { } protected GQAPNMoveEvaluator(GQAPNMoveEvaluator original, Cloner cloner) : base(original, cloner) { } public GQAPNMoveEvaluator() : base() { Parameters.Add(new LookupParameter("Assignment", GQAPSolutionCreator.AssignmentDescription)); Parameters.Add(new LookupParameter("Move", GQAPNMoveGenerator.MoveDescription)); Parameters.Add(new LookupParameter("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription)); Parameters.Add(new LookupParameter("Quality", GQAPEvaluator.QualityDescription)); Parameters.Add(new LookupParameter("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription)); Parameters.Add(new LookupParameter("InstallationQuality", GQAPEvaluator.InstallationQualityDescription)); Parameters.Add(new LookupParameter("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription)); Parameters.Add(new LookupParameter("MoveQuality", MoveQualityDescription)); Parameters.Add(new LookupParameter("MoveFlowDistanceQuality", MoveFlowDistanceQualityDescription)); Parameters.Add(new LookupParameter("MoveInstallationQuality", MoveInstallationQualityDescription)); Parameters.Add(new LookupParameter("MoveOverbookedCapacity", MoveOverbookedCapacityDescription)); Parameters.Add(new ValueLookupParameter("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription)); Parameters.Add(new ValueLookupParameter("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription)); Parameters.Add(new LookupParameter("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription)); Parameters.Add(new LookupParameter("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription)); Parameters.Add(new LookupParameter("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription)); Parameters.Add(new LookupParameter("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription)); Parameters.Add(new LookupParameter("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription)); Parameters.Add(new ValueLookupParameter("Evaluator", "The evaluator that is used to evaluate GQAP solutions.")); } public override IDeepCloneable Clone(Cloner cloner) { return new GQAPNMoveEvaluator(this, cloner); } public static double Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, DoubleArray demands, DoubleArray capacities, double transportationCosts, double expectedRandomQuality, IGQAPEvaluator evaluator) { double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity; Evaluate(move, assignment, weights, distances, installationCosts, demands, capacities, evaluator, out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity); return evaluator.GetFitness(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity, transportationCosts, expectedRandomQuality); } public static void Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, DoubleArray demands, DoubleArray capacities, IGQAPEvaluator evaluator, out double moveFlowDistanceQuality, out double moveInstallationQuality, out double moveOverbookedCapacity) { moveFlowDistanceQuality = moveInstallationQuality = moveOverbookedCapacity = 0.0; int moves = move.N; var slack = (DoubleArray)capacities.Clone(); var oldSlack = (DoubleArray)slack.Clone(); bool first = true; foreach (var kvp in move.NewAssignments) { int equip = kvp.Key; int newLoc = kvp.Value; moveInstallationQuality -= installationCosts[equip, assignment[equip]]; moveInstallationQuality += installationCosts[equip, newLoc]; for (int j = 0; j < assignment.Length; j++) { if (!move.NewAssignments.ContainsKey(j)) { moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, assignment[j]]; moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]]; moveFlowDistanceQuality += weights[j, equip] * distances[assignment[j], newLoc]; moveFlowDistanceQuality -= weights[j, equip] * distances[assignment[j], assignment[equip]]; if (first) { // only once for each untouched equipment deduct the demand from the capacity slack[assignment[j]] -= demands[j]; oldSlack[assignment[j]] -= demands[j]; } } else { moveFlowDistanceQuality += weights[equip, j] * distances[newLoc, move.NewAssignments[j]]; moveFlowDistanceQuality -= weights[equip, j] * distances[assignment[equip], assignment[j]]; } } first = false; slack[newLoc] -= demands[equip]; oldSlack[assignment[equip]] -= demands[equip]; } moveOverbookedCapacity = evaluator.EvaluateOverbooking(slack, capacities) - evaluator.EvaluateOverbooking(oldSlack, capacities); } public override IOperation Apply() { var evaluator = EvaluatorParameter.ActualValue; double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity; double quality = QualityParameter.ActualValue.Value; double transportationCosts = TransportationCostsParameter.ActualValue.Value; double expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue.Value; double flowDistanceQuality = FlowDistanceQualityParameter.ActualValue.Value; double installationQuality = InstallationQualityParameter.ActualValue.Value; double overbookedCapacity = OverbookedCapacityParameter.ActualValue.Value; Evaluate(MoveParameter.ActualValue, AssignmentParameter.ActualValue, WeightsParameter.ActualValue, DistancesParameter.ActualValue, InstallationCostsParameter.ActualValue, DemandsParameter.ActualValue, CapacitiesParameter.ActualValue, evaluator, out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity); MoveFlowDistanceQualityParameter.ActualValue = new DoubleValue(flowDistanceQuality + moveFlowDistanceQuality); MoveInstallationQualityParameter.ActualValue = new DoubleValue(installationQuality + moveInstallationQuality); MoveOverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity + moveOverbookedCapacity); MoveQualityParameter.ActualValue = new DoubleValue(evaluator.GetFitness( flowDistanceQuality + moveFlowDistanceQuality, installationQuality + moveInstallationQuality, overbookedCapacity + moveOverbookedCapacity, transportationCosts, expectedRandomQuality)); return base.Apply(); } } }