Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/22/17 01:32:13 (6 years ago)
Author:
abeham
Message:

#1614: fixed bugs

Location:
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASP.cs

    r15553 r15558  
    108108    }
    109109    [Storable]
    110     private FixedValueParameter<PercentValue> minimumDifferenceParameter;
    111     public IFixedValueParameter<PercentValue> MinimumDifferenceParameter {
     110    private FixedValueParameter<IntValue> minimumDifferenceParameter;
     111    public IFixedValueParameter<IntValue> MinimumDifferenceParameter {
    112112      get { return minimumDifferenceParameter; }
    113113    }
     
    121121      set { seedParameter.Value.Value = value; }
    122122    }
     123    public int EliteSetSize {
     124      get { return eliteSetSizeParameter.Value.Value; }
     125      set { eliteSetSizeParameter.Value.Value = value; }
     126    }
    123127    public int MinimumEliteSetSize {
    124128      get { return minimiumEliteSetSizeParameter.Value.Value; }
    125129      set { minimiumEliteSetSizeParameter.Value.Value = value; }
    126130    }
    127     public int EliteSetSize {
    128       get { return eliteSetSizeParameter.Value.Value; }
    129       set { eliteSetSizeParameter.Value.Value = value; }
     131    public int MaximumIterations {
     132      get { return maximumIterationsParameter.Value.Value; }
     133      set { maximumIterationsParameter.Value.Value = value; }
     134    }
     135    public int MaximumLocalSearchIterations {
     136      get { return maximumLocalSearchIterationsParameter.Value.Value; }
     137      set { maximumLocalSearchIterationsParameter.Value.Value = value; }
    130138    }
    131139    public double CandidateSizeFactor {
     
    141149      set { oneMoveProbabilityParameter.Value.Value = value; }
    142150    }
    143     public double MinimumDifference {
     151    public int MinimumDifference {
    144152      get { return minimumDifferenceParameter.Value.Value; }
    145153      set { minimumDifferenceParameter.Value.Value = value; }
     
    174182      Parameters.Add(maximumCandidateListSizeParameter = new FixedValueParameter<IntValue>("MaximumCandidateListSize", "The maximum number of candidates that should be found in each step.", new IntValue(10)));
    175183      Parameters.Add(oneMoveProbabilityParameter = new FixedValueParameter<PercentValue>("OneMoveProbability", "The probability for performing a 1-move, which is the opposite of performing a 2-move.", new PercentValue(.5)));
    176       Parameters.Add(minimumDifferenceParameter = new FixedValueParameter<PercentValue>("MinimumDifference", "The minimum amount of difference between two solutions so that they are both accepted in the elite set.", new PercentValue(1e-7)));
     184      Parameters.Add(minimumDifferenceParameter = new FixedValueParameter<IntValue>("MinimumDifference", "The minimum amount of difference between two solutions so that they are both accepted in the elite set.", new IntValue(4)));
    177185      Problem = new GQAP();
    178186    }
     
    252260            var fit = Problem.ProblemInstance.ToSingleObjective(pi_prime.Evaluation);
    253261            double[] similarities = context.Population.Select(x => HammingSimilarityCalculator.CalculateSimilarity(x.Solution.Assignment, pi_prime.Assignment)).ToArray();
    254             if (similarities.Max() <= 1.0 - MinimumDifference) { // cond. 2 of line 13 in Algorithm 1
     262            if (similarities.Max() <= 1.0 - (MinimumDifference / (double)pi_prime.Assignment.Length)) { // cond. 2 of line 13 in Algorithm 1
    255263              var replacement = context.Population.Select((v, i) => new { V = v, Index = i })
    256264                                          .Where(x => x.V.Fitness >= fit).ToArray();
     
    277285        }
    278286
    279         context.Iterations++;
    280 
    281287        IResult result;
    282288        if (Results.TryGetValue("Iterations", out result))
     
    294300
    295301        context.RunOperator(analyzerParameter.Value, context.Scope, cancellationToken);
     302
     303        context.Iterations++;
    296304      }
    297305    }
     
    299307    private bool IsSufficientlyDifferent(IntegerVector vec) {
    300308      return context.Population.All(x =>
    301         HammingSimilarityCalculator.CalculateSimilarity(x.Solution.Assignment, vec) <= 1.0 - MinimumDifference
     309        HammingSimilarityCalculator.CalculateSimilarity(vec, x.Solution.Assignment) <= 1.0 - (MinimumDifference / (double)vec.Length)
    302310      );
    303311    }
     
    329337      var localSearchEvaluations = 0;
    330338      ApproximateLocalSearch.Apply(context.Random, pi_prime, MaximumCandidateListSize,
    331         OneMoveProbability, 1000, Problem.ProblemInstance, out localSearchEvaluations);
     339        OneMoveProbability, MaximumLocalSearchIterations, Problem.ProblemInstance, out localSearchEvaluations);
    332340      context.EvaluatedSolutions += localSearchEvaluations;
    333341    }
    334342
    335343    private bool StoppingCriterion() {
    336       return context.Iterations > MaximumIterationsParameter.Value.Value;
     344      return context.Iterations > MaximumIterations;
    337345    }
    338346  }
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASPContext.cs

    r15553 r15558  
    174174      scope.Variables.Add(new Variable("Evaluation", code.Evaluation));
    175175      return scope;
    176     }
    177 
    178     public double Evaluate(GQAPSolution solution, CancellationToken token) {
    179       var solScope = ToScope(solution);
    180       Evaluate(solScope, token);
    181       return solScope.Fitness;
    182     }
    183 
    184     public void Evaluate(ISingleObjectiveSolutionScope<GQAPSolution> solScope, CancellationToken token) {
    185       var pdef = Problem as ISingleObjectiveProblemDefinition;
    186       if (pdef != null) {
    187         var ind = new SingleEncodingIndividual(pdef.Encoding, solScope);
    188         solScope.Fitness = pdef.Evaluate(ind, Random);
    189       } else {
    190         RunOperator(Problem.Evaluator, solScope, token);
    191       }
    192     }
    193 
    194     public GRASPSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<GQAPSolution> solution) {
    195       return new GRASPSolutionContext(this, solution);
    196176    }
    197177
     
    274254    #endregion
    275255  }
    276 
    277   [Item("GRASP+PR (GQAP) SolutionContext", "SolutionContext for GRASP+PR (GQAP).")]
    278   [StorableClass]
    279   public sealed class GRASPSolutionContext : ParameterizedNamedItem, IExecutionContext {
    280 
    281     private GRASPContext parent;
    282     public IExecutionContext Parent {
    283       get { return parent; }
    284       set { throw new InvalidOperationException("Cannot set the parent of a single-solution context."); }
    285     }
    286 
    287     [Storable]
    288     private ISingleObjectiveSolutionScope<GQAPSolution> scope;
    289     public IScope Scope {
    290       get { return scope; }
    291     }
    292 
    293     IKeyedItemCollection<string, IParameter> IExecutionContext.Parameters {
    294       get { return Parameters; }
    295     }
    296 
    297     public GQAP Problem {
    298       get { return parent.Problem; }
    299     }
    300     public bool Maximization {
    301       get { return parent.Maximization; }
    302     }
    303 
    304     public double BestQuality {
    305       get { return parent.BestQuality; }
    306       set { parent.BestQuality = value; }
    307     }
    308 
    309     public GQAPSolution BestSolution {
    310       get { return parent.BestSolution; }
    311       set { parent.BestSolution = value; }
    312     }
    313 
    314     public IRandom Random {
    315       get { return parent.Random; }
    316     }
    317 
    318     [Storable]
    319     private IValueParameter<IntValue> evaluatedSolutions;
    320     public int EvaluatedSolutions {
    321       get { return evaluatedSolutions.Value.Value; }
    322       set { evaluatedSolutions.Value.Value = value; }
    323     }
    324 
    325     [Storable]
    326     private IValueParameter<IntValue> iterations;
    327     public int Iterations {
    328       get { return iterations.Value.Value; }
    329       set { iterations.Value.Value = value; }
    330     }
    331 
    332     [StorableConstructor]
    333     private GRASPSolutionContext(bool deserializing) : base(deserializing) { }
    334     private GRASPSolutionContext(GRASPSolutionContext original, Cloner cloner)
    335       : base(original, cloner) {
    336       scope = cloner.Clone(original.scope);
    337       evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
    338       iterations = cloner.Clone(original.iterations);
    339     }
    340     public GRASPSolutionContext(GRASPContext baseContext, ISingleObjectiveSolutionScope<GQAPSolution> solution) {
    341       parent = baseContext;
    342       scope = solution;
    343 
    344       Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
    345       Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
    346     }
    347 
    348     public override IDeepCloneable Clone(Cloner cloner) {
    349       return new GRASPSolutionContext(this, cloner);
    350     }
    351 
    352     public void IncrementEvaluatedSolutions(int byEvaluations) {
    353       if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions.");
    354       EvaluatedSolutions += byEvaluations;
    355     }
    356     public double Evaluate(GQAPSolution solution, CancellationToken token) {
    357       return parent.Evaluate(solution, token);
    358     }
    359 
    360     public void Evaluate(ISingleObjectiveSolutionScope<GQAPSolution> solScope, CancellationToken token) {
    361       parent.Evaluate(solScope, token);
    362     }
    363 
    364     #region IExecutionContext members
    365     public IAtomicOperation CreateOperation(IOperator op) {
    366       return new Core.ExecutionContext(this, op, Scope);
    367     }
    368 
    369     public IAtomicOperation CreateOperation(IOperator op, IScope s) {
    370       return new Core.ExecutionContext(this, op, s);
    371     }
    372 
    373     public IAtomicOperation CreateChildOperation(IOperator op) {
    374       return new Core.ExecutionContext(this, op, Scope);
    375     }
    376 
    377     public IAtomicOperation CreateChildOperation(IOperator op, IScope s) {
    378       return new Core.ExecutionContext(this, op, s);
    379     }
    380     #endregion
    381   }
    382256}
Note: See TracChangeset for help on using the changeset viewer.