Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode2.cs @ 14783

Last change on this file since 14783 was 14653, checked in by jkarder, 8 years ago

#2205: worked on optimization networks

  • improved ttp evaluation
File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Networks;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.Knapsack;
33using HeuristicLab.Problems.TravelingSalesman;
34
35namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
36  [Item("TtpOrchestratorNode2", "Orchestrator for TTP optimization network version 2.")]
37  [StorableClass]
38  public sealed class TtpOrchestratorNode2 : TtpOrchestratorNode {
39    [StorableConstructor]
40    private TtpOrchestratorNode2(bool deserializing) : base(deserializing) { }
41    private TtpOrchestratorNode2(TtpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) { }
42    public TtpOrchestratorNode2() : this("TtpOrchestratorNode2") { }
43    public TtpOrchestratorNode2(string name) : base(name) {
44      MetaSolverOrchestrationPort = CreateOrchestrationPort<MaximizationVariegationProblem<RealVectorEncoding>>(MetaSolverName + OrchestrationPortNameSuffix);
45      MetaSolverEvaluationPort = CreateEvaluationPort<RealVector>(MetaSolverName + EvaluationPortNameSuffix, "RealVector", "Quality");
46      TspSolverOrchestrationPort = CreateOrchestrationPort<TravelingSalesmanProblem>(TspSolverName + OrchestrationPortNameSuffix);
47      KspSolverOrchestrationPort = CreateOrchestrationPort<LootProfitProblem>(KspSolverName + OrchestrationPortNameSuffix);
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new TtpOrchestratorNode2(this, cloner);
52    }
53
54    public override void Prepare(bool clearRuns = false) {
55      base.Prepare(clearRuns);
56
57      var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
58      var msgFlags = OrchestrationMessage.Prepare | OrchestrationMessage.SetEvalHook;
59      if (clearRuns) msgFlags |= OrchestrationMessage.ClearRuns;
60      metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(msgFlags);
61      var problem = new MaximizationVariegationProblem<RealVectorEncoding>();
62      problem.Encoding.Length = TspParameter.Value.Coordinates.Rows * 2;
63      problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
64      metaMsg["Problem"] = problem;
65      MetaSolverOrchestrationPort.SendMessage(metaMsg);
66    }
67
68    #region MetaSolver Message Handling
69    protected override void MetaSolverEvaluationPortMessage(IMessage message) {
70      var factors = (RealVector)message["RealVector"];
71
72      var tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone();
73      for (int i = 0; i < tsp.Coordinates.Rows; i++) {
74        tsp.Coordinates[i, 0] = (int)Math.Ceiling(tsp.Coordinates[i, 0] * factors[i * 2]);
75        tsp.Coordinates[i, 1] = (int)Math.Ceiling(tsp.Coordinates[i, 1] * factors[i * 2 + 1]);
76      }
77      tsp.DistanceMatrix = new DistanceMatrix(TtpUtils.GetDistances(tsp.Coordinates, distanceType));
78
79      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
80      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
81      tspMsg["Problem"] = tsp;
82      TspSolverOrchestrationPort.SendMessage(tspMsg);
83      cts.Token.ThrowIfCancellationRequested();
84
85      var tspResults = (ResultCollection)tspMsg["Results"];
86      var bestTspSolution = (PathTSPTour)tspResults["Best TSP Solution"].Value.Clone();
87      var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
88      var tour = new PathTSPTour(coordinates, bestTspSolution.Permutation, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution.Permutation)));
89
90      var availability = TtpUtils.GetAvailability(AvailabilityParameter.Value.ToArray());
91
92      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
93      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
94      var lpp = new LootProfitProblem {
95        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
96        Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(),
97        FixedTspSolution = bestTspSolution.Permutation,
98        Availability = availability,
99        RentingRatio = RentingRatioParameter.Value.Value,
100        MinSpeed = MinSpeedParameter.Value.Value,
101        MaxSpeed = MaxSpeedParameter.Value.Value,
102      };
103      lpp.Encoding.Length = KspParameter.Value.Length;
104      kspMsg["Problem"] = lpp;
105      KspSolverOrchestrationPort.SendMessage(kspMsg);
106      cts.Token.ThrowIfCancellationRequested();
107
108      var kspResults = (ResultCollection)kspMsg["Results"];
109      var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone();
110      var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone();
111      var kspPenalty = new DoubleValue(0.0);
112      var kspWeights = (IntArray)KspParameter.Value.Weights.Clone();
113      var kspValues = (IntArray)KspParameter.Value.Values.Clone();
114      var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality;
115      var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues);
116
117      #region Analyze
118      double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
119        availability, RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value);
120      ((DoubleValue)message["Quality"]).Value = objectiveValue;
121
122      IResult bestQuality;
123      if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
124        Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
125        Results.Add(new Result("Best Tour", tour));
126        Results.Add(new Result("Best Loot", loot));
127      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
128        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
129        Results["Best Tour"].Value = tour;
130        Results["Best Loot"].Value = loot;
131      }
132      #endregion
133    }
134    #endregion
135  }
136}
Note: See TracBrowser for help on using the repository browser.