Changeset 14450 for branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/ExhaustiveBitflipSubspace.cs
- Timestamp:
- 12/03/16 00:32:09 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/ExhaustiveBitflipSubspace.cs
r14420 r14450 20 20 #endregion 21 21 22 using System.Threading; 23 using HeuristicLab.Algorithms.MemPR.Interfaces; 22 24 using HeuristicLab.Common; 23 25 using HeuristicLab.Core; 26 using HeuristicLab.Encodings.Binary.LocalSearch; 24 27 using HeuristicLab.Encodings.BinaryVectorEncoding; 25 28 using HeuristicLab.Optimization; 26 using HeuristicLab.Optimization.LocalSearch;27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 30 29 namespace HeuristicLab. Encodings.Binary.LocalSearch {30 [Item("Exhaustive Bitflip Local (Subspace) Search (binary)", "" )]31 namespace HeuristicLab.Algorithms.MemPR.Binary.LocalSearch { 32 [Item("Exhaustive Bitflip Local (Subspace) Search (binary)", "", ExcludeGenericTypeInfo = true)] 31 33 [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 { 35 36 36 37 [StorableConstructor] 37 38 protected ExhaustiveBitflipSubspace(bool deserializing) : base(deserializing) { } 38 39 protected ExhaustiveBitflipSubspace(ExhaustiveBitflipSubspace<TContext> original, Cloner cloner) : base(original, cloner) { } 39 public ExhaustiveBitflipSubspace() { } 40 public ExhaustiveBitflipSubspace() { 41 Name = ItemName; 42 Description = ItemDescription; 43 } 40 44 41 45 public override IDeepCloneable Clone(Cloner cloner) { … … 44 48 45 49 public void Optimize(TContext context) { 50 var evalWrapper = new EvaluationWrapper(context); 46 51 var quality = context.Solution.Fitness; 47 52 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); 51 56 context.Iterations = result.Item2; 52 57 } finally { … … 54 59 } 55 60 } 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 } 56 81 } 57 82 }
Note: See TracChangeset
for help on using the changeset viewer.