- 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/BinaryMemPR.cs
r14420 r14450 24 24 using System.Linq; 25 25 using System.Threading; 26 using HeuristicLab.Algorithms.MemPR.Interfaces; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Encodings.BinaryVectorEncoding; 29 30 using HeuristicLab.Optimization; 30 using HeuristicLab.Optimization.LocalSearch;31 using HeuristicLab.Optimization.SolutionModel;32 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 32 using HeuristicLab.PluginInfrastructure; … … 38 37 [StorableClass] 39 38 [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)] 40 public class BinaryMemPR : MemPRAlgorithm< BinaryVector, BinaryMemPRContext, BinarySingleSolutionMemPRContext> {39 public class BinaryMemPR : MemPRAlgorithm<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> { 41 40 private const double UncommonBitSubsetMutationProbabilityMagicConst = 0.05; 42 41 … … 45 44 protected BinaryMemPR(BinaryMemPR original, Cloner cloner) : base(original, cloner) { } 46 45 public BinaryMemPR() { 47 foreach (var trainer in ApplicationManager.Manager.GetInstances<I BinarySolutionModelTrainer<BinaryMemPRContext>>())46 foreach (var trainer in ApplicationManager.Manager.GetInstances<ISolutionModelTrainer<BinaryMemPRPopulationContext>>()) 48 47 SolutionModelTrainerParameter.ValidValues.Add(trainer); 49 48 50 foreach (var localSearch in ApplicationManager.Manager.GetInstances<IBinaryLocalSearch<BinarySingleSolutionMemPRContext>>()) { 51 // only use local search operators that can deal with a restricted solution space 52 var lsType = localSearch.GetType(); 53 var genTypeDef = lsType.GetGenericTypeDefinition(); 54 // TODO: By convention, context type must be put last 55 // TODO: Fails with non-generic types 56 if (genTypeDef.GetGenericArguments().Last().GetGenericParameterConstraints().Any(x => typeof(IBinarySolutionSubspaceContext).IsAssignableFrom(x))) { 57 localSearch.EvaluateFunc = EvaluateFunc; 58 LocalSearchParameter.ValidValues.Add(localSearch); 59 } 49 foreach (var localSearch in ApplicationManager.Manager.GetInstances<ILocalSearch<BinaryMemPRSolutionContext>>()) { 50 LocalSearchParameter.ValidValues.Add(localSearch); 60 51 } 61 52 } … … 63 54 public override IDeepCloneable Clone(Cloner cloner) { 64 55 return new BinaryMemPR(this, cloner); 65 }66 67 protected double EvaluateFunc(BinaryVector solution) {68 var scope = ToScope(solution);69 Evaluate(scope, CancellationToken.None);70 return scope.Fitness;71 56 } 72 57 … … 98 83 } 99 84 100 protected override ISolutionSubspace CalculateSubspace(IEnumerable<BinaryVector> solutions, bool inverse = false) {85 protected override ISolutionSubspace<BinaryVector> CalculateSubspace(IEnumerable<BinaryVector> solutions, bool inverse = false) { 101 86 var pop = solutions.ToList(); 102 87 var N = pop[0].Length; … … 112 97 } 113 98 114 protected override ISingleObjectiveSolutionScope<BinaryVector> Create(CancellationToken token) { 115 var child = ToScope(null); 116 RunOperator(Problem.SolutionCreator, child, token); 117 return child; 118 } 119 120 protected override void TabuWalk(ISingleObjectiveSolutionScope<BinaryVector> scope, int steps, CancellationToken token, ISolutionSubspace subspace = null) { 99 protected override void TabuWalk(ISingleObjectiveSolutionScope<BinaryVector> scope, int steps, CancellationToken token, ISolutionSubspace<BinaryVector> subspace = null) { 121 100 var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null; 122 101 if (double.IsNaN(scope.Fitness)) Evaluate(scope, token); … … 183 162 var lastbp = 0; 184 163 for (var i = 0; i < code.Length; i++) { 185 if (code[i] == p2Code[i]) continue; // common bit186 164 if (bp % 2 == 1) { 187 165 code[i] = p2Code[i]; 188 166 } 189 if (Context.Random.Next(code.Length) < i - lastbp ) {167 if (Context.Random.Next(code.Length) < i - lastbp + 1) { 190 168 bp = (bp + 1) % 2; 191 169 lastbp = i; … … 195 173 } 196 174 197 protected override void Mutate(ISingleObjectiveSolutionScope<BinaryVector> offspring, CancellationToken token, ISolutionSubspace subspace = null) {175 protected override void Mutate(ISingleObjectiveSolutionScope<BinaryVector> offspring, CancellationToken token, ISolutionSubspace<BinaryVector> subspace = null) { 198 176 var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null; 199 177 offspring.Fitness = double.NaN;
Note: See TracChangeset
for help on using the changeset viewer.