Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/26/12 14:50:56 (13 years ago)
Author:
abeham
Message:

#1614: improved results output of GQAP

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/BestGQAPSolutionAnalyzer.cs

    r7412 r7415  
    2929using HeuristicLab.Parameters;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
    3132
    3233namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
     
    5152      get { return (LookupParameter<DoubleMatrix>)Parameters["Weights"]; }
    5253    }
     54    public LookupParameter<DoubleMatrix> InstallationCostsParameter {
     55      get { return (LookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
     56    }
     57    public LookupParameter<DoubleArray> DemandsParameter {
     58      get { return (LookupParameter<DoubleArray>)Parameters["Demands"]; }
     59    }
     60    public LookupParameter<DoubleArray> CapacitiesParameter {
     61      get { return (LookupParameter<DoubleArray>)Parameters["Capacities"]; }
     62    }
     63    public LookupParameter<DoubleValue> TransportationCostsParameter {
     64      get { return (LookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
     65    }
     66    public LookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
     67      get { return (LookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
     68    }
    5369    public ScopeTreeLookupParameter<IntegerVector> AssignmentParameter {
    5470      get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; }
     
    5672    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
    5773      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
     74    }
     75    public ScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter {
     76      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
     77    }
     78    public ScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter {
     79      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
     80    }
     81    public ScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter {
     82      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
    5883    }
    5984    public LookupParameter<GQAPAssignment> BestSolutionParameter {
     
    86111      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
    87112      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances between the locations."));
    88       Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the facilities."));
     113      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the equipments."));
     114      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The cost of installing equipment x at location y."));
     115      Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands of the equipments."));
     116      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities at the locations."));
     117      Parameters.Add(new LookupParameter<DoubleValue>("TransportationCosts", "The transportation cost represents the flow-unit per distance-unit cost factor. This value can also be set to 1 if these costs are factored into the weights matrix already."));
     118      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality."));
    89119      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", "The GQAP solutions from which the best solution should be analyzed."));
    90120      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the GQAP solutions which should be analyzed."));
     121      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", "The flow-distance qualities of the GQAP solutions which should be analyzed."));
     122      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", "The installation qualities of the GQAP solutions which should be analyzed."));
     123      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", "The overbooked capacities of the GQAP solutions which should be analyzed."));
    91124      Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution."));
    92125      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored."));
     
    109142
    110143    public override IOperation Apply() {
    111       DoubleMatrix distances = DistancesParameter.ActualValue;
    112       DoubleMatrix weights = WeightsParameter.ActualValue;
    113       ItemArray<IntegerVector> assignments = AssignmentParameter.ActualValue;
    114       ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
    115       ResultCollection results = ResultsParameter.ActualValue;
    116       bool max = MaximizationParameter.ActualValue.Value;
    117       DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
    118 
    119       var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray();
    120       if (max) sorted = sorted.Reverse().ToArray();
    121       int i = sorted.First().index;
    122 
    123       if (bestKnownQuality == null
    124           || max && qualities[i].Value > bestKnownQuality.Value
    125           || !max && qualities[i].Value < bestKnownQuality.Value) {
    126         // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known
    127         BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
    128         BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[i].Clone();
     144      var assignments = AssignmentParameter.ActualValue;
     145      var qualities = QualityParameter.ActualValue;
     146      var equipmentNames = EquipmentNamesParameter.ActualValue;
     147      var locationNames = LocationNamesParameter.ActualValue;
     148      var flowDistanceQualities = FlowDistanceQualityParameter.ActualValue;
     149      var installationQualities = InstallationQualityParameter.ActualValue;
     150      var overbookedCapacities = OverbookedCapacityParameter.ActualValue;
     151      var distances = DistancesParameter.ActualValue;
     152      var weights = WeightsParameter.ActualValue;
     153      var installationCosts = InstallationCostsParameter.ActualValue;
     154      var demands = DemandsParameter.ActualValue;
     155      var capacities = CapacitiesParameter.ActualValue;
     156      var transportationCosts = TransportationCostsParameter.ActualValue;
     157      var overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue;
     158      var results = ResultsParameter.ActualValue;
     159      var maximization = MaximizationParameter.ActualValue.Value;
     160      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
     161
     162      int bestIndex;
     163      var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value });
     164      if (maximization) bestIndex = tmp.SelectMax(x => x.Value).Index;
     165      else bestIndex = tmp.SelectMin(x => x.Value).Index;
     166
     167      if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) {
     168        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
     169        BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[bestIndex].Clone();
    129170      }
    130171
    131172      GQAPAssignment assignment = BestSolutionParameter.ActualValue;
    132173      if (assignment == null) {
    133         StringArray equipmentNames = EquipmentNamesParameter.ActualValue;
    134         StringArray locationNames = LocationNamesParameter.ActualValue;
    135         assignment = new GQAPAssignment(weights, (IntegerVector)assignments[i].Clone(), new DoubleValue(qualities[i].Value), equipmentNames, locationNames);
     174        assignment = new GQAPAssignment((IntegerVector)assignments[bestIndex].Clone(), (DoubleValue)qualities[bestIndex].Clone(),
     175          equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts,
     176          overbookedCapacityPenalty, flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex]);
    136177        assignment.Distances = distances;
    137178        BestSolutionParameter.ActualValue = assignment;
    138179        results.Add(new Result("Best GQAP Solution", assignment));
    139180      } else {
    140         if (max && assignment.Quality.Value < qualities[i].Value ||
    141           !max && assignment.Quality.Value > qualities[i].Value) {
     181        if (HasSolutionImproved(assignment.Quality.Value, qualities[bestIndex].Value, maximization)) {
     182          assignment.Assignment = (IntegerVector)assignments[bestIndex].Clone();
     183          assignment.Quality = (DoubleValue)qualities[bestIndex].Clone();
     184          assignment.EquipmentNames = equipmentNames;
     185          assignment.LocationNames = locationNames;
    142186          assignment.Distances = distances;
    143187          assignment.Weights = weights;
    144           assignment.Assignment = (IntegerVector)assignments[i].Clone();
    145           assignment.Quality.Value = qualities[i].Value;
     188          assignment.InstallationCosts = installationCosts;
     189          assignment.Demands = demands;
     190          assignment.Capacities = capacities;
     191          assignment.TransportationCosts = transportationCosts;
     192          assignment.OverbookedCapacityPenalty = overbookedCapacityPenalty;
     193          assignment.FlowDistanceQuality = (DoubleValue)flowDistanceQualities[bestIndex].Clone();
     194          assignment.InstallationQuality = (DoubleValue)installationQualities[bestIndex].Clone();
     195          assignment.OverbookedCapacity = (DoubleValue)overbookedCapacities[bestIndex].Clone();
    146196        }
    147197      }
     
    149199      return base.Apply();
    150200    }
     201
     202    private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) {
     203      return maximization && oldQuality < quality || !maximization && oldQuality > quality;
     204    }
    151205  }
    152206}
Note: See TracChangeset for help on using the changeset viewer.