Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/Manipulators/DemandEquivalentSwapEquipmentManipluator.cs @ 15504

Last change on this file since 15504 was 15504, checked in by abeham, 6 years ago

#1614: refactored code

  • change problem to derive from basic problem
  • using a combined instance class instead of individual parameters
File size: 3.6 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
33
34  [Item("DemandEquivalentSwapEquipmentManipluator", "Swaps equipment X from location A with as much equipments from location B that the demand of X is less than or equal to the demand of the swapped equipments in B.")]
35  [StorableClass]
36  public class DemandEquivalentSwapEquipmentManipluator : GQAPManipulator {
37
38    [StorableConstructor]
39    protected DemandEquivalentSwapEquipmentManipluator(bool deserializing) : base(deserializing) { }
40    protected DemandEquivalentSwapEquipmentManipluator(DemandEquivalentSwapEquipmentManipluator original, Cloner cloner) : base(original, cloner) { }
41    public DemandEquivalentSwapEquipmentManipluator()
42      : base() {
43    }
44
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new DemandEquivalentSwapEquipmentManipluator(this, cloner);
47    }
48
49    public static void Apply(IRandom random, IntegerVector assignment, DoubleArray capacities, DoubleArray demands) {
50      if (assignment.Length < 2) throw new InvalidOperationException("Vector is too small for swap operation.");
51      int equipment1 = random.Next(assignment.Length), equipment2 = random.Next(assignment.Length);
52      int counter = 1;
53      while (assignment[equipment1] == assignment[equipment2] && counter < assignment.Length) {
54        equipment2 = random.Next(assignment.Length);
55        counter++;
56      }
57      if (assignment[equipment1] != assignment[equipment2]) {
58        var groupedLocations = assignment
59              .Select((value, index) => new { Index = index, Value = value })
60              .GroupBy(x => x.Value)
61              .ToDictionary(x => x.Key, y => new HashSet<int>(y.Select(x => x.Index)));
62        double demand = demands[equipment1];
63        int location1 = assignment[equipment1];
64        int location2 = assignment[equipment2];
65
66        assignment[equipment1] = location2;
67        while (demand > 0) {
68          demand -= demands[equipment2];
69          assignment[equipment2] = location1;
70          groupedLocations[location2].Remove(equipment2);
71          if (!groupedLocations[location2].Any()) break;
72          equipment2 = groupedLocations[location2].SampleRandom(random);
73        }
74      }
75    }
76
77    protected override void Manipulate(IRandom random, IntegerVector vector, GQAPInstance problemInstance) {
78      Apply(random, vector, problemInstance.Capacities, problemInstance.Demands);
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.