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 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.Problems.GeneralizedQuadraticAssignment.Common;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
|
---|
35 | [Item("RandomButFeasibleSolutionCreator", "Creates a random, but feasible solution to the Generalized Quadratic Assignment Problem.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class RandomFeasibleSolutionCreator : GQAPStochasticSolutionCreator {
|
---|
38 |
|
---|
39 | public IValueLookupParameter<IntValue> MaximumTriesParameter {
|
---|
40 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTries"]; }
|
---|
41 | }
|
---|
42 | public IValueLookupParameter<BoolValue> CreateMostFeasibleSolutionParameter {
|
---|
43 | get { return (IValueLookupParameter<BoolValue>)Parameters["CreateMostFeasibleSolution"]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | protected RandomFeasibleSolutionCreator(bool deserializing) : base(deserializing) { }
|
---|
48 | protected RandomFeasibleSolutionCreator(RandomFeasibleSolutionCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
49 | public RandomFeasibleSolutionCreator()
|
---|
50 | : base() {
|
---|
51 | 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.", new IntValue(100000)));
|
---|
52 | 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)));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new RandomFeasibleSolutionCreator(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static IntegerVector CreateSolution(IRandom random, DoubleArray demands, DoubleArray capacities, int maximumTries, bool createMostFeasibleSolution, CancellationToken cancel) {
|
---|
60 | IntegerVector result = null;
|
---|
61 | bool isFeasible = false;
|
---|
62 | int counter = 0;
|
---|
63 | double minViolation = double.MaxValue;
|
---|
64 | var slack = new DoubleArray(capacities.Length);
|
---|
65 | var assignment = new IntegerVector(demands.Length);
|
---|
66 |
|
---|
67 | while (!isFeasible) {
|
---|
68 | cancel.ThrowIfCancellationRequested();
|
---|
69 | if (maximumTries > 0) {
|
---|
70 | counter++;
|
---|
71 | if (counter > maximumTries) {
|
---|
72 | if (createMostFeasibleSolution) break;
|
---|
73 | else throw new InvalidOperationException("A feasible solution could not be obtained after " + maximumTries + " attempts.");
|
---|
74 | }
|
---|
75 | }
|
---|
76 | for (int i = 0; i < capacities.Length; i++) slack[i] = capacities[i];
|
---|
77 | foreach (var equipment in Enumerable.Range(0, demands.Length).Shuffle(random)) {
|
---|
78 | var freeLocations = GetFreeLocations(equipment, demands, slack);
|
---|
79 | assignment[equipment] = freeLocations.ChooseUniformRandom(random);
|
---|
80 | slack[assignment[equipment]] -= demands[equipment];
|
---|
81 | }
|
---|
82 | double violation = GQAPEvaluator.EvaluateOverbooking(slack, capacities);
|
---|
83 | isFeasible = violation == 0;
|
---|
84 | if (isFeasible || violation < minViolation) {
|
---|
85 | result = (IntegerVector)assignment.Clone();
|
---|
86 | minViolation = violation;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return result;
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) {
|
---|
93 | return CreateSolution(random, demands, capacities,
|
---|
94 | MaximumTriesParameter.ActualValue.Value,
|
---|
95 | CreateMostFeasibleSolutionParameter.ActualValue.Value,
|
---|
96 | CancellationToken);
|
---|
97 | }
|
---|
98 |
|
---|
99 | private static IEnumerable<int> GetFreeLocations(int equipment, DoubleArray demands, DoubleArray freeCapacities) {
|
---|
100 | var freeLocations = freeCapacities
|
---|
101 | .Select((v, idx) => new KeyValuePair<int, double>(idx, v))
|
---|
102 | .Where(x => x.Value >= demands[equipment]);
|
---|
103 | if (!freeLocations.Any()) {
|
---|
104 | freeLocations = freeCapacities
|
---|
105 | .Select((v, idx) => new KeyValuePair<int, double>(idx, v))
|
---|
106 | .OrderByDescending(x => x.Value)
|
---|
107 | .Take(3); // if there are none, take the three where the free capacity is largest
|
---|
108 | }
|
---|
109 | return freeLocations.Select(x => x.Key);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|