Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/ExchangeManipulator.cs @ 11301

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

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

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