Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/11/12 10:54:36 (12 years ago)
Author:
abeham
Message:

#1614

  • Investigation in different solution creator that result in feasible solutions even for 95% fill level instances
  • Added option for all solution creators to produce the least infeasible solution if no feasible solution could be found
  • Added maximum tries parameter to all creators
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GreedyRandomizedSolutionCreator.cs

    r7523 r7593  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    3940      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTries"]; }
    4041    }
     42    public IValueLookupParameter<BoolValue> CreateMostFeasibleSolutionParameter {
     43      get { return (IValueLookupParameter<BoolValue>)Parameters["CreateMostFeasibleSolution"]; }
     44    }
    4145
    4246    [StorableConstructor]
     
    4650    public GreedyRandomizedSolutionCreator()
    4751      : base() {
    48       Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution.", new IntValue(10000)));
     52      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)));
     53      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)));
    4954    }
    5055
     
    5358    }
    5459
    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
     60    public static IntegerVector CreateSolution(IRandom random, DoubleArray demands, DoubleArray capacities, int maximumTries, bool createMostFeasibleSolution, CancellationToken cancelToken) {
     61      int tries = 0;
     62      var assignment = new Dictionary<int, int>(demands.Length);
     63      DoubleArray slack = new DoubleArray(capacities.Length);
     64      double minViolation = double.MaxValue;
     65      Dictionary<int, int> bestAssignment = null;
     66      HashSet<int> CF = new HashSet<int>(), // set of chosen facilities / equipments
    6467          T = new HashSet<int>(), // set of facilities / equpiments that can be assigned to the set of chosen locations (CL)
    6568          CL = new HashSet<int>(), // set of chosen locations
    6669          F = new HashSet<int>(Enumerable.Range(0, demands.Length)), // set of (initially) all facilities / equipments
    6770          L = new HashSet<int>(Enumerable.Range(0, capacities.Length)); // set of (initially) all locations
     71
     72      while (maximumTries <= 0 || tries < maximumTries) {
     73        cancelToken.ThrowIfCancellationRequested();
     74
     75        assignment.Clear();
     76        for (int i = 0; i < capacities.Length; i++) slack[i] = capacities[i];
     77        CF.Clear();
     78        T.Clear();
     79        CL.Clear();
     80        F.Clear(); F.UnionWith(Enumerable.Range(0, demands.Length));
     81        L.Clear(); L.UnionWith(Enumerable.Range(0, capacities.Length));
    6882
    6983        double threshold = 1.0;
     
    87101          }
    88102        } while (T.Any() || L.Any());
    89         tries++;
    90         if (!F.Any()) break;
     103        if (maximumTries > 0) tries++;
     104        if (!F.Any()) {
     105          bestAssignment = assignment;
     106          break;
     107        } else if (createMostFeasibleSolution) {
     108          // complete the solution and remember the one with least violation
     109          while (F.Any()) {
     110            var f = F.ChooseMax(x => demands[x]);
     111            var l = L.ChooseMax(x => slack[x]);
     112            F.Remove(f);
     113            assignment.Add(f, l);
     114            slack[l] -= demands[f];
     115          }
     116          double violation = GQAPEvaluator.EvaluateOverbooking(slack, capacities);
     117          if (violation < minViolation) {
     118            bestAssignment = assignment;
     119            assignment = new Dictionary<int, int>(demands.Length);
     120            minViolation = violation;
     121          }
     122        }
    91123      }
    92124
    93       if (assignment.Count != demands.Length) throw new InvalidOperationException(String.Format("No solution could be found in {0} tries.", maxTries));
     125      if (bestAssignment == null || bestAssignment.Count != demands.Length) throw new InvalidOperationException(String.Format("No solution could be found in {0} tries.", maximumTries));
    94126
    95       return new IntegerVector(assignment.OrderBy(x => x.Key).Select(x => x.Value).ToArray());
     127      return new IntegerVector(bestAssignment.OrderBy(x => x.Key).Select(x => x.Value).ToArray());
     128    }
     129
     130    protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) {
     131      return CreateSolution(random, demands, capacities,
     132        MaximumTriesParameter.ActualValue.Value,
     133        CreateMostFeasibleSolutionParameter.ActualValue.Value,
     134        CancellationToken);
    96135    }
    97136
     
    102141    }
    103142
    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();
     143    private static double GetMaximumSlack(DoubleArray slack, HashSet<int> CL) {
     144      return slack.Select((val, idx) => new { idx, val }).Where(x => CL.Contains(x.idx)).Select(x => x.val).Max();
    106145    }
    107146
    108     private static IEnumerable<int> WithSlackGreaterOrEqual(HashSet<int> locations, double minimum, Dictionary<int, double> slack) {
     147    private static IEnumerable<int> WithSlackGreaterOrEqual(HashSet<int> locations, double minimum, DoubleArray slack) {
    109148      foreach (int l in locations) {
    110149        if (slack[l] >= minimum) yield return l;
Note: See TracChangeset for help on using the changeset viewer.