- Timestamp:
- 01/09/17 00:36:20 (8 years ago)
- 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 38 38 [StorableClass] 39 39 [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> { 41 41 [StorableConstructor] 42 42 protected BinaryMemPR(bool deserializing) : base(deserializing) { } … … 66 66 } 67 67 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.Scope73 };74 }75 76 68 protected override ISolutionSubspace<BinaryVector> CalculateSubspace(IEnumerable<BinaryVector> solutions, bool inverse = false) { 77 69 var pop = solutions.ToList(); … … 92 84 var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null; 93 85 if (double.IsNaN(scope.Fitness)) { 94 Evaluate(scope, token);86 Context.Evaluate(scope, token); 95 87 evaluations++; 96 88 } … … 104 96 var order = Enumerable.Range(0, N).Where(x => subset == null || subset[x]).Shuffle(Context.Random).ToArray(); 105 97 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); 107 99 var range = Math.Abs(bound - Context.LocalOptimaLevel); 108 100 if (range.IsAlmost(0)) range = Math.Abs(bound * 0.05); … … 122 114 var before = currentScope.Fitness; 123 115 current[idx] = !current[idx]; 124 Evaluate(currentScope, token);116 Context.Evaluate(currentScope, token); 125 117 evaluations++; 126 118 var after = currentScope.Fitness; … … 133 125 } 134 126 } 135 var diff = Problem.Maximization ? after - before : before - after;127 var diff = Context.Maximization ? after - before : before - after; 136 128 if (diff > 0) moved = true; 137 129 else { … … 158 150 var N = p1.Solution.Length; 159 151 160 var probe = ToScope((BinaryVector)p1.Solution.Clone());152 var probe = Context.ToScope((BinaryVector)p1.Solution.Clone()); 161 153 162 154 var cache = new HashSet<BinaryVector>(new BinaryVectorEqualityComparer()); … … 179 171 continue; 180 172 } 181 Evaluate(probe, token);173 Context.Evaluate(probe, token); 182 174 evaluations++; 183 175 cache.Add(c); … … 210 202 var idx = order[i]; 211 203 child[idx] = !child[idx]; // move 212 Evaluate(childScope, token);204 Context.Evaluate(childScope, token); 213 205 evaluations++; 214 206 var s = childScope.Fitness; -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/BinaryMemPRContext.cs
r14450 r14552 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Algorithms.MemPR.Interfaces; 23 24 using HeuristicLab.Common; … … 31 32 [Item("MemPR Population Context (binary)", "MemPR population context for binary encoded problems.")] 32 33 [StorableClass] 33 public sealed class BinaryMemPRPopulationContext : MemPRPopulationContext< SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> {34 public sealed class BinaryMemPRPopulationContext : MemPRPopulationContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> { 34 35 35 36 [StorableConstructor] … … 47 48 return new BinaryMemPRSolutionContext(this, solution); 48 49 } 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 } 49 58 } 50 59 51 60 [Item("MemPR Solution Context (binary)", "MemPR solution context for binary encoded problems.")] 52 61 [StorableClass] 53 public sealed class BinaryMemPRSolutionContext : MemPRSolutionContext< SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext>, IBinaryVectorSubspaceContext {62 public sealed class BinaryMemPRSolutionContext : MemPRSolutionContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext>, IBinaryVectorSubspaceContext { 54 63 55 64 [Storable] -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/ExhaustiveBitflip.cs
r14450 r14552 33 33 [Item("Exhaustive Bitflip Local Search (binary)", "", ExcludeGenericTypeInfo = true)] 34 34 [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> { 36 38 37 39 [StorableConstructor] … … 48 50 49 51 public void Optimize(TContext context) { 50 var evalWrapper = new EvaluationWrapper<BinaryVector>(context.Problem, context.Solution);51 52 var quality = context.Solution.Fitness; 52 53 try { 53 54 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); 55 56 context.IncrementEvaluatedSolutions(result.Item1); 56 57 context.Iterations = result.Item2; -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/ExhaustiveBitflipSubspace.cs
r14466 r14552 34 34 [StorableClass] 35 35 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> { 37 38 38 39 [StorableConstructor] … … 49 50 50 51 public void Optimize(TContext context) { 51 var evalWrapper = new EvaluationWrapper<BinaryVector>(context.Problem, context.Solution);52 52 var quality = context.Solution.Fitness; 53 53 try { 54 54 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); 56 56 context.IncrementEvaluatedSolutions(result.Item1); 57 57 context.Iterations = result.Item2; -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/StaticAPI/ExhaustiveBitflip.cs
r14450 r14552 30 30 namespace HeuristicLab.Encodings.Binary.LocalSearch { 31 31 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); 34 34 var improved = false; 35 35 var order = Enumerable.Range(0, solution.Length).Shuffle(random).ToArray(); … … 47 47 // bitflip the solution 48 48 solution[idx] = !solution[idx]; 49 var after = evalFunc(solution );49 var after = evalFunc(solution, token); 50 50 evaluations++; 51 51 if (FitnessComparer.IsBetter(maximization, after, quality)) { -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/SolutionModel/Univariate/BiasedModelTrainer.cs
r14450 r14552 34 34 [StorableClass] 35 35 public class BiasedModelTrainer<TContext> : ParameterizedNamedItem, ISolutionModelTrainer<TContext> 36 where TContext : IPopulationBasedHeuristicAlgorithmContext< SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector>, ISolutionModelContext<BinaryVector> {36 where TContext : IPopulationBasedHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector>, ISolutionModelContext<BinaryVector> { 37 37 38 38 [Storable] … … 58 58 59 59 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)); 61 61 } 62 62 } -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/SolutionModel/Univariate/UnbiasedModelTrainer.cs
r14450 r14552 32 32 [StorableClass] 33 33 public class UniasedModelTrainer<TContext> : NamedItem, ISolutionModelTrainer<TContext> 34 where TContext : IPopulationBasedHeuristicAlgorithmContext< SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector>, ISolutionModelContext<BinaryVector> {34 where TContext : IPopulationBasedHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, BinaryVector>, ISolutionModelContext<BinaryVector> { 35 35 36 36 [StorableConstructor]
Note: See TracChangeset
for help on using the changeset viewer.