Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/16/15 14:39:15 (10 years ago)
Author:
mkommend
Message:

#2282: Implemented stop button in PPP and adapted to new BasicAlgorithm.

Location:
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
Files:
1 deleted
3 edited

Legend:

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

    r11669 r11791  
    103103  </ItemGroup>
    104104  <ItemGroup>
    105     <Compile Include="AlgorithmBase.cs" />
    106105    <Compile Include="EnumerableBoolEqualityComparer.cs" />
    107106    <Compile Include="LinkageCrossover.cs" />
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs

    r11672 r11791  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     25using System.Threading;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Core;
    2628using HeuristicLab.Data;
    27 using HeuristicLab.Encodings.BinaryVectorEncoding;
    2829using HeuristicLab.Optimization;
    2930using HeuristicLab.Parameters;
     
    3738  [Creatable("Parameterless Population Pyramid")]
    3839  // In the GECCO paper, Section 2.1
    39   public class HillClimber : AlgorithmBase {
     40  public class HillClimber : BasicAlgorithm {
    4041    [Storable]
    4142    private IRandom random;
    4243
    4344    private const string IterationsParameterName = "Iterations";
     45
     46    public override Type ProblemType {
     47      get { return typeof(BinaryVectorProblem); }
     48    }
     49    public new BinaryVectorProblem Problem {
     50      get { return (BinaryVectorProblem)base.Problem; }
     51      set { base.Problem = value; }
     52    }
    4453
    4554    public IFixedValueParameter<IntValue> IterationsParameter {
     
    6675      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
    6776    }
    68     protected override void Run() {
     77    protected override void Run(CancellationToken cancellationToken) {
    6978      var BestQuality = new DoubleValue(double.NaN);
    7079      Results.Add(new Result("Best quality", BestQuality));
  • branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs

    r11681 r11791  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using System.Threading.Tasks;
     24using System.Threading;
    2725using HeuristicLab.Analysis;
    2826using HeuristicLab.Common;
     
    3937  [StorableClass]
    4038  [Creatable("Parameterless Population Pyramid")]
    41 
    42   public class ParameterlessPopulationPyramid : AlgorithmBase {
     39  public class ParameterlessPopulationPyramid : BasicAlgorithm {
     40    public override Type ProblemType {
     41      get { return typeof(BinaryVectorProblem); }
     42    }
     43    public new BinaryVectorProblem Problem {
     44      get { return (BinaryVectorProblem)base.Problem; }
     45      set { base.Problem = value; }
     46    }
     47
    4348    private readonly IRandom random = new MersenneTwister();
    4449    private List<Population> pyramid;
     
    5156    private const string MaximumIterationsParameterName = "Maximum Iterations";
    5257    private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
     58    private const string MaximumRuntimeParameterName = "Maximum Runtime";
    5359    private const string SeedParameterName = "Seed";
    5460    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
     
    6167    public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
    6268      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
     69    }
     70    public IFixedValueParameter<IntValue> MaximumRuntimeParameter {
     71      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumRuntimeParameterName]; }
    6372    }
    6473    public IFixedValueParameter<IntValue> SeedParameter {
     
    7584      set { MaximumIterationsParameter.Value.Value = value; }
    7685    }
    77 
    7886    public int MaximumEvaluations {
    7987      get { return MaximumEvaluationsParameter.Value.Value; }
    8088      set { MaximumEvaluationsParameter.Value.Value = value; }
    8189    }
    82 
     90    public int MaximumRuntime {
     91      get { return MaximumRuntimeParameter.Value.Value; }
     92      set { MaximumRuntimeParameter.Value.Value = value; }
     93    }
    8394    public int Seed {
    8495      get { return SeedParameter.Value.Value; }
    8596      set { SeedParameter.Value.Value = value; }
    8697    }
    87 
    8898    public bool SetSeedRandomly {
    8999      get { return SetSeedRandomlyParameter.Value.Value; }
     
    151161    public ParameterlessPopulationPyramid() {
    152162      Parameters.Add(new FixedValueParameter<IntValue>(MaximumIterationsParameterName, "", new IntValue(Int32.MaxValue)));
    153       Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(10000)));
     163      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue)));
     164      Parameters.Add(new FixedValueParameter<IntValue>(MaximumRuntimeParameterName, "The maximum runtime in seconds after which the algorithm stops. Use -1 to specify no limit for the runtime", new IntValue(3600)));
    154165      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
    155166      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
     167    }
     168
     169    protected override void OnExecutionTimeChanged() {
     170      base.OnExecutionTimeChanged();
     171      if (CancellationTokenSource == null) return;
     172      if (MaximumRuntime == -1) return;
     173      if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();
    156174    }
    157175
     
    190208    }
    191209
    192     protected override void Run() {
     210    protected override void Run(CancellationToken cancellationToken) {
    193211      // Set up the algorithm
    194212      if (SetSeedRandomly) Seed = new System.Random().Next();
     
    225243        try {
    226244          fitness = iterate();
    227         }
    228         catch (OperationCanceledException) {
    229           throw;
     245          cancellationToken.ThrowIfCancellationRequested();
    230246        }
    231247        finally {
Note: See TracChangeset for help on using the changeset viewer.