#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 System.Linq;
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 {
///
/// An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.
///
[Item("Best GeneralizedQAP Solution Analyzer", "An operator for analyzing the best solution of Generalized Quadratic Assignment Problems.")]
[StorableClass]
public sealed class BestGQAPSolutionAnalyzer : SingleSuccessorOperator, IAssignmentsAwareGQAPOperator,
IQualitiesAwareGQAPOperator, IDistancesAwareGQAPOperator, IWeightsAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
IExpectedRandomQualityAwareGQAPOperator, IEquipmentNamesAwareGQAPOperator, ILocationNamesAwareGQAPOperator,
IBestKnownQualityAwareGQAPOperator, IBestKnownSolutionAwareGQAPOperator,
IEvaluatorAwareGQAPOperator, IAnalyzer {
public bool EnabledByDefault {
get { return true; }
}
#region Parameter Properties
public IScopeTreeLookupParameter AssignmentParameter {
get { return (IScopeTreeLookupParameter)Parameters["Assignment"]; }
}
public ILookupParameter MaximizationParameter {
get { return (ILookupParameter)Parameters["Maximization"]; }
}
public IScopeTreeLookupParameter QualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["Quality"]; }
}
public IScopeTreeLookupParameter FlowDistanceQualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["FlowDistanceQuality"]; }
}
public IScopeTreeLookupParameter InstallationQualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["InstallationQuality"]; }
}
public IScopeTreeLookupParameter OverbookedCapacityParameter {
get { return (IScopeTreeLookupParameter)Parameters["OverbookedCapacity"]; }
}
public ILookupParameter DistancesParameter {
get { return (ILookupParameter)Parameters["Distances"]; }
}
public ILookupParameter WeightsParameter {
get { return (ILookupParameter)Parameters["Weights"]; }
}
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 TransportationCostsParameter {
get { return (IValueLookupParameter)Parameters["TransportationCosts"]; }
}
public IValueLookupParameter ExpectedRandomQualityParameter {
get { return (IValueLookupParameter)Parameters["ExpectedRandomQuality"]; }
}
public ILookupParameter EquipmentNamesParameter {
get { return (ILookupParameter)Parameters["EquipmentNames"]; }
}
public ILookupParameter LocationNamesParameter {
get { return (ILookupParameter)Parameters["LocationNames"]; }
}
public ILookupParameter BestKnownQualityParameter {
get { return (ILookupParameter)Parameters["BestKnownQuality"]; }
}
public ILookupParameter BestKnownSolutionParameter {
get { return (ILookupParameter)Parameters["BestKnownSolution"]; }
}
public ILookupParameter BestSolutionParameter {
get { return (ILookupParameter)Parameters["BestSolution"]; }
}
public IValueLookupParameter ResultsParameter {
get { return (IValueLookupParameter)Parameters["Results"]; }
}
public IValueLookupParameter EvaluatorParameter {
get { return (IValueLookupParameter)Parameters["Evaluator"]; }
}
#endregion
[StorableConstructor]
private BestGQAPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
private BestGQAPSolutionAnalyzer(BestGQAPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new BestGQAPSolutionAnalyzer(this, cloner);
}
public BestGQAPSolutionAnalyzer()
: base() {
Parameters.Add(new LookupParameter("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
Parameters.Add(new LookupParameter("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
Parameters.Add(new LookupParameter("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
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("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
Parameters.Add(new ValueLookupParameter("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
Parameters.Add(new ScopeTreeLookupParameter("Assignment", GQAPSolutionCreator.AssignmentDescription));
Parameters.Add(new ScopeTreeLookupParameter("Quality", GQAPEvaluator.QualityDescription));
Parameters.Add(new ScopeTreeLookupParameter("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
Parameters.Add(new ScopeTreeLookupParameter("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
Parameters.Add(new ScopeTreeLookupParameter("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
Parameters.Add(new LookupParameter("EquipmentNames", GeneralizedQuadraticAssignmentProblem.EquipmentNamesDescription));
Parameters.Add(new LookupParameter("LocationNames", GeneralizedQuadraticAssignmentProblem.LocationNamesDescription));
Parameters.Add(new LookupParameter("BestKnownQuality", GeneralizedQuadraticAssignmentProblem.BestKnownQualityDescription));
Parameters.Add(new LookupParameter("BestKnownSolution", GeneralizedQuadraticAssignmentProblem.BestKnownSolutionDescription));
Parameters.Add(new LookupParameter("BestSolution", "The best GQAP solution found so far."));
Parameters.Add(new ValueLookupParameter("Results", "The result collection where the best GQAP solution should be stored."));
Parameters.Add(new ValueLookupParameter("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
}
public override IOperation Apply() {
var evaluator = EvaluatorParameter.ActualValue;
var assignments = AssignmentParameter.ActualValue;
var qualities = QualityParameter.ActualValue;
var equipmentNames = EquipmentNamesParameter.ActualValue;
var locationNames = LocationNamesParameter.ActualValue;
var flowDistanceQualities = FlowDistanceQualityParameter.ActualValue;
var installationQualities = InstallationQualityParameter.ActualValue;
var overbookedCapacities = OverbookedCapacityParameter.ActualValue;
var distances = DistancesParameter.ActualValue;
var weights = WeightsParameter.ActualValue;
var installationCosts = InstallationCostsParameter.ActualValue;
var demands = DemandsParameter.ActualValue;
var capacities = CapacitiesParameter.ActualValue;
var transportationCosts = TransportationCostsParameter.ActualValue;
var expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue;
var results = ResultsParameter.ActualValue;
var maximization = MaximizationParameter.ActualValue.Value;
var bestKnownQuality = BestKnownQualityParameter.ActualValue;
int bestIndex;
var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
if (maximization) bestIndex = tmp.MaxItems(x => x.Value).First().Index;
else bestIndex = tmp.MinItems(x => x.Value).First().Index;
if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
BestKnownSolutionParameter.ActualValue = new GQAPAssignment((IntegerVector)assignments[bestIndex].Clone(), (DoubleValue)qualities[bestIndex].Clone(), flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex], equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts, expectedRandomQuality, evaluator);
}
var assignment = BestSolutionParameter.ActualValue;
if (assignment == null) {
assignment = new GQAPAssignment((IntegerVector)assignments[bestIndex].Clone(), (DoubleValue)qualities[bestIndex].Clone(), flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex], equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts, expectedRandomQuality, evaluator);
assignment.Distances = distances;
BestSolutionParameter.ActualValue = assignment;
} else {
if (HasSolutionImproved(assignment.Quality.Value, qualities[bestIndex].Value, maximization)) {
assignment.Assignment = (IntegerVector)assignments[bestIndex].Clone();
assignment.Quality = (DoubleValue)qualities[bestIndex].Clone();
assignment.FlowDistanceQuality = (DoubleValue)flowDistanceQualities[bestIndex].Clone();
assignment.InstallationQuality = (DoubleValue)installationQualities[bestIndex].Clone();
assignment.OverbookedCapacity = (DoubleValue)overbookedCapacities[bestIndex].Clone();
assignment.EquipmentNames = equipmentNames;
assignment.LocationNames = locationNames;
assignment.Distances = distances;
assignment.Weights = weights;
assignment.InstallationCosts = installationCosts;
assignment.Demands = demands;
assignment.Capacities = capacities;
assignment.TransportationCosts = transportationCosts;
assignment.ExpectedRandomQuality = expectedRandomQuality;
assignment.Evaluator = evaluator;
}
}
if (!results.ContainsKey("Best GQAP Assignment"))
results.Add(new Result("Best GQAP Assignment", assignment));
return base.Apply();
}
private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
return maximization && oldQuality < quality || !maximization && oldQuality > quality;
}
}
}