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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
31 |
|
---|
32 | namespace 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 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 | [Storable]
|
---|
56 | public TtpUtils.DistanceType DistanceType { get; set; }
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected TourProfitProblem(bool deserializing) : base(deserializing) { }
|
---|
60 | protected TourProfitProblem(TourProfitProblem original, Cloner cloner) : base(original, cloner) {
|
---|
61 | Tsp = cloner.Clone(original.Tsp);
|
---|
62 | Ksp = cloner.Clone(original.Ksp);
|
---|
63 | FixedKspSolution = cloner.Clone(original.FixedKspSolution);
|
---|
64 | Availability = original.Availability != null ? (int[])original.Availability.Clone() : null;
|
---|
65 | RentingRatio = original.RentingRatio;
|
---|
66 | MinSpeed = original.MinSpeed;
|
---|
67 | MaxSpeed = original.MaxSpeed;
|
---|
68 | DistanceType = original.DistanceType;
|
---|
69 | }
|
---|
70 | public TourProfitProblem() : base() {
|
---|
71 | Encoding.Length = 5;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
75 | return new TourProfitProblem(this, cloner);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
79 | return TtpUtils.Evaluate(Tsp, individual.Permutation().ToArray(),
|
---|
80 | Ksp, FixedKspSolution.ToArray(),
|
---|
81 | Availability, RentingRatio, MinSpeed, MaxSpeed, DistanceType);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
85 | while (true) {
|
---|
86 | var neighbor = individual.Copy();
|
---|
87 | switch (random.Next(7)) {
|
---|
88 | case 0: InsertionManipulator.Apply(random, neighbor.Permutation()); break;
|
---|
89 | case 1: InversionManipulator.Apply(random, neighbor.Permutation()); break;
|
---|
90 | case 2: ScrambleManipulator.Apply(random, neighbor.Permutation()); break;
|
---|
91 | case 3: Swap2Manipulator.Apply(random, neighbor.Permutation()); break;
|
---|
92 | case 4: Swap3Manipulator.Apply(random, neighbor.Permutation()); break;
|
---|
93 | case 5: TranslocationInversionManipulator.Apply(random, neighbor.Permutation()); break;
|
---|
94 | case 6: TranslocationManipulator.Apply(random, neighbor.Permutation()); break;
|
---|
95 | }
|
---|
96 | yield return neighbor;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
101 | base.Analyze(individuals, qualities, results, random);
|
---|
102 | var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
|
---|
103 | var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
|
---|
104 |
|
---|
105 | if (!results.ContainsKey("Best TSP Solution"))
|
---|
106 | results.Add(new Result("Best TSP Solution", typeof(Permutation)));
|
---|
107 | results["Best TSP Solution"].Value = (IItem)best.Permutation().Clone();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|