Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/GreedyRandomizedSolutionCreator.cs @ 7432

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

#1614

  • updated gqap (finished path-relinking)
  • fixed some bugs
File size: 5.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;
31using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
34  [Item("GreedyRandomizedSolutionCreator", "Creates a solution according to the procedure described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.")]
35  [StorableClass]
36  public class GreedyRandomizedSolutionCreator : GQAPStochasticSolutionCreator {
37
38    public IValueLookupParameter<IntValue> MaximumTriesParameter {
39      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTries"]; }
40    }
41
42    [StorableConstructor]
43    protected GreedyRandomizedSolutionCreator(bool deserializing) : base(deserializing) { }
44    protected GreedyRandomizedSolutionCreator(GreedyRandomizedSolutionCreator original, Cloner cloner)
45      : base(original, cloner) { }
46    public GreedyRandomizedSolutionCreator()
47      : base() {
48      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution.", new IntValue(10000)));
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new GreedyRandomizedSolutionCreator(this, cloner);
53    }
54
55    protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) {
56      int tries = 0, maxTries = MaximumTriesParameter.ActualValue.Value;
57      var assignment = new Dictionary<int, int>();
58      var slack = new Dictionary<int, double>();
59
60      while (tries < maxTries) {
61        assignment.Clear();
62        slack = capacities.Select((v, i) => new { Index = i, Value = v }).ToDictionary(x => x.Index, y => y.Value);
63        HashSet<int> CF = new HashSet<int>(), // set of chosen facilities / equipments
64          T = new HashSet<int>(), // set of facilities / equpiments that can be assigned to the set of chosen locations (CL)
65          CL = new HashSet<int>(), // set of chosen locations
66          F = new HashSet<int>(Enumerable.Range(0, demands.Length)), // set of (initially) all facilities / equipments
67          L = new HashSet<int>(Enumerable.Range(0, capacities.Length)); // set of (initially) all locations
68
69        double threshold = 1.0;
70        do {
71          if (L.Any() && random.NextDouble() < threshold) {
72            int l = L.ChooseUniformRandom(random);
73            L.Remove(l);
74            CL.Add(l);
75            T = new HashSet<int>(WithDemandEqualOrLess(F, GetMaximumSlack(slack, CL), demands));
76          }
77          if (T.Any()) {
78            int f = T.ChooseUniformRandom(random);
79            T.Remove(f);
80            F.Remove(f);
81            CF.Add(f);
82            int l = WithSlackGreaterOrEqual(CL, demands[f], slack).ChooseUniformRandom(random);
83            assignment.Add(f, l);
84            slack[l] -= demands[f];
85            T = new HashSet<int>(WithDemandEqualOrLess(F, GetMaximumSlack(slack, CL), demands));
86            threshold = 1.0 - (double)T.Count / Math.Max(F.Count, 1.0);
87          }
88        } while (T.Any() || L.Any());
89        tries++;
90        if (!F.Any()) break;
91      }
92
93      if (assignment.Count != demands.Length) throw new InvalidOperationException(String.Format("No solution could be found in {0} tries.", maxTries));
94
95      return new IntegerVector(assignment.OrderBy(x => x.Key).Select(x => x.Value).ToArray());
96    }
97
98    private static IEnumerable<int> WithDemandEqualOrLess(IEnumerable<int> facilities, double maximum, DoubleArray demands) {
99      foreach (int f in facilities) {
100        if (demands[f] <= maximum) yield return f;
101      }
102    }
103
104    private static double GetMaximumSlack(Dictionary<int, double> slack, HashSet<int> CL) {
105      return slack.Where(x => CL.Contains(x.Key)).Select(x => x.Value).Max();
106    }
107
108    private static IEnumerable<int> WithSlackGreaterOrEqual(HashSet<int> locations, double minimum, Dictionary<int, double> slack) {
109      foreach (int l in locations) {
110        if (slack[l] >= minimum) yield return l;
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.