1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 System.Threading;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Random;
|
---|
33 | using HEAL.Attic;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
36 | [Item("SlackMinimizationSolutionCreator", "A heuristic that creates a solution to the Generalized Quadratic Assignment Problem by minimizing the amount of slack.")]
|
---|
37 | [StorableType("02676A57-A686-4FDB-B912-B3792C749669")]
|
---|
38 | public class SlackMinimizationSolutionCreator : GQAPStochasticSolutionCreator {
|
---|
39 |
|
---|
40 | public IValueLookupParameter<IntValue> MaximumTriesParameter {
|
---|
41 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTries"]; }
|
---|
42 | }
|
---|
43 | public IValueLookupParameter<BoolValue> CreateMostFeasibleSolutionParameter {
|
---|
44 | get { return (IValueLookupParameter<BoolValue>)Parameters["CreateMostFeasibleSolution"]; }
|
---|
45 | }
|
---|
46 | public IValueLookupParameter<IntValue> DepthParameter {
|
---|
47 | get { return (IValueLookupParameter<IntValue>)Parameters["Depth"]; }
|
---|
48 | }
|
---|
49 | public IValueLookupParameter<IntValue> RandomWalkLengthParameter {
|
---|
50 | get { return (IValueLookupParameter<IntValue>)Parameters["RandomWalkLength"]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | protected SlackMinimizationSolutionCreator(StorableConstructorFlag _) : base(_) { }
|
---|
55 | protected SlackMinimizationSolutionCreator(SlackMinimizationSolutionCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
56 | public SlackMinimizationSolutionCreator()
|
---|
57 | : base() {
|
---|
58 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution after which an exception is thrown. If it is set to 0 or a negative value there will be an infinite number of attempts to create a feasible solution.", new IntValue(100000)));
|
---|
59 | Parameters.Add(new ValueLookupParameter<BoolValue>("CreateMostFeasibleSolution", "If this is set to true the operator will always succeed, and outputs the solution with the least violation instead of throwing an exception.", new BoolValue(false)));
|
---|
60 | Parameters.Add(new ValueLookupParameter<IntValue>("Depth", "How deep the algorithm should look forward.", new IntValue(3)));
|
---|
61 | Parameters.Add(new ValueLookupParameter<IntValue>("RandomWalkLength", "The length of the random walk in the feasible region that is used to diversify the found assignments.", new IntValue(10)));
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
65 | return new SlackMinimizationSolutionCreator(this, cloner);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static IntegerVector CreateSolution(IRandom random, GQAPInstance problemInstance,
|
---|
69 | int depth, int maximumTries, bool createMostFeasibleSolution, int randomWalkLength, CancellationToken cancel) {
|
---|
70 | var capacities = problemInstance.Capacities;
|
---|
71 | var demands = problemInstance.Demands;
|
---|
72 |
|
---|
73 | IntegerVector result = null;
|
---|
74 | bool isFeasible = false;
|
---|
75 | int counter = 0;
|
---|
76 | double minViolation = double.MaxValue;
|
---|
77 | var slack = new double[capacities.Length];
|
---|
78 | var assignment = new Dictionary<int, int>(demands.Length);
|
---|
79 |
|
---|
80 | while (!isFeasible) {
|
---|
81 | cancel.ThrowIfCancellationRequested();
|
---|
82 | if (maximumTries > 0) {
|
---|
83 | counter++;
|
---|
84 | if (counter > maximumTries) {
|
---|
85 | if (createMostFeasibleSolution) break;
|
---|
86 | else throw new InvalidOperationException("A feasible solution could not be obtained after " + maximumTries + " attempts.");
|
---|
87 | }
|
---|
88 | }
|
---|
89 | assignment.Clear();
|
---|
90 | for (int i = 0; i < capacities.Length; i++) slack[i] = capacities[i];
|
---|
91 | var remainingEquipment = new HashSet<int>(Enumerable.Range(0, demands.Length));
|
---|
92 | while (remainingEquipment.Any()) {
|
---|
93 | var minimumDemand = remainingEquipment.Min(x => demands[x]);
|
---|
94 | var possibleLocations = Enumerable.Range(0, capacities.Length).Where(x => slack[x] >= minimumDemand);
|
---|
95 | if (!possibleLocations.Any()) break;
|
---|
96 | foreach (var location in possibleLocations.Shuffle(random)) {
|
---|
97 | var group = FindBestGroup(location, slack[location], remainingEquipment, demands, depth);
|
---|
98 | foreach (var eq in group) {
|
---|
99 | remainingEquipment.Remove(eq);
|
---|
100 | assignment[eq] = location;
|
---|
101 | slack[location] -= demands[eq];
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | if (assignment.Count != demands.Length) {
|
---|
106 | // complete the solution
|
---|
107 | while (remainingEquipment.Any()) {
|
---|
108 | var f = remainingEquipment.MaxItems(x => demands[x]).SampleRandom(random);
|
---|
109 | var l = Enumerable.Range(0, capacities.Length).MaxItems(x => slack[x]).SampleRandom(random);
|
---|
110 | remainingEquipment.Remove(f);
|
---|
111 | assignment.Add(f, l);
|
---|
112 | slack[l] -= demands[f];
|
---|
113 | }
|
---|
114 | } else RandomFeasibleWalk(random, assignment, demands, slack, randomWalkLength);
|
---|
115 | double violation = slack.Select(x => x < 0 ? -x : 0).Sum();
|
---|
116 | isFeasible = violation == 0;
|
---|
117 | if (isFeasible || violation < minViolation) {
|
---|
118 | result = new IntegerVector(assignment.OrderBy(x => x.Key).Select(x => x.Value).ToArray());
|
---|
119 | minViolation = violation;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 |
|
---|
125 | private static IEnumerable<int> FindBestGroup(int location, double slack, HashSet<int> remainingEquipment, DoubleArray demands, int depth = 3) {
|
---|
126 | var feasibleEquipment = remainingEquipment.Where(x => demands[x] <= slack).ToArray();
|
---|
127 |
|
---|
128 | if (!feasibleEquipment.Any()) yield break;
|
---|
129 | if (depth == 0) {
|
---|
130 | var e = feasibleEquipment.MaxItems(x => demands[x]).First();
|
---|
131 | yield return e;
|
---|
132 | yield break;
|
---|
133 | }
|
---|
134 |
|
---|
135 | double bestSlack = slack;
|
---|
136 | int bestEquipment = -1;
|
---|
137 | int[] bestColleagues = new int[0];
|
---|
138 | foreach (var e in feasibleEquipment) {
|
---|
139 | remainingEquipment.Remove(e);
|
---|
140 | var colleagues = FindBestGroup(location, slack - demands[e], remainingEquipment, demands, depth - 1).ToArray();
|
---|
141 | var slackWithColleagues = slack - demands[e] - colleagues.Sum(x => demands[x]);
|
---|
142 | if (bestSlack > slackWithColleagues || (bestSlack == slackWithColleagues && colleagues.Length < bestColleagues.Length)) {
|
---|
143 | bestSlack = slackWithColleagues;
|
---|
144 | bestEquipment = e;
|
---|
145 | bestColleagues = colleagues;
|
---|
146 | }
|
---|
147 | remainingEquipment.Add(e);
|
---|
148 | }
|
---|
149 | yield return bestEquipment;
|
---|
150 | foreach (var a in bestColleagues) yield return a;
|
---|
151 | }
|
---|
152 |
|
---|
153 | private static void RandomFeasibleWalk(IRandom random, Dictionary<int, int> assignment, DoubleArray demands, double[] slack, int walkLength) {
|
---|
154 | for (int i = 0; i < walkLength; i++) {
|
---|
155 | var equipments = Enumerable.Range(0, demands.Length).Shuffle(random);
|
---|
156 | foreach (var e in equipments) {
|
---|
157 | var partners = Enumerable.Range(0, demands.Length)
|
---|
158 | .Where(x => slack[assignment[x]] + demands[x] - demands[e] >= 0
|
---|
159 | && slack[assignment[e]] + demands[e] - demands[x] >= 0);
|
---|
160 | if (!partners.Any()) continue;
|
---|
161 | var f = partners.SampleRandom(random);
|
---|
162 | int h = assignment[e];
|
---|
163 | assignment[e] = assignment[f];
|
---|
164 | assignment[f] = h;
|
---|
165 | slack[assignment[e]] += demands[f] - demands[e];
|
---|
166 | slack[assignment[f]] += demands[e] - demands[f];
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | protected override IntegerVector CreateRandomSolution(IRandom random, GQAPInstance problemInstance) {
|
---|
173 | return CreateSolution(random, problemInstance,
|
---|
174 | DepthParameter.ActualValue.Value,
|
---|
175 | MaximumTriesParameter.ActualValue.Value,
|
---|
176 | CreateMostFeasibleSolutionParameter.ActualValue.Value,
|
---|
177 | RandomWalkLengthParameter.ActualValue.Value,
|
---|
178 | CancellationToken);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|