Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/20/17 12:07:20 (8 years ago)
Author:
bwerth
Message:

#2745 fixed pausing and stopping and namespaces; Added MaximalDatasetSize and rudimentary ISurrogateAlgorithm-interface

Location:
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO

    • Property svn:global-ignores set to
      obj
    • Property svn:ignore set to
      HeuristicLab.Algorithms.EGO-3.4.csproj.user
      Plugin.cs
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EfficientGlobalOptimizationAlgorithm.cs

    r14741 r14768  
    2424using System.Linq;
    2525using System.Threading;
    26 using HeuristicLab.Algorithms.CMAEvolutionStrategy;
    2726using HeuristicLab.Algorithms.DataAnalysis;
    28 using HeuristicLab.Algorithms.EGO;
    2927using HeuristicLab.Analysis;
    3028using HeuristicLab.Common;
     
    3836using HeuristicLab.Random;
    3937
    40 namespace HeuristicLab.Problems.SurrogateProblem {
     38namespace HeuristicLab.Algorithms.EGO {
    4139  [StorableClass]
    4240  [Creatable(CreatableAttribute.Categories.Algorithms, Priority = 95)]
    4341  [Item("EfficientGlobalOptimizationAlgortihm", "Solves a problem by sequentially learning a model, solving a subproblem on the model and evaluating the best found solution for this subproblem.")]
    44   public class EfficientGlobalOptimizationAlgorithm : BasicAlgorithm {
     42  public class EfficientGlobalOptimizationAlgorithm : BasicAlgorithm, ISurrogateAlgorithm<RealVector> {
    4543    #region Basic-Alg-Essentials
    4644    public override bool SupportsPause => true;
     
    6462    private const string SeedParameterName = "Seed";
    6563    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
     64    private const string MaximalDataSetSizeParameterName = "MaximalDataSetSize";
    6665    #endregion
    6766
     
    9392    public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>;
    9493    public IFixedValueParameter<IntValue> SeedParameter => Parameters[SeedParameterName] as IFixedValueParameter<IntValue>;
    95     public FixedValueParameter<BoolValue> SetSeedRandomlyParameter => Parameters[SetSeedRandomlyParameterName] as FixedValueParameter<BoolValue>;
     94    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter => Parameters[SetSeedRandomlyParameterName] as IFixedValueParameter<BoolValue>;
     95    public IFixedValueParameter<IntValue> MaximalDataSetSizeParameter => Parameters[MaximalDataSetSizeParameterName] as IFixedValueParameter<IntValue>;
    9696    #endregion
    9797
    9898    #region Properties
     99
    99100    public int GenerationSize
    100101    {
     
    137138      get { return SetSeedRandomlyParameter.Value.Value; }
    138139    }
     140    public int MaximalDatasetSize
     141    {
     142      get { return MaximalDataSetSizeParameter.Value.Value; }
     143    }
     144
     145    private IEnumerable<Tuple<RealVector, double>> DataSamples
     146    {
     147      get
     148      {
     149        return Samples.Count > MaximalDatasetSize && MaximalDatasetSize > 0
     150          ? Samples.Skip(Samples.Count - MaximalDatasetSize)
     151          : Samples;
     152      }
     153    }
     154
    139155    #endregion
    140156
     
    144160    [Storable]
    145161    private List<Tuple<RealVector, double>> Samples;
     162    [Storable]
     163    private List<Tuple<RealVector, double>> InitialSamples;
    146164    #endregion
    147165
     
    201219      Random = cloner.Clone(Random);
    202220      if (original.Samples != null) Samples = original.Samples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();
     221      if (original.InitialSamples != null) Samples = original.InitialSamples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();
    203222      RegisterEventhandlers();
    204223    }
    205224    public override IDeepCloneable Clone(Cloner cloner) { return new EfficientGlobalOptimizationAlgorithm(this, cloner); }
    206225    public EfficientGlobalOptimizationAlgorithm() {
    207       var cmaes = new CMAEvolutionStrategy {
     226      var cmaes = new CMAEvolutionStrategy.CMAEvolutionStrategy {
    208227        MaximumGenerations = 300,
    209228        PopulationSize = 50
     
    226245      InfillCriterionParameter.ValidValues.Add(new ExpectedQuality());
    227246      InfillCriterionParameter.ValidValues.Add(new ConfidenceBound());
     247      Parameters.Add(new FixedValueParameter<IntValue>(MaximalDataSetSizeParameterName, "The maximum number of sample points used to generate the model. Set 0 or less to use always all samples ", new IntValue(-1)));
     248
    228249      SetInfillProblem();
    229250      RegisterEventhandlers();
     
    240261      if (SetSeedRandomly) SeedParameter.Value.Value = new System.Random().Next();
    241262      Random.Reset(Seed);
    242       Samples = new List<Tuple<RealVector, double>>();
     263      Samples = InitialSamples == null ? new List<Tuple<RealVector, double>>() : InitialSamples.ToList();
    243264
    244265      //results
    245266      Results.Add(new Result(IterationsResultName, new IntValue(0)));
    246       Results.Add(new Result(EvaluatedSoultionsResultName, new IntValue(0)));
     267      Results.Add(new Result(EvaluatedSoultionsResultName, new IntValue(Samples.Count)));
    247268      Results.Add(new Result(BestSolutionResultName, new RealVector(1)));
    248269      Results.Add(new Result(BestQualityResultName, new DoubleValue(Problem.Maximization ? double.MinValue : double.MaxValue)));
     
    254275      Results.Add(new Result(QualitiesChartResultName, table));
    255276
    256       ResultsQualities.Rows.Add(new DataRow("DEBUG:ModelBuildingIterations"));
    257 
    258277      //initial samples
    259       var points = EgoUtilities.GetUniformRandomDesign(InitialEvaluations, enc.Length, enc.Bounds, Random);
    260       foreach (var t in points) {
    261         Samples.Add(Evaluate(t));
    262         cancellationToken.ThrowIfCancellationRequested();
     278      if (Samples.Count < InitialEvaluations) {
     279        var points = EgoUtilities.GetUniformRandomDesign(InitialEvaluations - Samples.Count, enc.Length, enc.Bounds, Random);
     280        foreach (var t in points) {
     281          Samples.Add(Evaluate(t));
     282          cancellationToken.ThrowIfCancellationRequested();
     283        }
    263284      }
    264285
     
    269290      for (ResultsIterations = 0; ResultsIterations < MaximumIterations; ResultsIterations++) {
    270291        try {
    271           ResultsModel = BuildModel();
     292          ResultsModel = BuildModel(cancellationToken);
    272293          cancellationToken.ThrowIfCancellationRequested();
    273294          for (var i = 0; i < GenerationSize; i++) {
     
    285306    }
    286307
     308    public void SetInitialSamples(RealVector[] individuals, double[] qualities) {
     309      InitialSamples = individuals.Zip(qualities, (individual, d) => new Tuple<RealVector, double>(individual, d)).ToList();
     310    }
     311
    287312    #region Eventhandling
    288313    private void RegisterEventhandlers() {
     
    324349      if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();
    325350    }
    326     protected override void OnPaused() {
    327       base.OnPaused();
     351    public override void Pause() {
    328352      if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started) InfillOptimizationAlgorithm.Pause();
    329353      if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Pause();
    330 
    331     }
    332     protected override void OnStopped() {
    333       base.OnStopped();
    334       if (InfillOptimizationAlgorithm.ExecutionState != ExecutionState.Stopped) InfillOptimizationAlgorithm.Stop();
    335       if (RegressionAlgorithm.ExecutionState != ExecutionState.Stopped) RegressionAlgorithm.Stop();
     354      base.Pause();
     355    }
     356    public override void Stop() {
     357      if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started || InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Paused) InfillOptimizationAlgorithm.Stop();
     358      if (RegressionAlgorithm.ExecutionState == ExecutionState.Started || RegressionAlgorithm.ExecutionState == ExecutionState.Paused) RegressionAlgorithm.Stop();
     359      base.Stop();
    336360    }
    337361    protected override void OnProblemChanged() {
     
    351375      InfillOptimizationAlgorithm.Problem = infillProblem;
    352376    }
    353     private IRegressionSolution BuildModel() {
    354       var dataset = EgoUtilities.GetDataSet(Samples);
     377    private IRegressionSolution BuildModel(CancellationToken cancellationToken) {
     378      var dataset = EgoUtilities.GetDataSet(DataSamples.ToList());
    355379      var problemdata = new RegressionProblemData(dataset, dataset.VariableNames.Where(x => !x.Equals("output")), "output");
    356380      problemdata.TrainingPartition.Start = 0;
    357       problemdata.TrainingPartition.End = Samples.Count;
    358       problemdata.TestPartition.Start = Samples.Count;
    359       problemdata.TestPartition.End = Samples.Count;
     381      problemdata.TrainingPartition.End = dataset.Rows;
     382      problemdata.TestPartition.Start = dataset.Rows;
     383      problemdata.TestPartition.End = dataset.Rows;
    360384
    361385      //train
     
    369393        solution = results.Select(x => x.Value).OfType<IRegressionSolution>().SingleOrDefault();
    370394        r2 = solution?.TrainingRSquared ?? 0;
     395        cancellationToken.ThrowIfCancellationRequested();
    371396      }
    372       ResultsQualities.Rows["DEBUG:ModelBuildingIterations"].Values.Add(i);
     397
    373398      if (solution == null) throw new ArgumentException("The Algorithm did not return a Model");
     399      RegressionAlgorithm.Runs.Clear();
    374400      return solution;
    375401    }
     
    381407      if (infillProblem.Problem != Problem) throw new ArgumentException("Expensive real problem is not correctly set in InfillProblem. Problem with Eventhandling?");
    382408      infillProblem.RegressionSolution = ResultsModel;
     409      if (MaximalDatasetSize > 0 && MaximalDatasetSize < Samples.Count) { infillProblem.Encoding.Bounds = EgoUtilities.GetBoundingBox(DataSamples.Select(x => x.Item1)); }
    383410
    384411      RealVector bestVector = null;
     
    406433    }
    407434    private Tuple<RealVector, double> Evaluate(RealVector point) {
    408       var scope = new Scope();
    409       scope.Variables.Add(new Variable(Problem.Encoding.Name, point));
    410       var ind = new SingleEncodingIndividual(Problem.Encoding, scope);
    411       var q = Problem.Evaluate(ind, Random);
    412       return new Tuple<RealVector, double>(point, q);
     435      return new Tuple<RealVector, double>(point, Problem.Evaluate(GetIndividual(point), Random));
    413436    }
    414437    private void Analyze() {
     
    422445      ResultsQualitiesIteration.Values.Add(Samples[Samples.Count - 1].Item2);
    423446      ResultsQualitiesWorst.Values.Add(Samples[Problem.Maximization ? min : max].Item2);
     447      Problem.Analyze(Samples.Select(x => GetIndividual(x.Item1)).ToArray(), Samples.Select(x => x.Item2).ToArray(), Results, Random);
     448    }
     449    private Individual GetIndividual(RealVector r) {
     450      var scope = new Scope();
     451      scope.Variables.Add(new Variable(Problem.Encoding.Name, r));
     452      return new SingleEncodingIndividual(Problem.Encoding, scope);
    424453    }
    425454    #endregion
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EgoUtilities.cs

    r14741 r14768  
    5252
    5353    public static ResultCollection SyncRunSubAlgorithm(IAlgorithm alg, int random) {
     54
     55      if (alg.Parameters.ContainsKey("SetSeedRandomly") && alg.Parameters.ContainsKey("Seed")) {
     56        var setSeed = alg.Parameters["SetSeedRandomly"].ActualValue as BoolValue;
     57        var seed = alg.Parameters["Seed"].ActualValue as IntValue;
     58        if (seed == null || setSeed == null) throw new ArgumentException("wrong SeedParametertypes");
     59        setSeed.Value = false;
     60        seed.Value = random;
     61
     62      }
     63
     64
    5465      EventWaitHandle trigger = new AutoResetEvent(false);
    5566      Exception ex = null;
     
    103114      return new Dataset(names.Concat(new[] { "output" }).ToArray(), data);
    104115    }
     116    public static DoubleMatrix GetBoundingBox(IEnumerable<RealVector> vectors) {
     117      DoubleMatrix res = null;
     118      foreach (var vector in vectors)
     119        if (res == null) {
     120          res = new DoubleMatrix(vector.Length, 2);
     121          for (var i = 0; i < vector.Length; i++)
     122            res[i, 0] = res[i, 1] = vector[i];
     123        } else
     124          for (var i = 0; i < vector.Length; i++) {
     125            res[i, 0] = Math.Min(vector[i], res[i, 0]);
     126            res[i, 1] = Math.Max(vector[i], res[i, 1]);
     127          }
     128      return res;
     129    }
    105130  }
    106131}
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/HeuristicLab.Algorithms.EGO-3.4.csproj

    r14741 r14768  
    6767      <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Encodings.RealVectorEncoding-3.3.dll</HintPath>
    6868    </Reference>
     69    <Reference Include="HeuristicLab.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec" />
    6970    <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    7071      <SpecificVersion>False</SpecificVersion>
     
    112113    <Compile Include="InfillCriteria\InfillCriterionBase.cs" />
    113114    <Compile Include="InfillCriteria\ExpectedImprovement.cs" />
     115    <Compile Include="Interfaces\ISurrogateAlgorithm.cs" />
    114116    <Compile Include="Interfaces\IInfillCriterion.cs" />
    115117    <Compile Include="Plugin.cs" />
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/InfillCriteria/ConfidenceBound.cs

    r14741 r14768  
    5252      get { return ConfidenceWeightParameter.Value.Value; }
    5353    }
    54 
    5554    #endregion
    5655
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/InfillCriteria/ExpectedImprovement.cs

    r14741 r14768  
    5353      get { return ExploitationWeightParameter.Value.Value; }
    5454    }
    55 
    5655    #endregion
    5756
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/InfillCriteria/RobustImprovement.cs

    r14741 r14768  
    5252      get { return ConfidenceWeightParameter.Value.Value; }
    5353    }
    54 
    5554    #endregion
    5655
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Plugin.cs.frame

    r14741 r14768  
    3838  [PluginDependency("HeuristicLab.Data", "3.3")]
    3939  [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")]
     40  [PluginDependency("HeuristicLab.Operators","3.3")]
    4041  [PluginDependency("HeuristicLab.Optimization","3.3")]
    4142  [PluginDependency("HeuristicLab.Parameters","3.3")]
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Problems/InfillProblem.cs

    r14741 r14768  
    2121
    2222using System;
    23 using HeuristicLab.Algorithms.EGO;
    2423using HeuristicLab.Common;
    2524using HeuristicLab.Core;
     
    3029using HeuristicLab.Problems.DataAnalysis;
    3130
    32 namespace HeuristicLab.Problems.SurrogateProblem {
     31namespace HeuristicLab.Algorithms.EGO {
    3332  [StorableClass]
    3433  [Item("InfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")]
    3534  public sealed class InfillProblem : SingleObjectiveBasicProblem<RealVectorEncoding> {
    3635
    37     [Storable]
    3836    public override bool Maximization => true;
    3937
     
    6058        if (problem == null) return;
    6159        Encoding = problem.Encoding as RealVectorEncoding;
     60        SolutionCreator = new UniformRandomRealVectorCreator();//ignore Problem specific Solution Creation
    6261        if (Encoding == null) throw new ArgumentException("EGO can not be performed on non-RealVectorEncodings");
    6362      }
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Properties

    • Property svn:ignore set to
      AssemblyInfo.cs
Note: See TracChangeset for help on using the changeset viewer.