Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7319 was 7319, checked in by abeham, 12 years ago

#1614: worked on GQAP (added operators, assignment view)

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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, ILocationAwareGQAPOperator, IEquipmentAwareGQAPManipulator {
37   
38    public ILookupParameter<DoubleArray> CapacitiesParameter {
39      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
40    }
41    public ILookupParameter<DoubleArray> DemandsParameter {
42      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
43    }
44
45    [StorableConstructor]
46    protected DemandEquivalentSwapEquipmentManipluator(bool deserializing) : base(deserializing) { }
47    protected DemandEquivalentSwapEquipmentManipluator(DemandEquivalentSwapEquipmentManipluator original, Cloner cloner) : base(original, cloner) { }
48    public DemandEquivalentSwapEquipmentManipluator()
49      : base() {
50      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations."));
51      Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments."));
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new DemandEquivalentSwapEquipmentManipluator(this, cloner);
56    }
57
58    public static void Apply(IRandom random, IntegerVector assignment, DoubleArray capacities, DoubleArray demands) {
59      if (assignment.Length < 2) throw new InvalidOperationException("Vector is too small for swap operation.");
60      int equipment1 = random.Next(assignment.Length), equipment2 = random.Next(assignment.Length);
61      int counter = 1;
62      while (assignment[equipment1] == assignment[equipment2] && counter < assignment.Length) {
63        equipment2 = random.Next(assignment.Length);
64        counter++;
65      }
66      if (assignment[equipment1] != assignment[equipment2]) {
67        var groupedLocations = assignment
68              .Select((value, index) => new { Index = index, Value = value })
69              .GroupBy(x => x.Value)
70              .ToDictionary(x => x.Key, y => new HashSet<int>(y.Select(x => x.Index)));
71        double demand = demands[equipment1];
72        int location1 = assignment[equipment1];
73        int location2 = assignment[equipment2];
74
75        assignment[equipment1] = location2;
76        while (demand > 0) {
77          demand -= demands[equipment2];
78          assignment[equipment2] = location1;
79          groupedLocations[location2].Remove(equipment2);
80          bool selected;
81          equipment2 = groupedLocations[location2].SelectRandomOrDefault(random, out selected);
82          if (!selected) break;
83        }
84      }
85    }
86
87    protected override void Manipulate(IRandom random, IntegerVector vector) {
88      Apply(random, vector, CapacitiesParameter.ActualValue, DemandsParameter.ActualValue);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.