Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11669


Ignore:
Timestamp:
12/07/14 17:25:50 (9 years ago)
Author:
bgoldman
Message:

#2282 Code cleanup, added Deceptive Step Trap problem.

Location:
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EnumerableBoolEqualityComparer.cs

    r11667 r11669  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Text;
    26 using System.Threading.Tasks;
    2725
    2826namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
     
    4543        }
    4644      }
     45      // combine in any remaining content
     46      if (word > 1) {
     47        hash ^= word;
     48      }
    4749      return hash;
    4850    }
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3.csproj

    r11667 r11669  
    106106    <Compile Include="EnumerableBoolEqualityComparer.cs" />
    107107    <Compile Include="LinkageCrossover.cs" />
     108    <Compile Include="ListExtensions.cs" />
    108109    <Compile Include="Plugin.cs" />
     110    <Compile Include="Problems\DeceptiveStepTrapProblem.cs" />
    109111    <Compile Include="Problems\DeceptiveTrapProblem.cs" />
    110112    <Compile Include="HillClimber.cs" />
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs

    r11667 r11669  
    2727
    2828namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
    29   public static class ListExtensions {
    30     public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB) {
    31       T tmp = list[indexA];
    32       list[indexA] = list[indexB];
    33       list[indexB] = tmp;
    34       return list;
    35     }
    36     public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random) {
    37       return list.ShuffleInPlace(random, 0, list.Count - 1);
    38     }
    39     public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random, int maxIndex) {
    40       return list.ShuffleInPlace(random, 0, maxIndex);
    41     }
    42     public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random, int minIndex, int maxIndex) {
    43       for (int i = maxIndex; i > minIndex; i--) {
    44         int swapIndex = random.Next(minIndex, i + 1);
    45         list.Swap(i, swapIndex);
    46       }
    47       return list;
    48     }
    49 
    50     public static IEnumerable<T> ShuffleList<T>(this IList<T> source, IRandom random) {
    51       for (int i = source.Count - 1; i > 0; i--) {
    52         // Swap element "i" with a random earlier element (including itself)
    53         int swapIndex = random.Next(i + 1);
    54         source.Swap(i, swapIndex);
    55         yield return source[i];
    56       }
    57       yield return source[0];
    58     }
    59 
    60   }
     29 
    6130  public class LinkageTree {
    6231
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs

    r11668 r11669  
    4747    // Tracks all solutions in Pyramid for quick membership checks
    4848    private HashSet<bool[]> seen = new HashSet<bool[]>(new EnumerableBoolEqualityComparer());
    49 
     49   
     50    #region ParameterNames
    5051    private const string MaximumIterationsParameterName = "Maximum Iterations";
    51 
     52    private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
     53    private const string SeedParameterName = "Seed";
     54    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
     55    #endregion
     56   
     57    #region ParameterProperties
    5258    public IFixedValueParameter<IntValue> MaximumIterationsParameter {
    5359      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
    5460    }
    55 
     61    public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
     62      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
     63    }
     64    public IFixedValueParameter<IntValue> SeedParameter {
     65      get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
     66    }
     67    public FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
     68      get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
     69    }
     70    #endregion
     71
     72    #region Properties
    5673    public int MaximumIterations {
    5774      get { return MaximumIterationsParameter.Value.Value; }
     
    5976    }
    6077
    61     private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
    62 
    63     public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
    64       get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
    65     }
    66 
    6778    public int MaximumEvaluations {
    6879      get { return MaximumEvaluationsParameter.Value.Value; }
     
    7081    }
    7182
    72 
    73     private const string SeedParameterName = "Seed";
    74 
    75     public IFixedValueParameter<IntValue> SeedParameter {
    76       get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
    77     }
    78 
    7983    public int Seed {
    8084      get { return SeedParameter.Value.Value; }
     
    8286    }
    8387
    84     private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
    85 
    86     public FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
    87       get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
    88     }
    89 
    9088    public bool SetSeedRandomly {
    9189      get { return SetSeedRandomlyParameter.Value.Value; }
    9290      set { SetSeedRandomlyParameter.Value.Value = value; }
    9391    }
     92    #endregion
    9493
    9594    #region ResultsProperties
     
    128127      get { return ResultsQualities.Rows["Iteration Quality"]; }
    129128    }
    130 
    131129    #endregion
    132130
     
    183181
    184182    protected override void Run() {
     183      // Set up the algorithm
    185184      if (SetSeedRandomly) Seed = new System.Random().Next();
    186185      pyramid = new List<Population>();
     
    188187      random.Reset(Seed);
    189188      tracker = new EvaluationTracker(Problem, MaximumEvaluations);
     189
     190      // Set up the results display
    190191      Results.Add(new Result("Iterations", new IntValue(0)));
    191192      Results.Add(new Result("Evaluations", new IntValue(0)));
     
    199200      table.Rows.Add(iterationRows);
    200201      Results.Add(new Result("Qualities", table));
     202
     203      // Loop until iteration limit reached or canceled.
    201204      for (ResultsIterations = 0; ResultsIterations < MaximumIterations; ResultsIterations++) {
    202205        double fitness = double.NaN;
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Population.cs

    r11664 r11669  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using System.Threading.Tasks;
    2723using HeuristicLab.Core;
    2824
    2925namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
    3026  public class Population {
    31     private readonly List<bool[]> solutions = new List<bool[]>();
    32     private LinkageTree tree;
     27    public List<bool[]> Solutions {
     28      get;
     29      private set;
     30    }
    3331
    3432    public LinkageTree Tree {
    35       get { return tree; }
    36     }
    37 
    38     public List<bool[]> Solutions {
    39       get { return solutions; }
     33      get;
     34      private set;
    4035    }
    4136
    4237    public Population(int length, IRandom rand) {
    43       tree = new LinkageTree(length, rand);
     38      Solutions = new List<bool[]>();
     39      Tree = new LinkageTree(length, rand);
    4440    }
    4541    public void Add(bool[] solution) {
    46       solutions.Add(solution);
    47       tree.Add(solution);
     42      Solutions.Add(solution);
     43      Tree.Add(solution);
    4844    }
    4945  }
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/DeceptiveTrapProblem.cs

    r11666 r11669  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using System.Threading.Tasks;
    2724using HeuristicLab.Common;
    2825using HeuristicLab.Core;
     
    5956      set { TrapSizeParameter.Value.Value = value; }
    6057    }
     58
     59    protected virtual int TrapMaximum {
     60      get { return TrapSize;  }
     61    }
     62
    6163    public DeceptiveTrapProblem()
    6264      : base() {
    63       Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(5)));
     65      Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(7)));
     66      Length = 49;
     67    }
     68
     69    protected virtual int Score(bool[] individual, int trapIndex) {
     70      int result = 0;
     71      for (int index = trapIndex; index < trapIndex + TrapSize; index++) {
     72        if (individual[index]) result++;
     73      }
     74
     75      // Make it deceptive
     76      if (result < TrapSize) {
     77        result = TrapSize - result - 1;
     78      }
     79      return result;
    6480    }
    6581
     
    6884      int total = 0;
    6985      for (int i = 0; i < individual.Length; i += TrapSize) {
    70         int partial = 0;
    71         for (int index = i; index < i + TrapSize; index++) {
    72           if (individual[index]) partial++;
    73         }
    74 
    75         // Make it deceptive
    76         if (partial < TrapSize) {
    77           partial = TrapSize - partial - 1;
    78         }
    79         total += partial;
     86        total += Score(individual, i);
    8087      }
    81       return total;
     88      return (double)(total * TrapSize) / (TrapMaximum * individual.Length);
    8289    }
    8390  }
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/EvaluationTracker.cs

    r11666 r11669  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using System.Threading.Tasks;
    2724
    2825namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
    2926  public class EvaluationTracker : IBinaryVectorProblem {
    3027    private readonly IBinaryVectorProblem problem;
     28
    3129    private int maxEvaluations;
    3230
    33     private double bestQuality = double.NaN;
     31    #region Properties
    3432    public double BestQuality {
    35       get { return bestQuality; }
     33      get;
     34      private set;
    3635    }
    3736
    38     private int evaluations = 0;
    3937    public int Evaluations {
    40       get { return evaluations; }
     38      get;
     39      private set;
    4140    }
    4241
    43     private int bestFoundOnEvaluation = 0;
    4442    public int BestFoundOnEvaluation {
    45       get { return bestFoundOnEvaluation; }
     43      get;
     44      private set;
    4645    }
    4746
    4847    public bool[] BestSolution {
    49       get; private set;
     48      get;
     49      private set;
    5050    }
     51    #endregion
    5152
    5253    public EvaluationTracker(IBinaryVectorProblem problem, int maxEvaluations) {
     
    5455      this.maxEvaluations = maxEvaluations;
    5556      BestSolution = new bool[0];
     57      BestQuality = double.NaN;
     58      Evaluations = 0;
     59      BestFoundOnEvaluation = 0;
    5660    }
    5761
    5862    public double Evaluate(bool[] individual) {
    59       if (evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
    60       evaluations++;
     63      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
     64      Evaluations++;
    6165      double fitness = problem.Evaluate(individual);
    62       if (double.IsNaN(bestQuality) || problem.IsBetter(fitness, bestQuality)) {
    63         bestQuality = fitness;
     66      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
     67        BestQuality = fitness;
    6468        BestSolution = (bool[])individual.Clone();
    65         bestFoundOnEvaluation = evaluations;
     69        BestFoundOnEvaluation = Evaluations;
    6670      }
    6771      return fitness;
    6872    }
    6973
    70 
     74    #region ForwardedInteraface
    7175    public int Length {
    7276      get { return problem.Length; }
     
    7781    public bool IsBetter(double quality, double bestQuality) {
    7882      return problem.IsBetter(quality, bestQuality);
    79     }   
     83    }
     84    #endregion
    8085  }
    8186}
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/HIFFProblem.cs

    r11666 r11669  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using System.Threading.Tasks;
    2724using HeuristicLab.Common;
    2825using HeuristicLab.Core;
     
    4845
    4946    public HIFFProblem() : base() {
    50       Length = 32;
     47      Length = 64;
    5148    }
    5249
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/IBinaryVectorProblem.cs

    r11666 r11669  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using System.Threading.Tasks;
    27 
    2822namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
    2923  public interface IBinaryVectorProblem {
Note: See TracChangeset for help on using the changeset viewer.