Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/24/12 17:23:21 (13 years ago)
Author:
abeham
Message:

#1614: worked on GQAP

Location:
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3
Files:
16 added
1 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Evaluators/GQAPEvaluator.cs

    r7319 r7407  
    8787    }
    8888
     89    public static DoubleValue Evaluate(IntegerVector assignment, DoubleMatrix weights, DoubleMatrix distances,
     90      DoubleMatrix installCosts, double transportCosts, DoubleArray demands, DoubleArray capacities,
     91      out double infeasibility) {
     92      double quality = 0;
     93      int len = assignment.Length;
     94      var slack = (DoubleArray)capacities.Clone();
     95      for (int i = 0; i < len; i++) {
     96        quality += installCosts[i, assignment[i]];
     97        for (int j = 0; j < len; j++) {
     98          quality += transportCosts * weights[i, j] * distances[assignment[i], assignment[j]];
     99        }
     100        slack[assignment[i]] -= demands[i];
     101      }
     102
     103      infeasibility = -slack.Where(x => x < 0).Sum();
     104      return new DoubleValue(quality);
     105    }
     106
    89107    public override IOperation Apply() {
    90108      IntegerVector assignment = AssignmentParameter.ActualValue;
     
    96114      DoubleArray capacities = (DoubleArray)CapacitiesParameter.ActualValue.Clone();
    97115      double penalty = PenaltyParameter.ActualValue.Value;
    98       double quality = 0;
    99       double infeasibility = 0;
    100116
    101117      if (weights.Rows != weights.Columns || distances.Rows != distances.Columns
     
    104120        throw new InvalidOperationException("ERROR: The problem configuration is not valid! Check the sizes of the weights (NxN), distances (MxM) and installation costs (NxM) matrices as well as the length of the demand (N) and capacities (M) vectors.");
    105121
    106       int len = assignment.Length;
    107       for (int i = 0; i < len; i++) {
    108         quality += installCosts[i, assignment[i]];
    109         for (int j = 0; j < len; j++) {
    110           quality += transportCosts * weights[i, j] * distances[assignment[i], assignment[j]];
    111         }
    112         capacities[assignment[i]] -= demands[i];
    113       }
    114 
    115       infeasibility = -capacities.Where(x => x < 0).Sum();
     122      double infeasibility;
     123      var quality = Evaluate(assignment, weights, distances, installCosts, transportCosts, demands, capacities, out infeasibility);
    116124
    117125      InfeasibilityParameter.ActualValue = new DoubleValue(infeasibility);
    118       QualityParameter.ActualValue = new DoubleValue(quality + penalty * infeasibility);
     126      QualityParameter.ActualValue = new DoubleValue(quality.Value + penalty * infeasibility);
    119127      return base.Apply();
    120128    }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GeneralizedQuadraticAssignmentProblem.cs

    r7373 r7407  
    170170      base.OnSolutionCreatorChanged();
    171171      Parameterize();
    172       SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
     172      SolutionCreator.AssignmentParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
    173173    }
    174174
     
    212212      SolutionCreator.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
    213213
     214      foreach (var op in Operators.OfType<IEquipmentAwareGQAPOperator>()) {
     215        op.DemandsParameter.ActualName = DemandsParameter.Name;
     216      }
     217      foreach (var op in Operators.OfType<IGQAPCrossover>()) {
     218        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
     219        op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
     220      }
     221      foreach (var op in Operators.OfType<IGQAPEvaluationOperator>()) {
     222        op.WeightsParameter.ActualName = WeightsParameter.Name;
     223        op.DistancesParameter.ActualName = DistancesParameter.Name;
     224        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
     225        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
     226        op.DemandsParameter.ActualName = DemandsParameter.Name;
     227        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
     228        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
     229      }
     230      foreach (var op in Operators.OfType<IGQAPLocalImprovementOperator>()) {
     231        op.WeightsParameter.ActualName = WeightsParameter.Name;
     232        op.DistancesParameter.ActualName = DistancesParameter.Name;
     233        op.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name;
     234        op.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name;
     235        op.DemandsParameter.ActualName = DemandsParameter.Name;
     236        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
     237        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
     238        op.InfeasibilityParameter.ActualName = Evaluator.InfeasibilityParameter.ActualName;
     239      }
     240      foreach (var op in Operators.OfType<IGQAPManipulator>()) {
     241        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
     242      }
     243      foreach (var op in Operators.OfType<IGQAPMoveOperator>()) {
     244        op.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
     245      }
     246      foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) {
     247        op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
     248      }
     249
    214250      foreach (var op in Operators.OfType<IIntegerVectorCrossover>()) {
    215251        op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
     
    218254      foreach (var op in Operators.OfType<IIntegerVectorManipulator>()) {
    219255        op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
    220       }
    221       foreach (var op in Operators.OfType<IGQAPCrossover>()) {
    222         op.ParentsParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
    223         op.ChildParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
    224       }
    225       foreach (var op in Operators.OfType<IGQAPManipulator>()) {
    226         op.IntegerVectorParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName;
    227       }
    228       foreach (var op in Operators.OfType<ILocationAwareGQAPOperator>()) {
    229         op.CapacitiesParameter.ActualName = CapacitiesParameter.Name;
    230       }
    231       foreach (var op in Operators.OfType<IEquipmentAwareGQAPOperator>()) {
    232         op.DemandsParameter.ActualName = DemandsParameter.Name;
    233256      }
    234257
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/HeuristicLab.Problems.GeneralizedQuadraticAssignment-3.3.csproj

    r7363 r7407  
    8989  <ItemGroup>
    9090    <Compile Include="Analyzers\BestGQAPSolutionAnalyzer.cs" />
    91     <Compile Include="ExtensionMethods.cs" />
     91    <Compile Include="Evaluators\GQAPNMoveEvaluator.cs" />
    9292    <Compile Include="GQAPAssignment.cs" />
    9393    <None Include="Plugin.cs.frame" />
    9494    <Compile Include="Evaluators\GQAPEvaluator.cs" />
    9595    <Compile Include="GeneralizedQuadraticAssignmentProblem.cs" />
     96    <Compile Include="Interfaces\IGQAPNMoveEvaluator.cs" />
    9697    <Compile Include="Interfaces\IEquipmentAwareGQAPOperator.cs" />
    9798    <Compile Include="Interfaces\IGQAPCrossover.cs" />
     99    <Compile Include="Interfaces\IGQAPEvaluationOperator.cs" />
    98100    <Compile Include="Interfaces\IGQAPEvaluator.cs" />
     101    <Compile Include="Interfaces\IGQAPLocalImprovementOperator.cs" />
    99102    <Compile Include="Interfaces\IGQAPManipulator.cs" />
     103    <Compile Include="Interfaces\IGQAPMoveEvaluator.cs" />
     104    <Compile Include="Interfaces\IGQAPMoveOperator.cs" />
     105    <Compile Include="Interfaces\IGQAPNMoveOperator.cs" />
    100106    <Compile Include="Interfaces\IGQAPOperator.cs" />
     107    <Compile Include="Interfaces\IGQAPSolutionCreator.cs" />
    101108    <Compile Include="Interfaces\ILocationAwareGQAPOperator.cs" />
     109    <Compile Include="Moves\NMove.cs" />
     110    <Compile Include="Moves\GQAPMoveGenerator.cs" />
     111    <Compile Include="Moves\GQAPNMoveGenerator.cs" />
     112    <Compile Include="Moves\NMoveMaker.cs" />
     113    <Compile Include="Moves\StochasticNMoveMultiMoveGenerator.cs" />
     114    <Compile Include="Moves\StochasticNMoveSingleMoveGenerator.cs" />
     115    <Compile Include="Operators\ApproximateLocalSearch.cs" />
     116    <Compile Include="Operators\GQAPStochasticSolutionCreator.cs" />
    102117    <Compile Include="Operators\DemandEquivalentSwapEquipmentManipluator.cs" />
    103118    <Compile Include="Operators\GQAPCrossover.cs" />
    104119    <Compile Include="Operators\DiscreteLocationCrossover.cs" />
     120    <Compile Include="Operators\GQAPSolutionCreator.cs" />
     121    <Compile Include="Operators\GreedyRandomizedSolutionCreator.cs" />
     122    <Compile Include="Operators\RandomSolutionCreator.cs" />
    105123    <Compile Include="Operators\RelocateEquipmentManipluator.cs" />
    106124    <Compile Include="Operators\GQAPManipulator.cs" />
     
    120138    </ProjectReference>
    121139  </ItemGroup>
     140  <ItemGroup />
    122141  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    123142  <PropertyGroup>
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Interfaces/IGQAPEvaluator.cs

    r7319 r7407  
    2222using HeuristicLab.Core;
    2323using HeuristicLab.Data;
    24 using HeuristicLab.Encodings.IntegerVectorEncoding;
    2524using HeuristicLab.Optimization;
    2625
    2726namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
    28   public interface IGQAPEvaluator : ISingleObjectiveEvaluator {
    29     ILookupParameter<DoubleMatrix> WeightsParameter { get; }
    30     ILookupParameter<DoubleMatrix> DistancesParameter { get; }
    31     ILookupParameter<DoubleMatrix> InstallationCostsParameter { get; }
    32     ILookupParameter<DoubleValue> TransportationCostsParameter { get; }
    33     ILookupParameter<DoubleArray> DemandsParameter { get; }
    34     ILookupParameter<DoubleArray> CapacitiesParameter { get; }
    35     ILookupParameter<IntegerVector> AssignmentParameter { get; }
     27  public interface IGQAPEvaluator : IGQAPEvaluationOperator, ISingleObjectiveEvaluator {
     28    ILookupParameter<DoubleValue> InfeasibilityParameter { get; }
    3629  }
    3730}
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/DemandEquivalentSwapEquipmentManipluator.cs

    r7373 r7407  
    2929using HeuristicLab.Parameters;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
    3132
    3233namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
     
    7980          groupedLocations[location2].Remove(equipment2);
    8081          bool selected;
    81           equipment2 = groupedLocations[location2].SelectRandomOrDefault(random, out selected);
     82          equipment2 = groupedLocations[location2].ChooseRandomOrDefault(random, out selected);
    8283          if (!selected) break;
    8384        }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/DiscreteLocationCrossover.cs

    r7319 r7407  
    2828using HeuristicLab.Parameters;
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
    3031
    31 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Operators {
     32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
    3233  [Item("DiscreteLocationCrossover", "Combines the assignment to locations from various parents.")]
    3334  [StorableClass]
     
    5253
    5354    public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, DoubleArray capacities) {
    54      
     55
    5556      var groupedLocations = parents
    5657              .Select(p => p.Select((value, index) => new { Equipment = index, Location = value })
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/GreedyRandomizedSolutionCreator.cs

    r7373 r7407  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    2627using HeuristicLab.Data;
    2728using HeuristicLab.Encodings.IntegerVectorEncoding;
    28 using HeuristicLab.Optimization;
    2929using HeuristicLab.Parameters;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
    3132
    32 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Operators {
     33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
    3334  [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.")]
    3435  [StorableClass]
    35   public class GreedyRandomizedSolutionCreator : GQAPSolutionCreator, IStochasticOperator {
     36  public class GreedyRandomizedSolutionCreator : GQAPStochasticSolutionCreator {
    3637
    37     public ILookupParameter<IRandom> RandomParameter {
    38       get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    39     }
    4038    public IValueLookupParameter<IntValue> MaximumTriesParameter {
    4139      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumTries"]; }
     
    4846    public GreedyRandomizedSolutionCreator()
    4947      : base() {
    50       Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    51       Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution."));
     48      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumTries", "The maximum number of tries to create a feasible solution.", new IntValue(10000)));
    5249    }
    5350
     
    5653    }
    5754
    58     protected override IntegerVector CreateSolution(DoubleArray demands, DoubleArray capacities) {
    59       int k = 0, t = MaximumTriesParameter.ActualValue.Value;
    60       HashSet<int> F = new HashSet<int>(Enumerable.Range(0, demands.Length));
    61       while (k < t && F.Any()) {
     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>();
    6259
     60      while (tries < maxTries) {
     61        assignment.Clear();
     62        demands.Select((v, i) => slack[i] = v).Enumerate();
     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.ChooseRandom(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.ChooseRandom(random);
     79            T.Remove(f);
     80            F.Remove(f);
     81            CF.Add(f);
     82            int l = WithSlackGreaterOrEqual(CL, demands[f], slack).ChooseRandom(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;
    6391      }
    6492
    65       // TODO: return actual solution
    66       return UniformRandomIntegerVectorCreator.Apply(RandomParameter.ActualValue, demands.Length, 0, capacities.Length);
     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      }
    67112    }
    68113  }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/RandomSolutionCreator.cs

    r7373 r7407  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.Encodings.IntegerVectorEncoding;
    26 using HeuristicLab.Optimization;
    27 using HeuristicLab.Parameters;
    2826using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2927
     
    3129  [Item("RandomSolutionCreator", "Creates a completely random solution to the Generalized Quadratic Assignment Problem.")]
    3230  [StorableClass]
    33   public class RandomSolutionCreator : GQAPSolutionCreator, IStochasticOperator {
    34 
    35     public ILookupParameter<IRandom> RandomParameter {
    36       get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    37     }
     31  public class RandomSolutionCreator : GQAPStochasticSolutionCreator {
    3832
    3933    [StorableConstructor]
    4034    protected RandomSolutionCreator(bool deserializing) : base(deserializing) { }
    41     protected RandomSolutionCreator(RandomSolutionCreator original, Cloner cloner)
    42       : base(original, cloner) { }
    43     public RandomSolutionCreator()
    44       : base() {
    45       Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    46     }
     35    protected RandomSolutionCreator(RandomSolutionCreator original, Cloner cloner) : base(original, cloner) { }
     36    public RandomSolutionCreator() : base() { }
    4737
    4838    public override IDeepCloneable Clone(Cloner cloner) {
     
    5040    }
    5141
    52     protected override IntegerVector CreateSolution(DoubleArray demands, DoubleArray capacities) {
    53       return UniformRandomIntegerVectorCreator.Apply(RandomParameter.ActualValue, demands.Length, 0, capacities.Length);
     42    protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) {
     43      return UniformRandomIntegerVectorCreator.Apply(random, demands.Length, 0, capacities.Length);
    5444    }
    5545  }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/RelocateEquipmentManipluator.cs

    r7319 r7407  
    2929using HeuristicLab.Parameters;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
    3132
    3233namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
     
    3435  [Item("RelocateEquipmentManipluator", "Relocates a random equipment from an overstuffed location to a random one with space or a random equipment if constraints are satisfied.")]
    3536  [StorableClass]
    36   public class RelocateEquipmentManipluator : GQAPManipulator, ILocationAwareGQAPOperator, IEquipmentAwareGQAPManipulator {
     37  public class RelocateEquipmentManipluator : GQAPManipulator, ILocationAwareGQAPOperator, IEquipmentAwareGQAPOperator {
    3738
    3839    public ILookupParameter<DoubleArray> CapacitiesParameter {
     
    6061      if (locations < 2) throw new InvalidOperationException("There are not enough locations for relocation operations.");
    6162
    62       Dictionary<int, double> usedCapacities = new Dictionary<int,double>();
     63      Dictionary<int, double> usedCapacities = new Dictionary<int, double>();
    6364      Dictionary<int, List<int>> groupedLocations = new Dictionary<int, List<int>>();
    6465      for (int i = 0; i < locations; i++) {
     
    7273
    7374      bool selected;
    74       var overstuffedLocation = usedCapacities.Where(x => capacities[x.Key] < x.Value).SelectRandomOrDefault(random, out selected);
     75      var overstuffedLocation = usedCapacities.Where(x => capacities[x.Key] < x.Value).ChooseRandomOrDefault(random, out selected);
    7576      if (selected) {
    76         int equipment = groupedLocations[overstuffedLocation.Key].SelectRandomOrDefault(random, out selected);
     77        int equipment = groupedLocations[overstuffedLocation.Key].ChooseRandomOrDefault(random, out selected);
    7778        var freeLocations = usedCapacities.Where(x => capacities[x.Key] > x.Value).ToList();
    78         var location = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipment]).SelectRandomOrDefault(random, out selected);
     79        var location = freeLocations.Where(x => capacities[x.Key] > x.Value + demands[equipment]).ChooseRandomOrDefault(random, out selected);
    7980        if (selected) {
    8081          assignment[equipment] = location.Key;
    8182        } else {
    82           location = freeLocations.SelectRandomOrDefault(random, out selected);
     83          location = freeLocations.ChooseRandomOrDefault(random, out selected);
    8384          if (selected) assignment[equipment] = location.Key;
    8485          else assignment[equipment] = random.Next(locations);
Note: See TracChangeset for help on using the changeset viewer.