Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/06/12 04:29:56 (12 years ago)
Author:
abeham
Message:

#1614: restructured architecture to allow for different evaluator with different penalty strategies

Location:
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators/GQAPEvaluator.cs

    r7593 r7970  
    3131
    3232namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
    33   [Item("GQAPEvaluator", "Evaluates solutions to the generalized quadratic assignment problem.")]
     33  [Item("GQAPEvaluator", "Base class for solution evaluators for the generalized quadratic assignment problem.")]
    3434  [StorableClass]
    35   public class GQAPEvaluator : SingleSuccessorOperator, IGQAPEvaluator,
     35  public abstract class GQAPEvaluator : SingleSuccessorOperator, IGQAPEvaluator,
    3636  IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
    37   IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, IAssignmentAwareGQAPOperator {
     37  IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, IAssignmentAwareGQAPOperator,
     38  IExpectedRandomQualityAwareGQAPOperator {
    3839
    3940    #region Parameter Descriptions
     
    6263      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
    6364    }
    64     public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
    65       get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
     65    public IValueLookupParameter<DoubleValue> ExpectedRandomQualityParameter {
     66      get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
    6667    }
    6768    public ILookupParameter<DoubleMatrix> WeightsParameter {
     
    8788    protected GQAPEvaluator(bool deserializing) : base(deserializing) { }
    8889    protected GQAPEvaluator(GQAPEvaluator original, Cloner cloner) : base(original, cloner) { }
    89     public GQAPEvaluator()
    90       : base() {
     90    public GQAPEvaluator() : base() {
    9191      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
    9292      Parameters.Add(new LookupParameter<DoubleValue>("Quality", QualityDescription));
     
    9595      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", OverbookedCapacityDescription));
    9696      Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
    97       Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", GeneralizedQuadraticAssignmentProblem.OverbookedCapacityPenaltyDescription));
     97      Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
    9898      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
    9999      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
     
    104104    }
    105105
    106     public override IDeepCloneable Clone(Cloner cloner) {
    107       return new GQAPEvaluator(this, cloner);
    108     }
    109 
    110     public static double Evaluate(IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances,
    111                                   DoubleMatrix installCosts, DoubleArray demands, DoubleArray capacities,
    112                                   DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty) {
     106    public virtual double Evaluate(IntegerVector assignment, DoubleMatrix weights,
     107      DoubleMatrix distances, DoubleMatrix installCosts, DoubleArray demands,
     108      DoubleArray capacities, double transportationCosts, double expectedRandomQuality) {
    113109      double flowDistanceQuality, installationQuality, overbookedCapacity;
    114110      Evaluate(assignment, weights, distances, installCosts, demands, capacities,
    115111        out flowDistanceQuality, out installationQuality, out overbookedCapacity);
    116       return GetCombinedQuality(flowDistanceQuality, installationQuality, overbookedCapacity,
    117         transportationCosts.Value, overbookedCapacityPenalty.Value);
     112      return GetFitness(flowDistanceQuality, installationQuality, overbookedCapacity,
     113        transportationCosts, expectedRandomQuality);
    118114    }
    119115
    120     public static void Evaluate(IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances,
    121                                 DoubleMatrix installCosts, DoubleArray demands, DoubleArray capacities,
    122                                 out double flowDistanceQuality, out double installationQuality, out double overbookedCapacity) {
     116    public virtual void Evaluate(IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances,
     117      DoubleMatrix installCosts, DoubleArray demands, DoubleArray capacities,
     118      out double flowDistanceQuality, out double installationQuality,
     119      out double overbookedCapacity) {
    123120      flowDistanceQuality = 0;
    124121      installationQuality = 0;
     
    135132    }
    136133
    137     public static double EvaluateOverbooking(DoubleArray slack, DoubleArray capacities) {
    138       return slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum();
     134    public virtual double EvaluateOverbooking(DoubleArray slack, DoubleArray capacities) {
     135      return slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -100.0 * x.V / capacities[x.Index]).Sum();
    139136    }
    140137
    141     public static double GetCombinedQuality(double flowDistanceQuality, double installationQuality, double overbookedCapacity,
    142       double transportationCosts, double overbookedCapacityPenalty) {
    143       return installationQuality
    144         + transportationCosts * flowDistanceQuality
    145         + overbookedCapacityPenalty * overbookedCapacity;
    146     }
     138    public abstract double GetFitness(double flowDistanceQuality,
     139      double installationQuality, double overbookedCapacity,
     140      double transportationCosts, double expectedRandomQuality);
    147141
    148142    public override IOperation Apply() {
     
    154148      var capacities = CapacitiesParameter.ActualValue;
    155149      var transportCosts = TransportationCostsParameter.ActualValue.Value;
    156       double penalty = OverbookedCapacityPenaltyParameter.ActualValue.Value;
     150      var expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue.Value;
    157151
    158152      if (weights.Rows != weights.Columns || distances.Rows != distances.Columns
     
    169163      OverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity);
    170164
    171       QualityParameter.ActualValue = new DoubleValue(GetCombinedQuality(flowDistanceQuality, installationQuality, overbookedCapacity, transportCosts, penalty));
     165      QualityParameter.ActualValue = new DoubleValue(GetFitness(flowDistanceQuality, installationQuality, overbookedCapacity, transportCosts, expectedRandomQuality));
    172166      return base.Apply();
    173167    }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators/GQAPNMoveEvaluator.cs

    r7505 r7970  
    2020#endregion
    2121
    22 using System.Linq;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
     
    3534  public class GQAPNMoveEvaluator : SingleSuccessorOperator, IGQAPNMoveEvaluator, IAssignmentAwareGQAPOperator,
    3635  IQualityAwareGQAPOperator, ITransportationCostsAwareGQAPOperator,
    37   IOverbookedCapacityPenaltyAwareGQAPOperator, IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator,
    38   IInstallationCostsAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator {
     36  IExpectedRandomQualityAwareGQAPOperator, IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator,
     37  IInstallationCostsAwareGQAPOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator,
     38  IEvaluatorAwareGQAPOperator {
    3939
    4040    #region Parameter Descriptions
     
    9494      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
    9595    }
    96     public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
    97       get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
     96    public IValueLookupParameter<DoubleValue> ExpectedRandomQualityParameter {
     97      get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
    9898    }
    9999    public ILookupParameter<DoubleMatrix> WeightsParameter {
     
    111111    public ILookupParameter<DoubleArray> CapacitiesParameter {
    112112      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
     113    }
     114    public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
     115      get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
    113116    }
    114117    #endregion
     
    130133      Parameters.Add(new LookupParameter<DoubleValue>("MoveInstallationQuality", MoveInstallationQualityDescription));
    131134      Parameters.Add(new LookupParameter<DoubleValue>("MoveOverbookedCapacity", MoveOverbookedCapacityDescription));
    132       Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", GeneralizedQuadraticAssignmentProblem.OverbookedCapacityPenaltyDescription));
     135      Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
    133136      Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
    134137      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
     
    137140      Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
    138141      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
     142      Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
    139143    }
    140144
     
    144148
    145149    public static double Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
    146       DoubleArray demands, DoubleArray capacities, DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty) {
     150      DoubleArray demands, DoubleArray capacities, double transportationCosts, double expectedRandomQuality, IGQAPEvaluator evaluator) {
    147151      double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
    148       Evaluate(move, assignment, weights, distances, installationCosts, demands, capacities,
     152      Evaluate(move, assignment, weights, distances, installationCosts, demands, capacities, evaluator,
    149153        out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
    150       return GQAPEvaluator.GetCombinedQuality(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity,
    151         transportationCosts.Value, overbookedCapacityPenalty.Value);
     154      return evaluator.GetFitness(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity,
     155        transportationCosts, expectedRandomQuality);
    152156    }
    153157
    154158    public static void Evaluate(NMove move, IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts,
    155       DoubleArray demands, DoubleArray capacities, out double moveFlowDistanceQuality, out double moveInstallationQuality, out double moveOverbookedCapacity) {
     159      DoubleArray demands, DoubleArray capacities, IGQAPEvaluator evaluator, out double moveFlowDistanceQuality, out double moveInstallationQuality, out double moveOverbookedCapacity) {
    156160      moveFlowDistanceQuality = moveInstallationQuality = moveOverbookedCapacity = 0.0;
    157161      int moves = move.N;
     
    184188      }
    185189
    186       moveOverbookedCapacity = slack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum()
    187                                - oldSlack.Select((v, i) => new { V = v, Index = i }).Where(x => x.V < 0.0).Select(x => -x.V / capacities[x.Index]).Sum();
     190      moveOverbookedCapacity = evaluator.EvaluateOverbooking(slack, capacities) - evaluator.EvaluateOverbooking(oldSlack, capacities);
    188191    }
    189192
    190193    public override IOperation Apply() {
     194      var evaluator = EvaluatorParameter.ActualValue;
    191195      double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
    192196      double quality = QualityParameter.ActualValue.Value;
    193197      double transportationCosts = TransportationCostsParameter.ActualValue.Value;
    194       double overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue.Value;
     198      double expectedRandomQuality = ExpectedRandomQualityParameter.ActualValue.Value;
    195199
    196200      double flowDistanceQuality = FlowDistanceQualityParameter.ActualValue.Value;
     
    203207               InstallationCostsParameter.ActualValue,
    204208               DemandsParameter.ActualValue, CapacitiesParameter.ActualValue,
     209               evaluator,
    205210               out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
    206211
     
    209214      MoveOverbookedCapacityParameter.ActualValue = new DoubleValue(overbookedCapacity + moveOverbookedCapacity);
    210215
    211       MoveQualityParameter.ActualValue = new DoubleValue(GQAPEvaluator.GetCombinedQuality(
     216      MoveQualityParameter.ActualValue = new DoubleValue(evaluator.GetFitness(
    212217        flowDistanceQuality + moveFlowDistanceQuality,
    213218        installationQuality + moveInstallationQuality,
    214219        overbookedCapacity + moveOverbookedCapacity,
    215         transportationCosts,
    216         overbookedCapacityPenalty));
     220        transportationCosts, expectedRandomQuality));
    217221      return base.Apply();
    218222    }
Note: See TracChangeset for help on using the changeset viewer.