Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17709


Ignore:
Timestamp:
08/03/20 11:35:39 (4 years ago)
Author:
abeham
Message:

#2521: working on VRP

Location:
branches/2521_ProblemRefactoring
Files:
12 added
3 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Core/3.3/Interfaces/IParameterizedItem.cs

    r17226 r17709  
    3030  public interface IParameterizedItem : IItem {
    3131    IKeyedItemCollection<string, IParameter> Parameters { get; }
    32 
     32    // TODO: Move the following to separate interface, not only paramterized items
     33    // could be collected for run. Mabe ICollectible
    3334    void CollectParameterValues(IDictionary<string, IItem> values);
    3435  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/EvaluationResult.cs

    r17381 r17709  
    3535    [Storable]
    3636    private readonly Dictionary<string, object> data = new Dictionary<string, object>();
     37    public IEnumerable<KeyValuePair<string, object>> AdditionalData => data.AsEnumerable();
    3738
    3839    protected EvaluationResult() : base() { }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEvaluationResult.cs

    r17459 r17709  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using HEAL.Attic;
    2324using HeuristicLab.Core;
     
    2829
    2930    //TODO: make methods generic for get/set additional data
     31    IEnumerable<KeyValuePair<string, object>> AdditionalData { get; }
    3032    void SetAdditionalData(string identifier, object o);
    3133    object GetAdditionalData(string identifier);
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs

    r17699 r17709  
    8686        scope.Variables.Add(new Variable(EvaluationResultName, solutionContext.EvaluationResult));
    8787      } else variable2.Value = solutionContext.EvaluationResult;
     88      if (solutionContext.EvaluationResult != null) {
     89        foreach (var item in solutionContext.EvaluationResult.AdditionalData) {
     90          if (item.Value is IItem i) {
     91            if (!scope.Variables.TryGetValue(item.Key, out var variable))
     92              scope.Variables.Add(new Variable(item.Key, i));
     93            else variable.Value = i;
     94          }
     95        }
     96      }
    8897    }
    8998  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs

    r17699 r17709  
    6969      ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext);
    7070      QualityParameter.ActualValue = new DoubleValue(solutionContext.EvaluationResult.Quality);
    71 
    7271      return base.InstrumentedApply();
    7372    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting.Views/3.4/HeuristicLab.Problems.VehicleRouting.Views-3.4.csproj

    r16723 r17709  
    119119  </ItemGroup>
    120120  <ItemGroup>
     121    <Compile Include="CVRPPDTWEvaluationView.cs">
     122      <SubType>UserControl</SubType>
     123    </Compile>
     124    <Compile Include="CVRPPDTWEvaluationView.Designer.cs">
     125      <DependentUpon>CVRPPDTWEvaluationView.cs</DependentUpon>
     126    </Compile>
     127    <Compile Include="CVRPTWEvaluationView.cs">
     128      <SubType>UserControl</SubType>
     129    </Compile>
     130    <Compile Include="CVRPTWEvaluationView.Designer.cs">
     131      <DependentUpon>CVRPTWEvaluationView.cs</DependentUpon>
     132    </Compile>
     133    <Compile Include="CVRPEvaluationView.cs">
     134      <SubType>UserControl</SubType>
     135    </Compile>
     136    <Compile Include="CVRPEvaluationView.Designer.cs">
     137      <DependentUpon>CVRPEvaluationView.cs</DependentUpon>
     138    </Compile>
    121139    <Compile Include="Properties\AssemblyInfo.cs" />
    122140    <Compile Include="MDCVRPPDTWView.cs">
     
    156174    <Compile Include="SingleDepotVRPView.Designer.cs">
    157175      <DependentUpon>SingleDepotVRPView.cs</DependentUpon>
     176    </Compile>
     177    <Compile Include="VRPEvaluationView.cs">
     178      <SubType>UserControl</SubType>
     179    </Compile>
     180    <Compile Include="VRPEvaluationView.Designer.cs">
     181      <DependentUpon>VRPEvaluationView.cs</DependentUpon>
    158182    </Compile>
    159183    <Compile Include="VRPImportDialog.cs">
     
    297321      <Private>False</Private>
    298322    </Reference>
     323  </ItemGroup>
     324  <ItemGroup>
     325    <EmbeddedResource Include="CVRPEvaluationView.resx">
     326      <DependentUpon>CVRPEvaluationView.cs</DependentUpon>
     327    </EmbeddedResource>
     328    <EmbeddedResource Include="CVRPPDTWEvaluationView.resx">
     329      <DependentUpon>CVRPPDTWEvaluationView.cs</DependentUpon>
     330    </EmbeddedResource>
     331    <EmbeddedResource Include="CVRPTWEvaluationView.resx">
     332      <DependentUpon>CVRPTWEvaluationView.cs</DependentUpon>
     333    </EmbeddedResource>
     334    <EmbeddedResource Include="VRPEvaluationView.resx">
     335      <DependentUpon>VRPEvaluationView.cs</DependentUpon>
     336    </EmbeddedResource>
    299337  </ItemGroup>
    300338  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/BestVRPSolutionAnalyzer.cs

    r17698 r17709  
    2121
    2222using System.Linq;
     23using HEAL.Attic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HeuristicLab.Data;
    2626using HeuristicLab.Operators;
    2727using HeuristicLab.Optimization;
    2828using HeuristicLab.Parameters;
    29 using HEAL.Attic;
    30 using HeuristicLab.Problems.VehicleRouting.Interfaces;
    3129using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
    32 using HeuristicLab.Problems.VehicleRouting.Variants;
    3330
    3431namespace HeuristicLab.Problems.VehicleRouting {
     
    3734  /// </summary>
    3835  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
    39   [StorableType("42DFB4D7-0DDA-424A-9DB3-1EDAB917A26B")]
    40   public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, IGeneralVRPOperator, ISingleObjectiveOperator {
    41     public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
    42       get { return (ILookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
    43     }
    44     public ScopeTreeLookupParameter<IVRPEncodedSolution> VRPToursParameter {
    45       get { return (ScopeTreeLookupParameter<IVRPEncodedSolution>)Parameters["VRPTours"]; }
    46     }
     36  [StorableType("3e1bb409-0b8f-4324-826c-2190aa5fb2b6")]
     37  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
    4738
    48     public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
    49       get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
    50     }
    51     public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
    52       get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
    53     }
    54     public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
    55       get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
    56     }
     39    [Storable] private IScopeTreeLookupParameter<VRPEvaluation> evaluationParameter;
     40    public IScopeTreeLookupParameter<VRPEvaluation> EvaluationParameter => evaluationParameter;
    5741
    58     public LookupParameter<VRPSolution> BestSolutionParameter {
    59       get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
    60     }
    61     public LookupParameter<VRPSolution> BestValidSolutionParameter {
    62       get { return (LookupParameter<VRPSolution>)Parameters["BestValidSolution"]; }
    63     }
    64     public ValueLookupParameter<ResultCollection> ResultsParameter {
    65       get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
    66     }
     42    [Storable] private ILookupParameter<VRPSolution> bestSolutionParameter;
     43    public ILookupParameter<VRPSolution> BestSolutionParameter => bestSolutionParameter;
    6744
    68     public LookupParameter<DoubleValue> BestKnownQualityParameter {
    69       get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    70     }
    71     public LookupParameter<VRPSolution> BestKnownSolutionParameter {
    72       get { return (LookupParameter<VRPSolution>)Parameters["BestKnownSolution"]; }
    73     }
     45    [Storable] private IResultParameter<VRPEvaluation> bestEvaluationParameter;
     46    public IResultParameter<VRPEvaluation> BestSolutionEvaluationParameter => bestEvaluationParameter;
    7447
    7548    public bool EnabledByDefault {
     
    7952    [StorableConstructor]
    8053    private BestVRPSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
    81 
     54    private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
     55      : base(original, cloner) {
     56      evaluationParameter = cloner.Clone(original.evaluationParameter);
     57      bestSolutionParameter = cloner.Clone(original.bestSolutionParameter);
     58      bestEvaluationParameter = cloner.Clone(original.bestEvaluationParameter);
     59    }
    8260    public BestVRPSolutionAnalyzer()
    8361      : base() {
    84       Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
    85       Parameters.Add(new ScopeTreeLookupParameter<IVRPEncodedSolution>("VRPTours", "The VRP tours which should be evaluated."));
    86 
    87       Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
    88       Parameters.Add(new LookupParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
    89 
    90       Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
    91       Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
    92       Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
    93 
    94       Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
    95       Parameters.Add(new LookupParameter<VRPSolution>("BestValidSolution", "The best valid VRP solution."));
    96       Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
     62      Parameters.Add(evaluationParameter = new ScopeTreeLookupParameter<VRPEvaluation>("EvaluationResult", "The qualities of the VRP solutions which should be analyzed."));
     63      Parameters.Add(bestSolutionParameter = new LookupParameter<VRPSolution>("BestSolution", "The best-so-far solution."));
     64      Parameters.Add(bestEvaluationParameter = new ResultParameter<VRPEvaluation>("Best VRP Evaluation", "The best VRP evaluation.", "Results"));
    9765    }
    9866
     
    10169    }
    10270
    103     private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
    104       : base(original, cloner) {
    105     }
     71    public override IOperation Apply() {
     72      var evaluations = EvaluationParameter.ActualValue;
    10673
    107     [StorableHook(HookType.AfterDeserialization)]
    108     private void AfterDeserialization() {
    109       #region Backwards Compatibility
    110       if (!Parameters.ContainsKey("BestKnownQuality")) {
    111         Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
    112       }
    113       if (!Parameters.ContainsKey("BestKnownSolution")) {
    114         Parameters.Add(new LookupParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
    115       }
    116       #endregion
    117     }
     74      int i = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First().index;
    11875
    119     public override IOperation Apply() {
    120       IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
    121       ItemArray<IVRPEncodedSolution> solutions = VRPToursParameter.ActualValue;
    122       ResultCollection results = ResultsParameter.ActualValue;
    123 
    124       ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
    125       ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
    126       ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
    127 
    128       int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
    129       IVRPEncodedSolution best = solutions[i].Clone() as IVRPEncodedSolution;
    130       VRPSolution solution = BestSolutionParameter.ActualValue;
    131       if (solution == null) {
    132         solution = new VRPSolution(problemInstance, best.Clone() as IVRPEncodedSolution, new DoubleValue(qualities[i].Value));
    133         BestSolutionParameter.ActualValue = solution;
    134         results.Add(new Result("Best VRP Solution", solution));
    135 
    136         results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
    137         results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
    138       } else {
    139         VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
    140         if (qualities[i].Value <= eval.Quality) {
    141           solution.ProblemInstance = problemInstance;
    142           solution.Solution = best.Clone() as IVRPEncodedSolution;
    143           solution.Quality.Value = qualities[i].Value;
    144           (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
    145           (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
    146         }
    147       }
    148 
    149       var idx = qualities.Select((x, index) => new { index, x.Value }).Where(index => problemInstance.Feasible(solutions[index.index])).OrderBy(x => x.Value).FirstOrDefault();
    150       if (idx != null) {
    151         int j = idx.index;
    152         IVRPEncodedSolution bestFeasible = solutions[j].Clone() as IVRPEncodedSolution;
    153         VRPSolution validSolution = BestValidSolutionParameter.ActualValue;
    154         if (validSolution == null) {
    155           validSolution = new VRPSolution(problemInstance, best.Clone() as IVRPEncodedSolution, new DoubleValue(qualities[j].Value));
    156           BestValidSolutionParameter.ActualValue = validSolution;
    157           if (results.ContainsKey("Best valid VRP Solution"))
    158             results["Best valid VRP Solution"].Value = validSolution;
    159           else
    160             results.Add(new Result("Best valid VRP Solution", validSolution));
    161 
    162           results.Add(new Result("Best valid VRP Solution Distance", new DoubleValue(distances[j].Value)));
    163           results.Add(new Result("Best valid VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[j].Value)));
    164         } else {
    165           if (qualities[j].Value <= validSolution.Quality.Value) {
    166             if (ProblemInstanceParameter.ActualValue.Feasible(best)) {
    167               validSolution.ProblemInstance = problemInstance;
    168               validSolution.Solution = best.Clone() as IVRPEncodedSolution;
    169               validSolution.Quality.Value = qualities[j].Value;
    170               (results["Best valid VRP Solution Distance"].Value as DoubleValue).Value = distances[j].Value;
    171               (results["Best valid VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[j].Value;
    172             }
    173           }
    174         }
    175 
    176         DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
    177         if (bestKnownQuality == null || qualities[j].Value < bestKnownQuality.Value) {
    178           BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[j].Value);
    179           BestKnownSolutionParameter.ActualValue = (VRPSolution)validSolution.Clone();
    180         }
     76      var bestEvaluation = BestSolutionEvaluationParameter.ActualValue;
     77     
     78      var bestSolution = BestSolutionParameter.ActualValue;
     79      if (bestSolution == null || evaluations[i].Quality <= bestSolution.Quality.Value) {
     80        BestSolutionEvaluationParameter.ActualValue = (VRPEvaluation)evaluations[i].Clone();
    18181      }
    18282
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/HeuristicLab.Problems.VehicleRouting-3.4.csproj

    r17708 r17709  
    128128    <Compile Include="Analyzer\BestAverageWorstTours\TimeWindowed\BestTimeWindowedVRPToursMemorizer.cs" />
    129129    <Compile Include="Analyzer\BestSolution\BestVRPSolutionAnalyzer.cs" />
    130     <Compile Include="Analyzer\BestSolution\Capacitated\BestCapacitatedVRPSolutionAnalyzer.cs" />
    131     <Compile Include="Analyzer\BestSolution\PickupAndDelivery\BestPickupAndDeliveryVRPSolutionAnalyzer.cs" />
    132     <Compile Include="Analyzer\BestSolution\TimeWindowed\BestTimeWindowedVRPSolutionAnalyzer.cs" />
    133130    <Compile Include="Analyzer\ConstraintRelaxation\Capacitated\CapacityRelaxationVRPAnalyzer.cs" />
    134131    <Compile Include="Analyzer\ConstraintRelaxation\PickupAndDelivery\PickupViolationsRelaxationVRPAnalyzer.cs" />
     
    490487    </Reference>
    491488  </ItemGroup>
     489  <ItemGroup />
    492490  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    493491  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPEvaluation.cs

    r17708 r17709  
    124124  [Item("VRPEvaluation", "")]
    125125  [StorableType("0c4dfa78-8e41-4558-b2dd-4a22954c35ba")]
    126   public class VRPEvaluation : EvaluationResult {
     126  public class VRPEvaluation : EvaluationResult, ISingleObjectiveEvaluationResult {
     127    // TODO: Would be nice to collect these, into individual results in a run
    127128    [Storable] public double Quality { get; set; }
    128129    [Storable] public double Distance { get; set; }
     
    130131    [Storable] public InsertionInfo InsertionInfo { get; set; }
    131132    [Storable] public double Penalty { get; set; }
    132 
     133    [Storable] public bool IsFeasible { get; set; }
    133134
    134135    [StorableConstructor]
     
    141142      InsertionInfo = cloner.Clone(original.InsertionInfo);
    142143      Penalty = original.Penalty;
     144      IsFeasible = original.IsFeasible;
    143145    }
    144146    public VRPEvaluation() : base() {
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPEvaluator.cs

    r17698 r17709  
    2020#endregion
    2121
     22using HEAL.Attic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Data;
    2526using HeuristicLab.Parameters;
    26 using HEAL.Attic;
    2727using HeuristicLab.Problems.VehicleRouting.Encodings;
    2828using HeuristicLab.Problems.VehicleRouting.Interfaces;
     
    124124        EvaluateTour(evaluation, instance, tour, solution);
    125125      }
    126 
     126      evaluation.IsFeasible = Feasible(evaluation);
    127127      return evaluation;
    128128    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs

    r17706 r17709  
    9797
    9898    public override ISingleObjectiveEvaluationResult Evaluate(IVRPEncodedSolution solution, IRandom random, CancellationToken cancellationToken) {
    99       return new SingleObjectiveEvaluationResult(ProblemInstance.Evaluate(solution).Quality);
     99      return ProblemInstance.Evaluate(solution);
    100100    }
    101101
     
    150150
    151151    void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
    152       //InitializeOperators();
    153       AttachProblemInstanceEventHandlers();
    154 
    155       //OnOperatorsChanged();
     152      InitializeOperators();
     153      AttachProblemInstanceEventHandlers();
     154
     155      OnOperatorsChanged();
    156156    }
    157157
     
    169169      Operators.Add(new QualitySimilarityCalculator());
    170170      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
     171      Operators.AddRange(ProblemInstance.Operators.OfType<IAnalyzer>());
    171172    }
    172173
Note: See TracChangeset for help on using the changeset viewer.