Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/OrOptManipulator.cs @ 8694

Last change on this file since 8694 was 7159, checked in by epitzer, 13 years ago

#1703 correct some typos

File size: 2.9 KB
RevLine 
[7145]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
29using HeuristicLab.Problems.VehicleRouting.Encodings;
30using HeuristicLab.Problems.VehicleRouting;
31using System;
32
[7159]33namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
34  [Item("OrOptManipulator", "Or opt manipulation")]
[7145]35  [StorableClass]
[7159]36  public sealed class OrOptManipulator : PotvinManipulator {
[7145]37    [StorableConstructor]
[7159]38    private OrOptManipulator(bool deserializing) : base(deserializing) { }
39    private OrOptManipulator(OrOptManipulator original, Cloner cloner)
[7145]40      : base(original, cloner) {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
[7159]43      return new OrOptManipulator(this, cloner);
[7145]44    }
[7159]45    public OrOptManipulator()
[7145]46      : base() {
47    }
48
49    public static void Apply(IRandom random, PotvinEncoding individual) {
50      List<Tour> tours = individual.Tours.FindAll(t => t.Cities.Count >= 2);
51
52      if (tours.Count > 0) {
53        int tourIdx = random.Next(tours.Count);
54        Tour tour = tours[tourIdx];
55
56        int segmentStart = random.Next(tour.Cities.Count);
57        int segmentLength;
58        if (segmentStart == 0) {
59          segmentLength = 1 + random.Next(tour.Cities.Count - 1);
60        } else {
61          segmentLength = 1 + random.Next(tour.Cities.Count - segmentStart);
62        }
63
64        List<int> segment = tour.Cities.GetRange(segmentStart, segmentLength);
65        tour.Cities.RemoveRange(segmentStart, segmentLength);
66        int newPos;
67        if(tour.Cities.Count == 1)
68          newPos = 0;
69        else
70          newPos = random.Next(tour.Cities.Count - 1);
71
72        if (newPos >= segmentStart)
73          newPos++;
74        tour.Cities.InsertRange(newPos, segment);
75      }
76    }
77
78    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
79      Apply(random, individual);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.