Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/10/17 22:11:10 (7 years ago)
Author:
abeham
Message:

#1614: refactored code

  • change problem to derive from basic problem
  • using a combined instance class instead of individual parameters
Location:
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators
Files:
1 deleted
5 edited

Legend:

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

    r7523 r15504  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3131  [Item("GQAPSolutionCreator", "Base class for solution creators of the Generalized Quadratic Assignment Problem.")]
    3232  [StorableClass]
    33   public abstract class GQAPSolutionCreator : SingleSuccessorOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator, IGQAPSolutionCreator {
     33  public abstract class GQAPSolutionCreator : SingleSuccessorOperator, IProblemInstanceAwareGQAPOperator, IGQAPSolutionCreator {
    3434
    3535    #region Parameter Descriptions
     
    3737    #endregion
    3838
    39     public ILookupParameter<IntegerVector> AssignmentParameter {
    40       get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
     39    public ILookupParameter<IntegerVector> IntegerVectorParameter {
     40      get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
    4141    }
    42     public ILookupParameter<DoubleArray> DemandsParameter {
    43       get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
     42    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
     43      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
    4444    }
    45     public ILookupParameter<DoubleArray> CapacitiesParameter {
    46       get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
     45    public IValueLookupParameter<IntValue> LengthParameter {
     46      get { return (IValueLookupParameter<IntValue>)Parameters["Length"]; }
     47    }
     48    public IValueLookupParameter<IntMatrix> BoundsParameter {
     49      get { return (IValueLookupParameter<IntMatrix>)Parameters["Bounds"]; }
    4750    }
    4851
     
    5356    public GQAPSolutionCreator()
    5457      : base() {
    55       Parameters.Add(new LookupParameter<IntegerVector>("Assignment", AssignmentDescription));
    56       Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
    57       Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
     58      Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", AssignmentDescription));
     59      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
     60      Parameters.Add(new ValueLookupParameter<IntValue>("Length", "The length of the vector.") { Hidden = true });
     61      Parameters.Add(new ValueLookupParameter<IntMatrix>("Bounds", "") { Hidden = true });
    5862    }
    5963
    6064    public override IOperation Apply() {
    61       AssignmentParameter.ActualValue = CreateSolution(DemandsParameter.ActualValue, CapacitiesParameter.ActualValue);
     65      IntegerVectorParameter.ActualValue = CreateSolution(ProblemInstanceParameter.ActualValue);
    6266      return base.Apply();
    6367    }
    6468
    65     protected abstract IntegerVector CreateSolution(DoubleArray demands, DoubleArray capacities);
     69    protected abstract IntegerVector CreateSolution(GQAPInstance problemInstance);
    6670  }
    6771}
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GQAPStochasticSolutionCreator.cs

    r7523 r15504  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.IntegerVectorEncoding;
    2625using HeuristicLab.Optimization;
     
    4645    }
    4746
    48     protected sealed override IntegerVector CreateSolution(DoubleArray demands, DoubleArray capacities) {
    49       return CreateRandomSolution(RandomParameter.ActualValue, demands, capacities);
     47    protected sealed override IntegerVector CreateSolution(GQAPInstance problemInstance) {
     48      return CreateRandomSolution(RandomParameter.ActualValue, problemInstance);
    5049    }
    5150
    52     protected abstract IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities);
     51    protected abstract IntegerVector CreateRandomSolution(IRandom random, GQAPInstance problemInstance);
    5352  }
    5453}
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GreedyRandomizedSolutionCreator.cs

    r7970 r15504  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3535  [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.")]
    3636  [StorableClass]
    37   public class GreedyRandomizedSolutionCreator : GQAPStochasticSolutionCreator,
    38     IEvaluatorAwareGQAPOperator {
     37  public class GreedyRandomizedSolutionCreator : GQAPStochasticSolutionCreator {
    3938
    4039    public IValueLookupParameter<IntValue> MaximumTriesParameter {
     
    4342    public IValueLookupParameter<BoolValue> CreateMostFeasibleSolutionParameter {
    4443      get { return (IValueLookupParameter<BoolValue>)Parameters["CreateMostFeasibleSolution"]; }
    45     }
    46     public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
    47       get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
    4844    }
    4945
     
    5652      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)));
    5753      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)));
    58       Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
    5954    }
    6055
     
    6358    }
    6459
    65     public static IntegerVector CreateSolution(IRandom random, DoubleArray demands, DoubleArray capacities,
    66       IGQAPEvaluator evaluator,
     60    public static IntegerVector CreateSolution(IRandom random, GQAPInstance problemInstance,
    6761      int maximumTries, bool createMostFeasibleSolution, CancellationToken cancelToken) {
     62      var demands = problemInstance.Demands;
     63      var capacities = problemInstance.Capacities;
    6864      int tries = 0;
    6965      var assignment = new Dictionary<int, int>(demands.Length);
     
    125121            slack[l] -= demands[f];
    126122          }
    127           double violation = evaluator.EvaluateOverbooking(slack, capacities);
     123          double violation = slack.Select(x => x < 0 ? -x : 0).Sum();
    128124          if (violation < minViolation) {
    129125            bestAssignment = assignment;
     
    139135    }
    140136
    141     protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) {
    142       return CreateSolution(random, demands, capacities,
    143         EvaluatorParameter.ActualValue,
     137    protected override IntegerVector CreateRandomSolution(IRandom random, GQAPInstance problemInstance) {
     138      return CreateSolution(random, problemInstance,
    144139        MaximumTriesParameter.ActualValue.Value,
    145140        CreateMostFeasibleSolutionParameter.ActualValue.Value,
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/RandomButFeasibleSolutionCreator.cs

    r7970 r15504  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3535  [Item("RandomButFeasibleSolutionCreator", "Creates a random, but feasible solution to the Generalized Quadratic Assignment Problem.")]
    3636  [StorableClass]
    37   public class RandomFeasibleSolutionCreator : GQAPStochasticSolutionCreator,
    38     IEvaluatorAwareGQAPOperator {
     37  public class RandomFeasibleSolutionCreator : GQAPStochasticSolutionCreator {
    3938
    4039    public IValueLookupParameter<IntValue> MaximumTriesParameter {
     
    4342    public IValueLookupParameter<BoolValue> CreateMostFeasibleSolutionParameter {
    4443      get { return (IValueLookupParameter<BoolValue>)Parameters["CreateMostFeasibleSolution"]; }
    45     }
    46     public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
    47       get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
    4844    }
    4945
     
    5551      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)));
    5652      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)));
    57       Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
    5853    }
    5954
     
    6257    }
    6358
    64     public static IntegerVector CreateSolution(IRandom random, DoubleArray demands,
    65       DoubleArray capacities, IGQAPEvaluator evaluator,
     59    public static IntegerVector CreateSolution(IRandom random, GQAPInstance problemInstance,
    6660      int maximumTries, bool createMostFeasibleSolution, CancellationToken cancel) {
     61      var capacities = problemInstance.Capacities;
     62      var demands = problemInstance.Demands;
    6763      IntegerVector result = null;
    6864      bool isFeasible = false;
     
    8783          slack[assignment[equipment]] -= demands[equipment];
    8884        }
    89         double violation = evaluator.EvaluateOverbooking(slack, capacities);
     85        double violation = slack.Select(x => x < 0 ? -x : 0).Sum();
    9086        isFeasible = violation == 0;
    9187        if (isFeasible || violation < minViolation) {
     
    9793    }
    9894
    99     protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) {
    100       return CreateSolution(random, demands, capacities,
    101         EvaluatorParameter.ActualValue,
     95    protected override IntegerVector CreateRandomSolution(IRandom random, GQAPInstance problemInstance) {
     96      return CreateSolution(random, problemInstance,
    10297        MaximumTriesParameter.ActualValue.Value,
    10398        CreateMostFeasibleSolutionParameter.ActualValue.Value,
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/SlackMinimizationSolutionCreator.cs

    r7970 r15504  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3535  [Item("SlackMinimizationSolutionCreator", "A heuristic that creates a solution to the Generalized Quadratic Assignment Problem by minimizing the amount of slack.")]
    3636  [StorableClass]
    37   public class SlackMinimizationSolutionCreator : GQAPStochasticSolutionCreator,
    38     IEvaluatorAwareGQAPOperator {
     37  public class SlackMinimizationSolutionCreator : GQAPStochasticSolutionCreator {
    3938
    4039    public IValueLookupParameter<IntValue> MaximumTriesParameter {
     
    5049      get { return (IValueLookupParameter<IntValue>)Parameters["RandomWalkLength"]; }
    5150    }
    52     public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
    53       get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
    54     }
    5551
    5652    [StorableConstructor]
     
    6359      Parameters.Add(new ValueLookupParameter<IntValue>("Depth", "How deep the algorithm should look forward.", new IntValue(3)));
    6460      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."));
    6661    }
    6762
     
    7065    }
    7166
    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     }
     67    public static IntegerVector CreateSolution(IRandom random, GQAPInstance problemInstance,
     68      int depth, int maximumTries, bool createMostFeasibleSolution, int randomWalkLength, CancellationToken cancel) {
     69      var capacities = problemInstance.Capacities;
     70      var demands = problemInstance.Demands;
    8171
    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) {
    8572      IntegerVector result = null;
    8673      bool isFeasible = false;
     
    125112          }
    126113        } else RandomFeasibleWalk(random, assignment, demands, slack, randomWalkLength);
    127         double violation = evaluator.EvaluateOverbooking(slack, capacities);
     114        double violation = slack.Select(x => x < 0 ? -x : 0).Sum();
    128115        isFeasible = violation == 0;
    129116        if (isFeasible || violation < minViolation) {
     
    182169    }
    183170
    184     protected override IntegerVector CreateRandomSolution(IRandom random, DoubleArray demands, DoubleArray capacities) {
    185       return CreateSolution(random, demands, capacities,
    186         EvaluatorParameter.ActualValue,
     171    protected override IntegerVector CreateRandomSolution(IRandom random, GQAPInstance problemInstance) {
     172      return CreateSolution(random, problemInstance,
    187173        DepthParameter.ActualValue.Value,
    188174        MaximumTriesParameter.ActualValue.Value,
Note: See TracChangeset for help on using the changeset viewer.