Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: worked on optimization networks

  • added abstract base classes for ttp networks/orchestrators
  • removed ttp networks/orchestrators from HeuristicLab.Networks.IntegratedOptimization
  • runs can now be cleared when preparing OrchestratedAlgorithmNodes
File size: 5.6 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
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new TtpOrchestratorNode2(this, cloner);
47    }
48
49    #region MetaSolver Message Handling
50    protected override void MetaSolverEvaluationPortMessage(IMessage message) {
51      var factors = (RealVector)message["RealVector"];
52
53      var tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone();
54      for (int i = 0; i < tsp.Coordinates.Rows; i++) {
55        tsp.Coordinates[i, 0] = (int)Math.Ceiling(tsp.Coordinates[i, 0] * factors[i * 2]);
56        tsp.Coordinates[i, 1] = (int)Math.Ceiling(tsp.Coordinates[i, 1] * factors[i * 2 + 1]);
57      }
58
59      var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
60      tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
61      tspMsg["Problem"] = tsp;
62      TspSolverOrchestrationPort.SendMessage(tspMsg);
63      cts.Token.ThrowIfCancellationRequested();
64
65      var bestTspSolution = (PathTSPTour)tspResults["Best TSP Solution"].Value.Clone();
66      var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
67      var tour = new PathTSPTour(coordinates, bestTspSolution.Permutation, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution.Permutation)));
68
69      var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
70      kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start);
71      var lpp = new LootProfitProblem {
72        Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
73        Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(),
74        FixedTspSolution = bestTspSolution.Permutation,
75        Availability = AvailabilityParameter.Value.ToArray(),
76        RentingRatio = RentingRatioParameter.Value.Value,
77        MinSpeed = MinSpeedParameter.Value.Value,
78        MaxSpeed = MaxSpeedParameter.Value.Value,
79        DistanceType = distanceType
80      };
81      lpp.Encoding.Length = KspParameter.Value.Length;
82      kspMsg["Problem"] = lpp;
83      KspSolverOrchestrationPort.SendMessage(kspMsg);
84      cts.Token.ThrowIfCancellationRequested();
85
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      #region Analyze
95      double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
96        AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);
97      ((DoubleValue)message["Quality"]).Value = objectiveValue;
98
99      IResult bestQuality;
100      if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
101        Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
102        Results.Add(new Result("Best Tour", tour));
103        Results.Add(new Result("Best Loot", loot));
104      } else if (objectiveValue > ((DoubleValue)bestQuality.Value).Value) {
105        ((DoubleValue)bestQuality.Value).Value = objectiveValue;
106        Results["Best Tour"].Value = tour;
107        Results["Best Loot"].Value = loot;
108      }
109      #endregion
110    }
111    #endregion
112  }
113}
Note: See TracBrowser for help on using the repository browser.