Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14607 for branches


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

#2205: added GetSolution() method to FLP

Location:
branches/OptimizationNetworks
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.LocationRouting/3.3/LrpOrchestratorNode1.cs

    r14606 r14607  
    278278
    279279      var bestFlpSolution = (IntegerVector)flpResults["Best Solution"].Value;
    280       var bestFlpQuality = FlpParameter.Value.Evaluate(bestFlpSolution);
    281       var flpCustomerDemands = (DoubleArray)FlpParameter.Value.CustomerDemandsParameter.Value.Clone();
    282       var flpDeliveryCosts = (DoubleMatrix)FlpParameter.Value.DeliveryCostsParameter.Value.Clone();
    283       var flpDepotCapacities = (DoubleArray)FlpParameter.Value.DepotCapacitiesParameter.Value.Clone();
    284       var flpOpeningCosts = (DoubleArray)FlpParameter.Value.OpeningCostsParameter.Value.Clone();
    285       double totalOpeningCosts, totalDeliveryCosts; double[] depotUsage;
    286       FacilityLocationProblem.Evaluate(bestFlpSolution.ToArray(), flpOpeningCosts, flpDepotCapacities, flpCustomerDemands, flpDeliveryCosts, out totalOpeningCosts, out totalDeliveryCosts, out depotUsage);
    287       var flpSolution = new FacilityLocationSolution {
    288         OpeningCosts = flpOpeningCosts,
    289         DepotCapacities = flpDepotCapacities,
    290         CustomerDemands = flpCustomerDemands,
    291         DeliveryCosts = flpDeliveryCosts
    292       };
    293       flpSolution.CustomerToDepotAssignmentParameter.Value = bestFlpSolution;
    294       flpSolution.FitnessValueParameter.Value = new DoubleValue(bestFlpQuality);
    295       flpSolution.TotalOpeningCostsParameter.Value = new DoubleValue(totalOpeningCosts);
    296       flpSolution.TotalDeliveryCostsParameter.Value = new DoubleValue(totalDeliveryCosts);
    297       flpSolution.TotalOverbookedCapacityParameter.Value = new DoubleValue(bestFlpQuality - totalOpeningCosts - totalDeliveryCosts);
    298 
     280      var flpSolution = FlpParameter.Value.GetSolution(bestFlpSolution);
     281     
    299282      var depots = bestFlpSolution.Select((x, i) => Tuple.Create(x, i)).GroupBy(x => x.Item1, x => x.Item2);
    300283      var vrpSolutions = new ResultCollection(depots.Count());
     
    305288        var vrp = (VehicleRoutingProblem)VrpParameter.Value.Clone();
    306289        var vrpInstance = (CVRPProblemInstance)vrp.ProblemInstance;
    307         var coordinates = LrpUtils.GetVrpCoordinates(depotCoordinates, customerCoordinates, depot.Key, customers);
     290        var coordinates = LrpUtils.GetVrpCoordinates(depotCoordinates, customerCoordinates, depotIdx, customers);
    308291        var distances = LrpUtils.GetVrpDistances(coordinates, distanceType);
    309292        vrpInstance.Coordinates = new DoubleMatrix(coordinates);
  • 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.