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/BinaryMemPR.cs

    r14420 r14450  
    2424using System.Linq;
    2525using System.Threading;
     26using HeuristicLab.Algorithms.MemPR.Interfaces;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
    2829using HeuristicLab.Encodings.BinaryVectorEncoding;
    2930using HeuristicLab.Optimization;
    30 using HeuristicLab.Optimization.LocalSearch;
    31 using HeuristicLab.Optimization.SolutionModel;
    3231using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3332using HeuristicLab.PluginInfrastructure;
     
    3837  [StorableClass]
    3938  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)]
    40   public class BinaryMemPR : MemPRAlgorithm<BinaryVector, BinaryMemPRContext, BinarySingleSolutionMemPRContext> {
     39  public class BinaryMemPR : MemPRAlgorithm<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> {
    4140    private const double UncommonBitSubsetMutationProbabilityMagicConst = 0.05;
    4241   
     
    4544    protected BinaryMemPR(BinaryMemPR original, Cloner cloner) : base(original, cloner) { }
    4645    public BinaryMemPR() {
    47       foreach (var trainer in ApplicationManager.Manager.GetInstances<IBinarySolutionModelTrainer<BinaryMemPRContext>>())
     46      foreach (var trainer in ApplicationManager.Manager.GetInstances<ISolutionModelTrainer<BinaryMemPRPopulationContext>>())
    4847        SolutionModelTrainerParameter.ValidValues.Add(trainer);
    4948     
    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);
    6051      }
    6152    }
     
    6354    public override IDeepCloneable Clone(Cloner cloner) {
    6455      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;
    7156    }
    7257
     
    9883    }
    9984
    100     protected override ISolutionSubspace CalculateSubspace(IEnumerable<BinaryVector> solutions, bool inverse = false) {
     85    protected override ISolutionSubspace<BinaryVector> CalculateSubspace(IEnumerable<BinaryVector> solutions, bool inverse = false) {
    10186      var pop = solutions.ToList();
    10287      var N = pop[0].Length;
     
    11297    }
    11398
    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) {
    121100      var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null;
    122101      if (double.IsNaN(scope.Fitness)) Evaluate(scope, token);
     
    183162      var lastbp = 0;
    184163      for (var i = 0; i < code.Length; i++) {
    185         if (code[i] == p2Code[i]) continue; // common bit
    186164        if (bp % 2 == 1) {
    187165          code[i] = p2Code[i];
    188166        }
    189         if (Context.Random.Next(code.Length) < i - lastbp) {
     167        if (Context.Random.Next(code.Length) < i - lastbp + 1) {
    190168          bp = (bp + 1) % 2;
    191169          lastbp = i;
     
    195173    }
    196174
    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) {
    198176      var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null;
    199177      offspring.Fitness = double.NaN;
Note: See TracChangeset for help on using the changeset viewer.