Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: worked on optimization networks

  • added variegation problem for minimization and maximization
  • refactored some classes
File size: 7.0 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
78      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
79      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
80      tspMsg["Problem"] = tsp;
81      TspSolverOrchestrationPort.SendMessage(tspMsg);
82      cts.Token.ThrowIfCancellationRequested();
83
84      var tspResults = (ResultCollection)tspMsg["Results"];
85      var bestTspSolution = (PathTSPTour)tspResults["Best TSP Solution"].Value.Clone();
86      var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
87      var tour = new PathTSPTour(coordinates, bestTspSolution.Permutation, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution.Permutation)));
88
89      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
90      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
91      var lpp = new LootProfitProblem {
92        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
93        Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(),
94        FixedTspSolution = bestTspSolution.Permutation,
95        Availability = AvailabilityParameter.Value.ToArray(),
96        RentingRatio = RentingRatioParameter.Value.Value,
97        MinSpeed = MinSpeedParameter.Value.Value,
98        MaxSpeed = MaxSpeedParameter.Value.Value,
99        DistanceType = distanceType
100      };
101      lpp.Encoding.Length = KspParameter.Value.Length;
102      kspMsg["Problem"] = lpp;
103      KspSolverOrchestrationPort.SendMessage(kspMsg);
104      cts.Token.ThrowIfCancellationRequested();
105
106      var kspResults = (ResultCollection)kspMsg["Results"];
107      var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone();
108      var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone();
109      var kspPenalty = new DoubleValue(0.0);
110      var kspWeights = (IntArray)KspParameter.Value.Weights.Clone();
111      var kspValues = (IntArray)KspParameter.Value.Values.Clone();
112      var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality;
113      var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues);
114
115      #region Analyze
116      double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
117        AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);
118      ((DoubleValue)message["Quality"]).Value = objectiveValue;
119
120      IResult bestQuality;
121      if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
122        Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
123        Results.Add(new Result("Best Tour", tour));
124        Results.Add(new Result("Best Loot", loot));
125      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
126        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
127        Results["Best Tour"].Value = tour;
128        Results["Best Loot"].Value = loot;
129      }
130      #endregion
131    }
132    #endregion
133  }
134}
Note: See TracBrowser for help on using the repository browser.