Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/03/16 00:32:09 (8 years ago)
Author:
abeham
Message:

#2701: working on MemPR implementation

File:
1 edited

Legend:

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

    r14420 r14450  
    2020#endregion
    2121
     22using System.Threading;
     23using HeuristicLab.Algorithms.MemPR.Interfaces;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
     26using HeuristicLab.Encodings.Binary.LocalSearch;
    2427using HeuristicLab.Encodings.BinaryVectorEncoding;
    2528using HeuristicLab.Optimization;
    26 using HeuristicLab.Optimization.LocalSearch;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2830
    29 namespace HeuristicLab.Encodings.Binary.LocalSearch {
    30   [Item("Exhaustive Bitflip Local (Subspace) Search (binary)", "")]
     31namespace HeuristicLab.Algorithms.MemPR.Binary.LocalSearch {
     32  [Item("Exhaustive Bitflip Local (Subspace) Search (binary)", "", ExcludeGenericTypeInfo = true)]
    3133  [StorableClass]
    32   public class ExhaustiveBitflipSubspace<TContext> : ExhaustiveBitflipOperator, IBinaryLocalSearch<TContext>
    33       where TContext : ISingleObjectiveSolutionContext<BinaryVector>, IStochasticContext, IMaximizationContext,
    34                        IEvaluatedSolutionsContext, IIterationsManipulationContext, IBinarySolutionSubspaceContext {
     34  public class ExhaustiveBitflipSubspace<TContext> : NamedItem, ILocalSearch<TContext>
     35      where TContext : ISingleSolutionHeuristicAlgorithmContext<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector>, IBinaryVectorSubspaceContext {
    3536
    3637    [StorableConstructor]
    3738    protected ExhaustiveBitflipSubspace(bool deserializing) : base(deserializing) { }
    3839    protected ExhaustiveBitflipSubspace(ExhaustiveBitflipSubspace<TContext> original, Cloner cloner) : base(original, cloner) { }
    39     public ExhaustiveBitflipSubspace() { }
     40    public ExhaustiveBitflipSubspace() {
     41      Name = ItemName;
     42      Description = ItemDescription;
     43    }
    4044
    4145    public override IDeepCloneable Clone(Cloner cloner) {
     
    4448
    4549    public void Optimize(TContext context) {
     50      var evalWrapper = new EvaluationWrapper(context);
    4651      var quality = context.Solution.Fitness;
    4752      try {
    48         var result = Heuristic.ExhaustiveBitFlipSearch(context.Random, context.Solution.Solution, ref quality,
    49           context.Maximization, EvaluateFunc, CancellationToken, context.Subspace != null ? context.Subspace.Subspace : null);
    50         context.EvaluatedSolutions = result.Item1;
     53        var result = ExhaustiveBitflip.Optimize(context.Random, context.Solution.Solution, ref quality,
     54          context.Problem.Maximization, evalWrapper.Evaluate, CancellationToken.None, context.Subspace.Subspace);
     55        context.IncrementEvaluatedSolutions(result.Item1);
    5156        context.Iterations = result.Item2;
    5257      } finally {
     
    5459      }
    5560    }
     61
     62    public sealed class EvaluationWrapper {
     63      private readonly TContext context;
     64      private readonly ISingleObjectiveSolutionScope<BinaryVector> scope;
     65      private readonly SingleEncodingIndividual individual;
     66
     67      public EvaluationWrapper(TContext context) {
     68        this.context = context;
     69        // don't clone the solution, which is thrown away again
     70        var cloner = new Cloner();
     71        cloner.RegisterClonedObject(context.Solution.Solution, null);
     72        this.scope = (ISingleObjectiveSolutionScope<BinaryVector>)context.Solution.Clone(cloner);
     73        this.individual = new SingleEncodingIndividual(context.Problem.Encoding, this.scope);
     74      }
     75
     76      public double Evaluate(BinaryVector b) {
     77        scope.Solution = b;
     78        return context.Problem.Evaluate(individual, null);
     79      }
     80    }
    5681  }
    5782}
Note: See TracChangeset for help on using the changeset viewer.