Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/25/17 16:20:27 (7 years ago)
Author:
abeham
Message:

#2205: added GetSolution() method to FLP

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Problems.FacilityLocation/3.3/FacilityLocationProblem.cs

    r14587 r14607  
    3737  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 230)]
    3838  [StorableClass]
    39   public class FacilityLocationProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {
     39  public sealed class FacilityLocationProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {
    4040    public override bool Maximization {
    4141      get { return false; }
     
    6767
    6868    [StorableConstructor]
    69     protected FacilityLocationProblem(bool deserializing) : base(deserializing) { }
    70     protected FacilityLocationProblem(FacilityLocationProblem original, Cloner cloner)
     69    private FacilityLocationProblem(bool deserializing) : base(deserializing) { }
     70    private FacilityLocationProblem(FacilityLocationProblem original, Cloner cloner)
    7171      : base(original, cloner) {
    7272      openingCostsParameter = cloner.Clone(original.openingCostsParameter);
     
    153153    }
    154154
    155     public virtual double Evaluate(IntegerVector depotAssignment) {
     155    public double Evaluate(IntegerVector depotAssignment) {
    156156      var openingCosts = OpeningCostsParameter.Value;
    157157      var capacities = DepotCapacitiesParameter.Value;
     
    163163
    164164      try {
    165         Evaluate(depotAssignment, openingCosts, capacities, demands, deliveryCosts, out totalOpeningCosts, out totalDeliveryCosts, out depotUsage);
     165        return Evaluate(depotAssignment, openingCosts, capacities, demands, deliveryCosts, out totalOpeningCosts, out totalDeliveryCosts, out depotUsage);
    166166      } catch (IndexOutOfRangeException) {
    167167        throw new ArgumentException("The solution contains more customers than there are problem data for.");
    168168      }
     169    }
     170
     171    public FacilityLocationSolution GetSolution(IntegerVector depotAssignment) {
     172      var openingCosts = OpeningCostsParameter.Value;
     173      var capacities = DepotCapacitiesParameter.Value;
     174      var demands = CustomerDemandsParameter.Value;
     175      var deliveryCosts = DeliveryCostsParameter.Value;
     176
     177      double totalOpeningCosts, totalDeliveryCosts, fitness;
     178      double[] depotUsage;
     179
     180      try {
     181        fitness = Evaluate(depotAssignment, openingCosts, capacities, demands, deliveryCosts, out totalOpeningCosts, out totalDeliveryCosts, out depotUsage);
     182      } catch (IndexOutOfRangeException) {
     183        throw new ArgumentException("The solution contains more customers than there are problem data for.");
     184      }
    169185
    170186      var overbooking = depotUsage.Zip(capacities, (a, b) => a > b ? a - b : 0).Sum();
    171       if (overbooking > 0) // infeasible
    172         return openingCosts.Sum() + deliveryCosts.Sum() + overbooking;
    173 
    174       return totalOpeningCosts + totalDeliveryCosts;
    175     }
    176 
    177     public static void Evaluate(IEnumerable<int> customer2Depot, DoubleArray openingCosts, DoubleArray capacities, DoubleArray demands, DoubleMatrix deliveryCosts, out double totalOpeningCosts, out double totalDeliveryCosts, out double[] capacityDemandsPerDepot) {
     187
     188      return new FacilityLocationSolution() {
     189        OpeningCosts = (DoubleArray)openingCosts.Clone(),
     190        DepotCapacities = (DoubleArray)capacities.Clone(),
     191        CustomerDemands = (DoubleArray)demands.Clone(),
     192        DeliveryCosts = (DoubleMatrix)deliveryCosts.Clone(),
     193        CustomerToDepotAssignmentParameter = { Value = (IntegerVector)depotAssignment.Clone() },
     194        FitnessValueParameter = { Value = new DoubleValue(fitness) },
     195        TotalOpeningCostsParameter = { Value = new DoubleValue(totalOpeningCosts) },
     196        TotalDeliveryCostsParameter = { Value = new DoubleValue(totalDeliveryCosts) },
     197        TotalOverbookedCapacityParameter = { Value = new DoubleValue(overbooking) }
     198      };
     199    }
     200
     201    public static double Evaluate(IEnumerable<int> customer2Depot, DoubleArray openingCosts, DoubleArray capacities, DoubleArray demands, DoubleMatrix deliveryCosts, out double totalOpeningCosts, out double totalDeliveryCosts, out double[] capacityDemandsPerDepot) {
    178202      var D = capacities.Length;
    179203
     
    189213        customer++;
    190214      }
     215
     216
     217      var overbooking = capacityDemandsPerDepot.Zip(capacities, (a, b) => a > b ? a - b : 0).Sum();
     218      if (overbooking > 0) // infeasible
     219        return openingCosts.Sum() + deliveryCosts.Sum() + overbooking;
     220
     221      return totalOpeningCosts + totalDeliveryCosts;
    191222    }
    192223
Note: See TracChangeset for help on using the changeset viewer.