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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Random;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
34 |
|
---|
35 | [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.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class DemandEquivalentSwapEquipmentManipluator : GQAPManipulator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator {
|
---|
38 |
|
---|
39 | public ILookupParameter<DoubleArray> DemandsParameter {
|
---|
40 | get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
|
---|
41 | }
|
---|
42 | public ILookupParameter<DoubleArray> CapacitiesParameter {
|
---|
43 | get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | protected DemandEquivalentSwapEquipmentManipluator(bool deserializing) : base(deserializing) { }
|
---|
48 | protected DemandEquivalentSwapEquipmentManipluator(DemandEquivalentSwapEquipmentManipluator original, Cloner cloner) : base(original, cloner) { }
|
---|
49 | public DemandEquivalentSwapEquipmentManipluator()
|
---|
50 | : base() {
|
---|
51 | Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
|
---|
52 | Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new DemandEquivalentSwapEquipmentManipluator(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static void Apply(IRandom random, IntegerVector assignment, DoubleArray capacities, DoubleArray demands) {
|
---|
60 | if (assignment.Length < 2) throw new InvalidOperationException("Vector is too small for swap operation.");
|
---|
61 | int equipment1 = random.Next(assignment.Length), equipment2 = random.Next(assignment.Length);
|
---|
62 | int counter = 1;
|
---|
63 | while (assignment[equipment1] == assignment[equipment2] && counter < assignment.Length) {
|
---|
64 | equipment2 = random.Next(assignment.Length);
|
---|
65 | counter++;
|
---|
66 | }
|
---|
67 | if (assignment[equipment1] != assignment[equipment2]) {
|
---|
68 | var groupedLocations = assignment
|
---|
69 | .Select((value, index) => new { Index = index, Value = value })
|
---|
70 | .GroupBy(x => x.Value)
|
---|
71 | .ToDictionary(x => x.Key, y => new HashSet<int>(y.Select(x => x.Index)));
|
---|
72 | double demand = demands[equipment1];
|
---|
73 | int location1 = assignment[equipment1];
|
---|
74 | int location2 = assignment[equipment2];
|
---|
75 |
|
---|
76 | assignment[equipment1] = location2;
|
---|
77 | while (demand > 0) {
|
---|
78 | demand -= demands[equipment2];
|
---|
79 | assignment[equipment2] = location1;
|
---|
80 | groupedLocations[location2].Remove(equipment2);
|
---|
81 | if (!groupedLocations[location2].Any()) break;
|
---|
82 | equipment2 = groupedLocations[location2].SampleRandom(random);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected override void Manipulate(IRandom random, IntegerVector vector) {
|
---|
88 | Apply(random, vector, CapacitiesParameter.ActualValue, DemandsParameter.ActualValue);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|