Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/SlackMinimizationSolutionCreator.cs @ 13397

Last change on this file since 13397 was 7970, checked in by abeham, 13 years ago

#1614: restructured architecture to allow for different evaluator with different penalty strategies

File size: 9.5 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 System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Random;
33
34namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
35  [Item("SlackMinimizationSolutionCreator", "A heuristic that creates a solution to the Generalized Quadratic Assignment Problem by minimizing the amount of slack.")]
36  [StorableClass]
37  public class SlackMinimizationSolutionCreator : GQAPStochasticSolutionCreator,
38    IEvaluatorAwareGQAPOperator {
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    public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
53      get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
54    }
55
56    [StorableConstructor]
57    protected SlackMinimizationSolutionCreator(bool deserializing) : base(deserializing) { }
58    protected SlackMinimizationSolutionCreator(SlackMinimizationSolutionCreator original, Cloner cloner) : base(original, cloner) { }
59    public SlackMinimizationSolutionCreator()
60      : base() {
61      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)));
62      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)));
63      Parameters.Add(new ValueLookupParameter<IntValue>("Depth", "How deep the algorithm should look forward.", new IntValue(3)));
64      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)));
65      Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new SlackMinimizationSolutionCreator(this, cloner);
70    }
71
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserialization() {
74      if (!Parameters.ContainsKey("Depth")) {
75        Parameters.Add(new ValueLookupParameter<IntValue>("Depth", "How deep the algorithm should look forward.", new IntValue(3)));
76      }
77      if (!Parameters.ContainsKey("RandomWalkLength")) {
78        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)));
79      }
80    }
81
82    public static IntegerVector CreateSolution(IRandom random, DoubleArray demands,
83      DoubleArray capacities, IGQAPEvaluator evaluator,
84      int depth, int maximumTries, bool createMostFeasibleSolution, int randomWalkLength, CancellationToken cancel) {
85      IntegerVector result = null;
86      bool isFeasible = false;
87      int counter = 0;
88      double minViolation = double.MaxValue;
89      var slack = new DoubleArray(capacities.Length);
90      var assignment = new Dictionary<int, int>(demands.Length);
91
92      while (!isFeasible) {
93        cancel.ThrowIfCancellationRequested();
94        if (maximumTries > 0) {
95          counter++;
96          if (counter > maximumTries) {
97            if (createMostFeasibleSolution) break;
98            else throw new InvalidOperationException("A feasible solution could not be obtained after " + maximumTries + " attempts.");
99          }
100        }
101        assignment.Clear();
102        for (int i = 0; i < capacities.Length; i++) slack[i] = capacities[i];
103        var remainingEquipment = new HashSet<int>(Enumerable.Range(0, demands.Length));
104        while (remainingEquipment.Any()) {
105          var minimumDemand = remainingEquipment.Min(x => demands[x]);
106          var possibleLocations = Enumerable.Range(0, capacities.Length).Where(x => slack[x] >= minimumDemand);
107          if (!possibleLocations.Any()) break;
108          foreach (var location in possibleLocations.Shuffle(random)) {
109            var group = FindBestGroup(location, slack[location], remainingEquipment, demands, depth);
110            foreach (var eq in group) {
111              remainingEquipment.Remove(eq);
112              assignment[eq] = location;
113              slack[location] -= demands[eq];
114            }
115          }
116        }
117        if (assignment.Count != demands.Length) {
118          // complete the solution
119          while (remainingEquipment.Any()) {
120            var f = remainingEquipment.MaxItems(x => demands[x]).SampleRandom(random);
121            var l = Enumerable.Range(0, capacities.Length).MaxItems(x => slack[x]).SampleRandom(random);
122            remainingEquipment.Remove(f);
123            assignment.Add(f, l);
124            slack[l] -= demands[f];
125          }
126        } else RandomFeasibleWalk(random, assignment, demands, slack, randomWalkLength);
127        double violation = evaluator.EvaluateOverbooking(slack, capacities);
128        isFeasible = violation == 0;
129        if (isFeasible || violation < minViolation) {
130          result = new IntegerVector(assignment.OrderBy(x => x.Key).Select(x => x.Value).ToArray());
131          minViolation = violation;
132        }
133      }
134      return result;
135    }
136
137    private static IEnumerable<int> FindBestGroup(int location, double slack, HashSet<int> remainingEquipment, DoubleArray demands, int depth = 3) {
138      var feasibleEquipment = remainingEquipment.Where(x => demands[x] <= slack).ToArray();
139
140      if (!feasibleEquipment.Any()) yield break;
141      if (depth == 0) {
142        var e = feasibleEquipment.MaxItems(x => demands[x]).First();
143        yield return e;
144        yield break;
145      }
146
147      double bestSlack = slack;
148      int bestEquipment = -1;
149      int[] bestColleagues = new int[0];
150      foreach (var e in feasibleEquipment) {
151        remainingEquipment.Remove(e);
152        var colleagues = FindBestGroup(location, slack - demands[e], remainingEquipment, demands, depth - 1).ToArray();
153        var slackWithColleagues = slack - demands[e] - colleagues.Sum(x => demands[x]);
154        if (bestSlack > slackWithColleagues || (bestSlack == slackWithColleagues && colleagues.Length < bestColleagues.Length)) {
155          bestSlack = slackWithColleagues;
156          bestEquipment = e;
157          bestColleagues = colleagues;
158        }
159        remainingEquipment.Add(e);
160      }
161      yield return bestEquipment;
162      foreach (var a in bestColleagues) yield return a;
163    }
164
165    private static void RandomFeasibleWalk(IRandom random, Dictionary<int, int> assignment, DoubleArray demands, DoubleArray slack, int walkLength) {
166      for (int i = 0; i < walkLength; i++) {
167        var equipments = Enumerable.Range(0, demands.Length).Shuffle(random);
168        foreach (var e in equipments) {
169          var partners = Enumerable.Range(0, demands.Length)
170            .Where(x => slack[assignment[x]] + demands[x] - demands[e] >= 0
171                && slack[assignment[e]] + demands[e] - demands[x] >= 0);
172          if (!partners.Any()) continue;
173          var f = partners.SampleRandom(random);
174          int h = assignment[e];
175          assignment[e] = assignment[f];
176          assignment[f] = h;
177          slack[assignment[e]] += demands[f] - demands[e];
178          slack[assignment[f]] += demands[e] - demands[f];
179          break;
180        }
181      }
182    }
183
184    protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) {
185      return CreateSolution(random, demands, capacities,
186        EvaluatorParameter.ActualValue,
187        DepthParameter.ActualValue.Value,
188        MaximumTriesParameter.ActualValue.Value,
189        CreateMostFeasibleSolutionParameter.ActualValue.Value,
190        RandomWalkLengthParameter.ActualValue.Value,
191        CancellationToken);
192    }
193  }
194}
Note: See TracBrowser for help on using the repository browser.