#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;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
[Item("GQAPPathRelinking", "Operator that performs path relinking between two solutions. It is described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.")]
[StorableClass]
public class GQAPPathRelinking : GQAPCrossover, IQualitiesAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator,
IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator, IInstallationCostsAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
IOverbookedCapacityPenaltyAwareGQAPOperator {
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 DemandsParameter {
get { return (ILookupParameter)Parameters["Demands"]; }
}
public ILookupParameter CapacitiesParameter {
get { return (ILookupParameter)Parameters["Capacities"]; }
}
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 IValueLookupParameter TransportationCostsParameter {
get { return (IValueLookupParameter)Parameters["TransportationCosts"]; }
}
public IValueLookupParameter OverbookedCapacityPenaltyParameter {
get { return (IValueLookupParameter)Parameters["OverbookedCapacityPenalty"]; }
}
[StorableConstructor]
protected GQAPPathRelinking(bool deserializing) : base(deserializing) { }
protected GQAPPathRelinking(GQAPPathRelinking original, Cloner cloner) : base(original, cloner) { }
public GQAPPathRelinking()
: base() {
Parameters.Add(new LookupParameter("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
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("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
Parameters.Add(new LookupParameter("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
Parameters.Add(new LookupParameter("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
Parameters.Add(new LookupParameter("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
Parameters.Add(new LookupParameter("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
Parameters.Add(new ValueLookupParameter("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
Parameters.Add(new ValueLookupParameter("OverbookedCapacityPenalty", GeneralizedQuadraticAssignmentProblem.OverbookedCapacityPenaltyDescription));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new GQAPPathRelinking(this, cloner);
}
public static IntegerVector Apply(IRandom random, ItemArray parents, ItemArray qualities, DoubleArray demands,
DoubleArray capacities, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, DoubleValue transportationCosts,
DoubleValue overbookedCapacityPenalty) {
if (random == null) throw new ArgumentNullException("random", "No IRandom provider is given.");
if (parents == null || !parents.Any()) throw new ArgumentException("No parents given for path relinking.", "parents");
if (parents.Length != 2) throw new ArgumentException(String.Format("Two parents were expected for path relinking, but {0} was/were given.", parents.Length.ToString()), "parents");
if (parents[0].Length != parents[1].Length) throw new ArgumentException("The length of the parents is not equal.", "parents");
if (qualities == null || qualities.Length == 0) throw new ArgumentException("The qualities are not given.", "qualities");
if (qualities.Length != parents.Length) throw new ArgumentException(String.Format("There are a different number of parents ({0}) than quality values ({1})", parents.Length.ToString(), qualities.Length.ToString()));
var source = parents[0];
var target = parents[1];
var nu = 1.0;
var pi_prime = (IntegerVector)source.Clone();
var fix = new HashSet();
var nonFix = new HashSet(Enumerable.Range(0, demands.Length));
var phi = new HashSet(GQAPIntegerVectorProximityCalculator.GetDifference(pi_prime, target));
while (phi.Any()) {
var B = new HashSet(new GQAPSolutionStructuralEqualityComparer());
foreach (var v in phi) {
pi_prime[v] = target[v];
var pi2 = makeFeasible(pi_prime, v);
double flowDistanceQuality, installationQuality, overbookedCapacity;
GQAPEvaluator.Evaluate(pi2, weights, distances, installationCosts, demands, capacities,
out flowDistanceQuality, out installationQuality, out overbookedCapacity);
if (overbookedCapacity <= 0.0) {
var quality = GQAPEvaluator.GetCombinedQuality(flowDistanceQuality, installationQuality, overbookedCapacity,
transportationCosts.Value, overbookedCapacityPenalty.Value);
var solution = new GQAPSolution(pi2, new DoubleValue(quality), new DoubleValue(flowDistanceQuality), new DoubleValue(installationQuality), new DoubleValue(overbookedCapacity));
if (B.Count >= nu * phi.Count) {
if (!B.Contains(solution) && quality <= B.Select(x => x.Quality.Value).Max()) {
// TODO: replace most similar element in B with worse cost
}
} else if (!B.Contains(solution)) {
B.Add(solution);
}
}
}
if (B.Any()) {
var pi = B.ChooseRandom(random);
var diff = GQAPIntegerVectorProximityCalculator.GetDifference(pi.Assignment, target);
var I = phi.Except(phi.Intersect(diff));
var i = I.ChooseRandom(random);
fix.Add(i);
nonFix.Remove(i);
pi_prime = pi.Assignment;
// TODO
}
}
return pi_prime;
}
protected override IntegerVector Cross(IRandom random, ItemArray parents) {
return Apply(random, parents, QualityParameter.ActualValue, DemandsParameter.ActualValue,
CapacitiesParameter.ActualValue, WeightsParameter.ActualValue, DistancesParameter.ActualValue,
InstallationCostsParameter.ActualValue, TransportationCostsParameter.ActualValue,
OverbookedCapacityPenaltyParameter.ActualValue);
}
private static IntegerVector makeFeasible(IntegerVector assignment, int equipment) {
// TODO: implement
return assignment;
}
}
}