Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Problems/TourProfitProblem.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: 4.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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.BinaryVectorEncoding;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.TravelingSalesman;
31
32namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
33  [Item("Tour Profit Problem", "Represents a problem whose objective is to optimize a TTP tour for fixed TTP loot.")]
34  [Creatable(CreatableAttribute.Categories.Problems, Priority = 999)]
35  [StorableClass]
36  public class TourProfitProblem : SingleObjectiveBasicProblem<PermutationEncoding> {
37    public override bool Maximization {
38      get { return true; }
39    }
40
41    [Storable]
42    public TravelingSalesmanProblem Tsp { get; set; }
43    [Storable]
44    public BinaryKnapsackProblem Ksp { get; set; }
45    [Storable]
46    public BinaryVector FixedKspSolution { get; set; }
47    [Storable]
48    public Dictionary<int, int[]> Availability { get; set; }
49    [Storable]
50    public double RentingRatio { get; set; }
51    [Storable]
52    public double MinSpeed { get; set; }
53    [Storable]
54    public double MaxSpeed { get; set; }
55
56    [StorableConstructor]
57    protected TourProfitProblem(bool deserializing) : base(deserializing) { }
58    protected TourProfitProblem(TourProfitProblem original, Cloner cloner) : base(original, cloner) {
59      Tsp = cloner.Clone(original.Tsp);
60      Ksp = cloner.Clone(original.Ksp);
61      FixedKspSolution = cloner.Clone(original.FixedKspSolution);
62      Availability = original.Availability != null ? original.Availability.ToDictionary(k => k.Key, v => (int[])v.Value.Clone()) : null;
63      RentingRatio = original.RentingRatio;
64      MinSpeed = original.MinSpeed;
65      MaxSpeed = original.MaxSpeed;
66    }
67    public TourProfitProblem() : base() {
68      Encoding.Length = 5;
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new TourProfitProblem(this, cloner);
73    }
74
75    public override double Evaluate(Individual individual, IRandom random) {
76      return TtpUtils.Evaluate(Tsp, individual.Permutation().ToArray(),
77                               Ksp, FixedKspSolution.ToArray(),
78                               Availability, RentingRatio, MinSpeed, MaxSpeed);
79    }
80
81    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
82      while (true) {
83        var neighbor = individual.Copy();
84        switch (random.Next(7)) {
85          case 0: InsertionManipulator.Apply(random, neighbor.Permutation()); break;
86          case 1: InversionManipulator.Apply(random, neighbor.Permutation()); break;
87          case 2: ScrambleManipulator.Apply(random, neighbor.Permutation()); break;
88          case 3: Swap2Manipulator.Apply(random, neighbor.Permutation()); break;
89          case 4: Swap3Manipulator.Apply(random, neighbor.Permutation()); break;
90          case 5: TranslocationInversionManipulator.Apply(random, neighbor.Permutation()); break;
91          case 6: TranslocationManipulator.Apply(random, neighbor.Permutation()); break;
92        }
93        yield return neighbor;
94      }
95    }
96
97    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
98      base.Analyze(individuals, qualities, results, random);
99      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
100      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
101
102      if (!results.ContainsKey("Best TSP Solution"))
103        results.Add(new Result("Best TSP Solution", typeof(Permutation)));
104      results["Best TSP Solution"].Value = (IItem)best.Permutation().Clone();
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.