Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode3.cs @ 14629

Last change on this file since 14629 was 14629, checked in by jkarder, 7 years ago

#2205: worked on optimization networks

  • fixed bugs in ttp network/orchestrator 3
File size: 6.7 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("TtpOrchestratorNode3", "Orchestrator for TTP optimization network version 3.")]
37  [StorableClass]
38  public sealed class TtpOrchestratorNode3 : TtpOrchestratorNode {
39    [StorableConstructor]
40    private TtpOrchestratorNode3(bool deserializing) : base(deserializing) { }
41    private TtpOrchestratorNode3(TtpOrchestratorNode3 original, Cloner cloner) : base(original, cloner) { }
42    public TtpOrchestratorNode3() : this("TtpOrchestratorNode3") { }
43    public TtpOrchestratorNode3(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<BinaryKnapsackProblem>(KspSolverName + OrchestrationPortNameSuffix);
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new TtpOrchestratorNode3(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 = KspParameter.Value.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      int fi = 0;
72
73      var ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone();
74      while (fi < ksp.Values.Length) {
75        ksp.Values[fi] = (int)Math.Ceiling(ksp.Values[fi] * factors[fi]);
76        ++fi;
77      }
78
79      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
80      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
81      kspMsg["Problem"] = ksp;
82      KspSolverOrchestrationPort.SendMessage(kspMsg);
83      cts.Token.ThrowIfCancellationRequested();
84
85      var kspResults = (ResultCollection)kspMsg["Results"];
86      var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone();
87      var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone();
88      var kspPenalty = new DoubleValue(0.0);
89      var kspWeights = (IntArray)KspParameter.Value.Weights.Clone();
90      var kspValues = (IntArray)KspParameter.Value.Values.Clone();
91      var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality;
92      var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues);
93
94      var tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone();
95      for (int j = 0; j < tsp.Coordinates.Rows; j++) {
96        tsp.Coordinates[j, 0] = (int)Math.Ceiling(tsp.Coordinates[j, 0] * factors[fi + j * 2]);
97        tsp.Coordinates[j, 1] = (int)Math.Ceiling(tsp.Coordinates[j, 1] * factors[fi + j * 2 + 1]);
98      }
99
100      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
101      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
102      tspMsg["Problem"] = tsp;
103      TspSolverOrchestrationPort.SendMessage(tspMsg);
104      cts.Token.ThrowIfCancellationRequested();
105
106      var tspResults = (ResultCollection)tspMsg["Results"];
107      var bestTspSolution = (PathTSPTour)tspResults["Best TSP Solution"].Value.Clone();
108      var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
109      var tour = new PathTSPTour(coordinates, bestTspSolution.Permutation, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution.Permutation)));
110
111      #region Analyze
112      double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
113        AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);
114      ((DoubleValue)message["Quality"]).Value = objectiveValue;
115
116      IResult bestQuality;
117      if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
118        Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
119        Results.Add(new Result("Best Tour", tour));
120        Results.Add(new Result("Best Loot", loot));
121      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
122        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
123        Results["Best Tour"].Value = tour;
124        Results["Best Loot"].Value = loot;
125      }
126      #endregion
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.