Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/09/17 00:36:20 (8 years ago)
Author:
abeham
Message:

#2701:

  • Added alternating bits binary test Problem
  • Refactored MemPR to work with programmable problem in current trunk
  • fixed a bug in permutation MemPR when crossover doesn't assign an offspring
Location:
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/BinaryMemPR.cs

    r14551 r14552  
    3838  [StorableClass]
    3939  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)]
    40   public class BinaryMemPR : MemPRAlgorithm<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> {
     40  public class BinaryMemPR : MemPRAlgorithm<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> {
    4141    [StorableConstructor]
    4242    protected BinaryMemPR(bool deserializing) : base(deserializing) { }
     
    6666    }
    6767
    68     protected override ISingleObjectiveSolutionScope<BinaryVector> ToScope(BinaryVector code, double fitness = double.NaN) {
    69       var creator = Problem.SolutionCreator as IBinaryVectorCreator;
    70       if (creator == null) throw new InvalidOperationException("Can only solve binary encoded problems with MemPR (binary)");
    71       return new SingleObjectiveSolutionScope<BinaryVector>(code, creator.BinaryVectorParameter.ActualName, fitness, Problem.Evaluator.QualityParameter.ActualName) {
    72         Parent = Context.Scope
    73       };
    74     }
    75 
    7668    protected override ISolutionSubspace<BinaryVector> CalculateSubspace(IEnumerable<BinaryVector> solutions, bool inverse = false) {
    7769      var pop = solutions.ToList();
     
    9284      var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null;
    9385      if (double.IsNaN(scope.Fitness)) {
    94         Evaluate(scope, token);
     86        Context.Evaluate(scope, token);
    9587        evaluations++;
    9688      }
     
    10496      var order = Enumerable.Range(0, N).Where(x => subset == null || subset[x]).Shuffle(Context.Random).ToArray();
    10597
    106       var bound = Problem.Maximization ? Context.Population.Max(x => x.Fitness) : Context.Population.Min(x => x.Fitness);
     98      var bound = Context.Maximization ? Context.Population.Max(x => x.Fitness) : Context.Population.Min(x => x.Fitness);
    10799      var range = Math.Abs(bound - Context.LocalOptimaLevel);
    108100      if (range.IsAlmost(0)) range = Math.Abs(bound * 0.05);
     
    122114          var before = currentScope.Fitness;
    123115          current[idx] = !current[idx];
    124           Evaluate(currentScope, token);
     116          Context.Evaluate(currentScope, token);
    125117          evaluations++;
    126118          var after = currentScope.Fitness;
     
    133125            }
    134126          }
    135           var diff = Problem.Maximization ? after - before : before - after;
     127          var diff = Context.Maximization ? after - before : before - after;
    136128          if (diff > 0) moved = true;
    137129          else {
     
    158150      var N = p1.Solution.Length;
    159151
    160       var probe = ToScope((BinaryVector)p1.Solution.Clone());
     152      var probe = Context.ToScope((BinaryVector)p1.Solution.Clone());
    161153
    162154      var cache = new HashSet<BinaryVector>(new BinaryVectorEqualityComparer());
     
    179171          continue;
    180172        }
    181         Evaluate(probe, token);
     173        Context.Evaluate(probe, token);
    182174        evaluations++;
    183175        cache.Add(c);
     
    210202          var idx = order[i];
    211203          child[idx] = !child[idx]; // move
    212           Evaluate(childScope, token);
     204          Context.Evaluate(childScope, token);
    213205          evaluations++;
    214206          var s = childScope.Fitness;
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/BinaryMemPRContext.cs

    r14450 r14552  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Algorithms.MemPR.Interfaces;
    2324using HeuristicLab.Common;
     
    3132  [Item("MemPR Population Context (binary)", "MemPR population context for binary encoded problems.")]
    3233  [StorableClass]
    33   public sealed class BinaryMemPRPopulationContext : MemPRPopulationContext<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> {
     34  public sealed class BinaryMemPRPopulationContext : MemPRPopulationContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> {
    3435
    3536    [StorableConstructor]
     
    4748      return new BinaryMemPRSolutionContext(this, solution);
    4849    }
     50
     51    public override ISingleObjectiveSolutionScope<BinaryVector> ToScope(BinaryVector code, double fitness = double.NaN) {
     52      var creator = Problem.SolutionCreator as IBinaryVectorCreator;
     53      if (creator == null) throw new InvalidOperationException("MemPR (binary) context expects a problem with an IBinaryVectorCreator as solution creator.");
     54      return new SingleObjectiveSolutionScope<BinaryVector>(code, creator.BinaryVectorParameter.ActualName, fitness, Problem.Evaluator.QualityParameter.ActualName) {
     55        Parent = Scope
     56      };
     57    }
    4958  }
    5059
    5160  [Item("MemPR Solution Context (binary)", "MemPR solution context for binary encoded problems.")]
    5261  [StorableClass]
    53   public sealed class BinaryMemPRSolutionContext : MemPRSolutionContext<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext>, IBinaryVectorSubspaceContext {
     62  public sealed class BinaryMemPRSolutionContext : MemPRSolutionContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext>, IBinaryVectorSubspaceContext {
    5463
    5564    [Storable]
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/ExhaustiveBitflip.cs

    r14450 r14552  
    3333  [Item("Exhaustive Bitflip Local Search (binary)", "", ExcludeGenericTypeInfo = true)]
    3434  [StorableClass]
    35   public class ExhaustiveBitflip<TContext> : NamedItem, ILocalSearch<TContext> where TContext : ISingleSolutionHeuristicAlgorithmContext<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector> {
     35  public class ExhaustiveBitflip<TContext> : NamedItem, ILocalSearch<TContext>
     36    where TContext : ISingleSolutionHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector>,
     37                     IEvaluationServiceContext<BinaryVector> {
    3638   
    3739    [StorableConstructor]
     
    4850
    4951    public void Optimize(TContext context) {
    50       var evalWrapper = new EvaluationWrapper<BinaryVector>(context.Problem, context.Solution);
    5152      var quality = context.Solution.Fitness;
    5253      try {
    5354        var result = ExhaustiveBitflip.Optimize(context.Random, context.Solution.Solution, ref quality,
    54           context.Problem.Maximization, evalWrapper.Evaluate, CancellationToken.None);
     55          context.Maximization, context.Evaluate, CancellationToken.None);
    5556        context.IncrementEvaluatedSolutions(result.Item1);
    5657        context.Iterations = result.Item2;
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/ExhaustiveBitflipSubspace.cs

    r14466 r14552  
    3434  [StorableClass]
    3535  public class ExhaustiveBitflipSubspace<TContext> : NamedItem, ILocalSearch<TContext>
    36       where TContext : ISingleSolutionHeuristicAlgorithmContext<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector>, IBinaryVectorSubspaceContext {
     36      where TContext : ISingleSolutionHeuristicAlgorithmContext<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector>,
     37                       IBinaryVectorSubspaceContext, IEvaluationServiceContext<BinaryVector> {
    3738
    3839    [StorableConstructor]
     
    4950
    5051    public void Optimize(TContext context) {
    51       var evalWrapper = new EvaluationWrapper<BinaryVector>(context.Problem, context.Solution);
    5252      var quality = context.Solution.Fitness;
    5353      try {
    5454        var result = ExhaustiveBitflip.Optimize(context.Random, context.Solution.Solution, ref quality,
    55           context.Problem.Maximization, evalWrapper.Evaluate, CancellationToken.None, context.Subspace.Subspace);
     55          context.Problem.Maximization, context.Evaluate, CancellationToken.None, context.Subspace.Subspace);
    5656        context.IncrementEvaluatedSolutions(result.Item1);
    5757        context.Iterations = result.Item2;
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/StaticAPI/ExhaustiveBitflip.cs

    r14450 r14552  
    3030namespace HeuristicLab.Encodings.Binary.LocalSearch {
    3131  public static class ExhaustiveBitflip {
    32     public static Tuple<int, int> Optimize(IRandom random, BinaryVector solution, ref double quality, bool maximization, Func<BinaryVector, double> evalFunc, CancellationToken token, bool[] subspace = null) {
    33       if (double.IsNaN(quality)) quality = evalFunc(solution);
     32    public static Tuple<int, int> Optimize(IRandom random, BinaryVector solution, ref double quality, bool maximization, Func<BinaryVector, CancellationToken, double> evalFunc, CancellationToken token, bool[] subspace = null) {
     33      if (double.IsNaN(quality)) quality = evalFunc(solution, token);
    3434      var improved = false;
    3535      var order = Enumerable.Range(0, solution.Length).Shuffle(random).ToArray();
     
    4747          // bitflip the solution
    4848          solution[idx] = !solution[idx];
    49           var after = evalFunc(solution);
     49          var after = evalFunc(solution, token);
    5050          evaluations++;
    5151          if (FitnessComparer.IsBetter(maximization, after, quality)) {
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/SolutionModel/Univariate/BiasedModelTrainer.cs

    r14450 r14552  
    3434  [StorableClass]
    3535  public class BiasedModelTrainer<TContext> : ParameterizedNamedItem, ISolutionModelTrainer<TContext>
    36     where TContext : IPopulationBasedHeuristicAlgorithmContext<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector>, ISolutionModelContext<BinaryVector> {
     36    where TContext : IPopulationBasedHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector>, ISolutionModelContext<BinaryVector> {
    3737   
    3838    [Storable]
     
    5858
    5959    public void TrainModel(TContext context) {
    60       context.Model = Trainer.TrainBiased(ModelBias, context.Random, context.Problem.Maximization, context.Population.Select(x => x.Solution), context.Population.Select(x => x.Fitness));
     60      context.Model = Trainer.TrainBiased(ModelBias, context.Random, context.Maximization, context.Population.Select(x => x.Solution), context.Population.Select(x => x.Fitness));
    6161    }
    6262  }
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/SolutionModel/Univariate/UnbiasedModelTrainer.cs

    r14450 r14552  
    3232  [StorableClass]
    3333  public class UniasedModelTrainer<TContext> : NamedItem, ISolutionModelTrainer<TContext>
    34     where TContext : IPopulationBasedHeuristicAlgorithmContext<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector>, ISolutionModelContext<BinaryVector> {
     34    where TContext : IPopulationBasedHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector>, ISolutionModelContext<BinaryVector> {
    3535   
    3636    [StorableConstructor]
Note: See TracChangeset for help on using the changeset viewer.