Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14574 was 9836, checked in by epitzer, 11 years ago

Fix VRP FLA manipulators and adapt to VRP-3.4 (#1703)

File size: 3.6 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;
[9836]32using HeuristicLab.Problems.VehicleRouting.Interfaces;
[7145]33
[7159]34namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
35  [Item("OrOptManipulator", "Or opt manipulation")]
[7145]36  [StorableClass]
[7159]37  public sealed class OrOptManipulator : PotvinManipulator {
[7145]38    [StorableConstructor]
[7159]39    private OrOptManipulator(bool deserializing) : base(deserializing) { }
40    private OrOptManipulator(OrOptManipulator original, Cloner cloner)
[7145]41      : base(original, cloner) {
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
[7159]44      return new OrOptManipulator(this, cloner);
[7145]45    }
[7159]46    public OrOptManipulator()
[7145]47      : base() {
48    }
49
[9836]50    public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, bool allowInfeasible) {
[9750]51      List<Tour> tours = individual.Tours.FindAll(t => t.Stops.Count >= 2);
[9836]52      if (tours.Count == 0) return;
53      bool feasible;
54      int count = 0;
[7145]55
[9836]56      do {
57        feasible = true;
[7145]58        int tourIdx = random.Next(tours.Count);
59        Tour tour = tours[tourIdx];
60
[9750]61        int segmentStart = random.Next(tour.Stops.Count);
[7145]62        int segmentLength;
63        if (segmentStart == 0) {
[9750]64          segmentLength = 1 + random.Next(tour.Stops.Count - 1);
[9836]65        }
66        else {
[9750]67          segmentLength = 1 + random.Next(tour.Stops.Count - segmentStart);
[7145]68        }
69
[9836]70        bool originalFeasible = instance.TourFeasible(tour, individual);
71
[9750]72        List<int> segment = tour.Stops.GetRange(segmentStart, segmentLength);
73        tour.Stops.RemoveRange(segmentStart, segmentLength);
[7145]74        int newPos;
[9750]75        if (tour.Stops.Count == 1)
[7145]76          newPos = 0;
77        else
[9750]78          newPos = random.Next(tour.Stops.Count - 1);
[7145]79
80        if (newPos >= segmentStart)
81          newPos++;
[9750]82        tour.Stops.InsertRange(newPos, segment);
[9836]83
84        if (!allowInfeasible && originalFeasible) {
85          feasible = instance.TourFeasible(tour, individual);
86          if (!feasible) {
87            tour.Stops.RemoveRange(newPos, segment.Count);
88            tour.Stops.InsertRange(segmentStart, segment);
89          }
90        }
91      } while (!feasible && count++ < 100);
[7145]92    }
93
94    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
[9836]95      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
96      Apply(random, individual, ProblemInstance, allowInfeasible);
[7145]97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.