source:
branches/2780_SAPBA/SAPBA.patch
@
18000
Last change on this file since 18000 was 16108, checked in by bwerth, 6 years ago | |
---|---|
File size: 76.1 KB |
-
.
-
.vs/HeuristicLab.Algorithms.SAPBA/v14/.suo
Property changes on: . ___________________________________________________________________ Added: svn:global-ignores ## -0,0 +1 ## +.vs Added: svn:ignore ## -0,0 +1 ## +.vs Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
-
.vs/Sliding
Property changes on: .vs/HeuristicLab.Algorithms.SAPBA/v14/.suo ___________________________________________________________________ Deleted: svn:mime-type ## -1 +0,0 ## -application/octet-stream \ No newline at end of property Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
-
.vs/HeuristicLab.Algorithms.SAPBA/v14/.suo
Property changes on: .vs/Sliding Window GP/v14/.suo ___________________________________________________________________ Deleted: svn:mime-type ## -1 +0,0 ## -application/octet-stream \ No newline at end of property Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
-
.vs/HeuristicLab.Algorithms.SAPBA/v14/.suo
Property changes on: .vs/HeuristicLab.Algorithms.SAPBA/v14/.suo ___________________________________________________________________ Deleted: svn:mime-type ## -1 +0,0 ## -application/octet-stream \ No newline at end of property Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
-
.vs/HeuristicLab.Algorithms.SAPBA/v14/.suo
Property changes on: .vs/HeuristicLab.Algorithms.SAPBA/v14/.suo ___________________________________________________________________ Deleted: svn:mime-type ## -1 +0,0 ## -application/octet-stream \ No newline at end of property Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
-
.vs/Sliding
Property changes on: .vs/HeuristicLab.Algorithms.SAPBA/v14/.suo ___________________________________________________________________ Deleted: svn:mime-type ## -1 +0,0 ## -application/octet-stream \ No newline at end of property Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
-
.vs/Sliding
Property changes on: .vs/Sliding Window GP/v14/.suo ___________________________________________________________________ Deleted: svn:mime-type ## -1 +0,0 ## -application/octet-stream \ No newline at end of property Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
-
.vs/Sliding
Property changes on: .vs/Sliding Window GP/v14/.suo ___________________________________________________________________ Deleted: svn:mime-type ## -1 +0,0 ## -application/octet-stream \ No newline at end of property Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
-
HeuristicLab.Algorithms.SAPBA/HeuristicLab.Algorithms.SAPBA-3.4.csproj
Property changes on: .vs/Sliding Window GP/v14/.suo ___________________________________________________________________ Deleted: svn:mime-type ## -1 +0,0 ## -application/octet-stream \ No newline at end of property
30 30 <WarningLevel>4</WarningLevel> 31 31 </PropertyGroup> 32 32 <ItemGroup> 33 <Reference Include="HeuristicLab.Algorithms.CMAEvolutionStrategy-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">34 <SpecificVersion>False</SpecificVersion>35 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.CMAEvolutionStrategy-3.4.dll</HintPath>36 </Reference>37 33 <Reference Include="HeuristicLab.Algorithms.DataAnalysis-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 38 34 <SpecificVersion>False</SpecificVersion> 39 35 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.DataAnalysis-3.4.dll</HintPath> 40 36 </Reference> 41 <Reference Include="HeuristicLab.Algorithms.E volutionStrategy-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">37 <Reference Include="HeuristicLab.Algorithms.EGO-3.4, Version=3.4.0.0, Culture=neutral, processorArchitecture=MSIL"> 42 38 <SpecificVersion>False</SpecificVersion> 43 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.E volutionStrategy-3.3.dll</HintPath>39 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.EGO-3.4.dll</HintPath> 44 40 </Reference> 45 41 <Reference Include="HeuristicLab.Algorithms.GeneticAlgorithm-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 46 42 <SpecificVersion>False</SpecificVersion> 47 43 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.GeneticAlgorithm-3.3.dll</HintPath> 48 44 </Reference> 45 <Reference Include="HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 46 <SpecificVersion>False</SpecificVersion> 47 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy-3.3.dll</HintPath> 48 </Reference> 49 49 <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 50 50 <SpecificVersion>False</SpecificVersion> 51 51 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath> … … 58 58 <SpecificVersion>False</SpecificVersion> 59 59 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 60 60 </Reference> 61 <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">62 <SpecificVersion>False</SpecificVersion>63 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Common.Resources-3.3.dll</HintPath>64 </Reference>65 61 <Reference Include="HeuristicLab.Core-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 66 62 <SpecificVersion>False</SpecificVersion> 67 63 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> … … 74 70 <SpecificVersion>False</SpecificVersion> 75 71 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Encodings.RealVectorEncoding-3.3.dll</HintPath> 76 72 </Reference> 77 <Reference Include="HeuristicLab.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec" /> 73 <Reference Include="HeuristicLab.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 74 <SpecificVersion>False</SpecificVersion> 75 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath> 76 </Reference> 78 77 <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 79 78 <SpecificVersion>False</SpecificVersion> 80 79 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath> … … 99 98 <SpecificVersion>False</SpecificVersion> 100 99 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath> 101 100 </Reference> 102 <Reference Include="HeuristicLab.Problems.Instances.DataAnalysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">103 <SpecificVersion>False</SpecificVersion>104 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Problems.Instances.DataAnalysis-3.3.dll</HintPath>105 </Reference>106 <Reference Include="HeuristicLab.Problems.Instances.DataAnalysis.Views-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">107 <SpecificVersion>False</SpecificVersion>108 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Problems.Instances.DataAnalysis.Views-3.3.dll</HintPath>109 </Reference>110 <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">111 <SpecificVersion>False</SpecificVersion>112 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>113 </Reference>114 101 <Reference Include="System" /> 115 102 <Reference Include="System.Core" /> 116 103 <Reference Include="System.Windows.Forms" /> … … 122 109 <Reference Include="System.Xml" /> 123 110 </ItemGroup> 124 111 <ItemGroup> 125 <Compile Include="EgoUtilities.cs" /> 126 <Compile Include="Strategies\IndividualStrategy.cs" /> 127 <Compile Include="Strategies\GenerationalStrategy.cs" /> 112 <Compile Include="Operators\FixedSolutionCreator.cs" /> 113 <Compile Include="SapbaUtilities.cs" /> 114 <Compile Include="Strategies\LamarckianStrategy.cs" /> 115 <Compile Include="Strategies\InfillStrategy.cs" /> 128 116 <Compile Include="SurrogateAssistedPopulationBasedAlgorithm.cs" /> 129 117 <Compile Include="Interfaces\ISurrogateStrategy.cs" /> 130 118 <Compile Include="Interfaces\ISurrogateAlgorithm.cs" /> -
HeuristicLab.Algorithms.SAPBA/Interfaces/ISurrogateStrategy.cs
19 19 */ 20 20 #endregion 21 21 22 using System.Threading;23 22 using HeuristicLab.Core; 24 23 using HeuristicLab.Encodings.RealVectorEncoding; 25 24 using HeuristicLab.Optimization; … … 29 28 double Evaluate(RealVector r, IRandom random); 30 29 void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random); 31 30 void Initialize(SurrogateAssistedPopulationBasedAlgorithm algorithm); 32 void UpdateCancellation(CancellationToken cancellationToken);31 bool Maximization(); 33 32 } 34 33 } -
HeuristicLab.Algorithms.SAPBA/Operators/FixedSolutionCreator.cs
1 using HeuristicLab.Common; 2 using HeuristicLab.Core; 3 using HeuristicLab.Data; 4 using HeuristicLab.Encodings.RealVectorEncoding; 5 using HeuristicLab.Optimization; 6 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 7 8 namespace HeuristicLab.Algorithms.SAPBA.Operators { 9 [Item("FixedRealVectorCreator", "An operator which creates a new real vector cloned from a single Point")] 10 [StorableClass] 11 public class FixedRealVectorCreator : RealVectorCreator, IStrategyParameterCreator { 12 [Storable] 13 private RealVector Point; 14 15 [StorableConstructor] 16 protected FixedRealVectorCreator(bool deserializing) : base(deserializing) { } 17 protected FixedRealVectorCreator(FixedRealVectorCreator original, Cloner cloner) : base(original, cloner) { 18 Point = cloner.Clone(original.Point); 19 } 20 public FixedRealVectorCreator(RealVector r) : base() { 21 Point = r; 22 } 23 public override IDeepCloneable Clone(Cloner cloner) { return new FixedRealVectorCreator(this, cloner); } 24 protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) { 25 return (RealVector)Point.Clone(); 26 } 27 } 28 29 } 30 -
HeuristicLab.Algorithms.SAPBA/Operators/FixedSolutionCreator.cs
1 using HeuristicLab.Common; 2 using HeuristicLab.Core; 3 using HeuristicLab.Data; 4 using HeuristicLab.Encodings.RealVectorEncoding; 5 using HeuristicLab.Optimization; 6 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 7 8 namespace HeuristicLab.Algorithms.SAPBA.Operators { 9 [Item("FixedRealVectorCreator", "An operator which creates a new real vector cloned from a single Point")] 10 [StorableClass] 11 public class FixedRealVectorCreator : RealVectorCreator, IStrategyParameterCreator { 12 [Storable] 13 private RealVector Point; 14 15 [StorableConstructor] 16 protected FixedRealVectorCreator(bool deserializing) : base(deserializing) { } 17 protected FixedRealVectorCreator(FixedRealVectorCreator original, Cloner cloner) : base(original, cloner) { 18 Point = cloner.Clone(original.Point); 19 } 20 public FixedRealVectorCreator(RealVector r) : base() { 21 Point = r; 22 } 23 public override IDeepCloneable Clone(Cloner cloner) { return new FixedRealVectorCreator(this, cloner); } 24 protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) { 25 return (RealVector)Point.Clone(); 26 } 27 } 28 29 } 30 -
HeuristicLab.Algorithms.SAPBA/Plugin.cs
25 25 using HeuristicLab.PluginInfrastructure; 26 26 27 27 namespace HeuristicLab.Algorithms.SAPBA { 28 [Plugin("HeuristicLab.Algorithms.SAPBA", "3.4.5.1489 3")]28 [Plugin("HeuristicLab.Algorithms.SAPBA", "3.4.5.14894")] 29 29 [PluginFile("HeuristicLab.Algorithms.SAPBA-3.4.dll", PluginFileType.Assembly)] 30 [PluginFile("displayModelFrame.html", PluginFileType.Data)]31 [PluginDependency("HeuristicLab.Algorithms.CMAEvolutionStrategy", "3.4")]32 30 [PluginDependency("HeuristicLab.Algorithms.DataAnalysis", "3.4")] 31 [PluginDependency("HeuristicLab.Algorithms.EGO", "3.4")] 32 [PluginDependency("HeuristicLab.Algorithms.GeneticAlgorithm", "3.3")] 33 [PluginDependency("HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy", "3.3")] 33 34 [PluginDependency("HeuristicLab.Analysis", "3.3")] 34 35 [PluginDependency("HeuristicLab.Collections", "3.3")] 35 36 [PluginDependency("HeuristicLab.Common", "3.3")] 36 [PluginDependency("HeuristicLab.Common.Resources", "3.3")]37 37 [PluginDependency("HeuristicLab.Core", "3.3")] 38 38 [PluginDependency("HeuristicLab.Data", "3.3")] 39 39 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 40 [PluginDependency("HeuristicLab.Operators","3.3")]41 40 [PluginDependency("HeuristicLab.Optimization","3.3")] 42 41 [PluginDependency("HeuristicLab.Parameters","3.3")] 43 42 [PluginDependency("HeuristicLab.Persistence","3.3")] 44 43 [PluginDependency("HeuristicLab.Problems.DataAnalysis", "3.4")] 45 [PluginDependency("HeuristicLab.Problems.Instances", "3.3")]46 [PluginDependency("HeuristicLab.Random", "3.3")]47 44 public class HeuristicLabProblemsDataAnalysisSymbolicViewsPlugin : PluginBase { 48 45 } 49 46 } -
HeuristicLab.Algorithms.SAPBA/Plugin.cs.frame
27 27 namespace HeuristicLab.Algorithms.SAPBA { 28 28 [Plugin("HeuristicLab.Algorithms.SAPBA", "3.4.5.$WCREV$")] 29 29 [PluginFile("HeuristicLab.Algorithms.SAPBA-3.4.dll", PluginFileType.Assembly)] 30 [PluginFile("displayModelFrame.html", PluginFileType.Data)]31 [PluginDependency("HeuristicLab.Algorithms.CMAEvolutionStrategy", "3.4")]32 30 [PluginDependency("HeuristicLab.Algorithms.DataAnalysis", "3.4")] 31 [PluginDependency("HeuristicLab.Algorithms.EGO", "3.4")] 32 [PluginDependency("HeuristicLab.Algorithms.GeneticAlgorithm", "3.3")] 33 [PluginDependency("HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy", "3.3")] 33 34 [PluginDependency("HeuristicLab.Analysis", "3.3")] 34 35 [PluginDependency("HeuristicLab.Collections", "3.3")] 35 36 [PluginDependency("HeuristicLab.Common", "3.3")] 36 [PluginDependency("HeuristicLab.Common.Resources", "3.3")]37 37 [PluginDependency("HeuristicLab.Core", "3.3")] 38 38 [PluginDependency("HeuristicLab.Data", "3.3")] 39 39 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 40 [PluginDependency("HeuristicLab.Operators","3.3")]41 40 [PluginDependency("HeuristicLab.Optimization","3.3")] 42 41 [PluginDependency("HeuristicLab.Parameters","3.3")] 43 42 [PluginDependency("HeuristicLab.Persistence","3.3")] 44 43 [PluginDependency("HeuristicLab.Problems.DataAnalysis", "3.4")] 45 [PluginDependency("HeuristicLab.Problems.Instances", "3.3")]46 [PluginDependency("HeuristicLab.Random", "3.3")]47 44 public class HeuristicLabProblemsDataAnalysisSymbolicViewsPlugin : PluginBase { 48 45 } 49 46 } -
HeuristicLab.Algorithms.SAPBA/Problems/SurrogateProblem.cs
1 using HeuristicLab.Common; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 * 5 * This file is part of HeuristicLab. 6 * 7 * HeuristicLab is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * HeuristicLab is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 #endregion 21 22 using System; 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 2 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 3 27 using HeuristicLab.Encodings.RealVectorEncoding; 4 28 using HeuristicLab.Optimization; 5 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 6 30 7 31 namespace HeuristicLab.Algorithms.SAPBA { 8 32 [StorableClass] 9 [Item("Surrogate problem (single-objective)", " Wrapper for a problem that allows surrogate models to mitigate some of the work")]33 [Item("Surrogate problem (single-objective)", "A problem that uses a Surrogate Strategy to emulate an expensive problem")] 10 34 public class SurrogateProblem : SingleObjectiveBasicProblem<RealVectorEncoding> { 11 35 public override bool Maximization => Strategy?.Maximization() ?? false; 12 36 [Storable] 13 37 private ISurrogateStrategy Strategy; 14 38 … … 18 42 [StorableHook(HookType.AfterDeserialization)] 19 43 private void AfterDeserialization() { } 20 44 protected SurrogateProblem(SurrogateProblem original, Cloner cloner) : base(original, cloner) { 21 Strategy = original?.Strategy;45 Strategy = cloner.Clone(original.Strategy); 22 46 } 23 public override IDeepCloneable Clone(Cloner cloner) { return new SurrogateProblem(this, cloner); }24 public SurrogateProblem() {47 public override IDeepCloneable Clone(Cloner cloner) { 48 return new SurrogateProblem(this, cloner); 25 49 } 50 public SurrogateProblem() { } 26 51 #endregion 27 52 28 53 public override double Evaluate(Individual individual, IRandom random) { … … 32 57 base.Analyze(individuals, qualities, results, random); 33 58 Strategy.Analyze(individuals, qualities, results, random); 34 59 } 60 public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) { 61 var bounds = Encoding.Bounds; 62 var michalewiczIteration = 0; 63 while (true) { 64 var neighbour = individual.Copy(); 65 var r = neighbour.RealVector(); 66 switch (random.Next(5)) { 67 case 0: UniformOnePositionManipulator.Apply(random, r, bounds); break; 68 case 1: FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break; 69 case 2: MichalewiczNonUniformAllPositionsManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break; 70 case 3: MichalewiczNonUniformOnePositionManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break; 71 case 4: BreederGeneticAlgorithmManipulator.Apply(random, r, bounds, new DoubleValue(0.1)); break; 72 default: throw new NotImplementedException(); 73 } 74 yield return neighbour; 75 michalewiczIteration %= 10000; 76 } 77 } 35 78 36 public override bool Maximization { get; } 37 38 public void SetStrategy(ISurrogateStrategy strategy) { 79 public void Initialize(SingleObjectiveBasicProblem<IEncoding> expensiveProblem, ISurrogateStrategy strategy) { 80 if (expensiveProblem == null) return; 81 var enc = (RealVectorEncoding)expensiveProblem.Encoding; 82 Encoding.Bounds = enc.Bounds; 83 Encoding.Length = enc.Length; 84 SolutionCreator = expensiveProblem.SolutionCreator; 39 85 Strategy = strategy; 40 86 } 41 public void SetProblem(SingleObjectiveBasicProblem<IEncoding> expensiveProblem) {42 if (expensiveProblem != null) Encoding = expensiveProblem.Encoding as RealVectorEncoding;43 }44 45 87 } 46 88 } 47 No newline at end of file -
HeuristicLab.Algorithms.SAPBA/Properties/AssemblyInfo.cs
52 52 // You can specify all the values or you can default the Build and Revision Numbers 53 53 // by using the '*' as shown below: 54 54 [assembly: AssemblyVersion("3.4.0.0")] 55 [assembly: AssemblyFileVersion("3.4.0.1489 3")]55 [assembly: AssemblyFileVersion("3.4.0.14894")] -
HeuristicLab.Algorithms.SAPBA/SapbaUtilities.cs
1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 * 5 * This file is part of HeuristicLab. 6 * 7 * HeuristicLab is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * HeuristicLab is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 #endregion 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 using System.Threading; 26 using HeuristicLab.Algorithms.DataAnalysis; 27 using HeuristicLab.Common; 28 using HeuristicLab.Core; 29 using HeuristicLab.Data; 30 using HeuristicLab.Encodings.RealVectorEncoding; 31 using HeuristicLab.Optimization; 32 using HeuristicLab.Problems.DataAnalysis; 33 34 namespace HeuristicLab.Algorithms.SAPBA { 35 internal static class SapbaUtilities { 36 //Extention methods for convenience 37 public static int ArgMax<T>(this IEnumerable<T> values, Func<T, double> func) { 38 var max = double.MinValue; 39 var maxIdx = 0; 40 var idx = 0; 41 foreach (var v in values) { 42 var d = func.Invoke(v); 43 if (d > max) { 44 max = d; 45 maxIdx = idx; 46 } 47 idx++; 48 } 49 return maxIdx; 50 } 51 public static int ArgMin<T>(this IEnumerable<T> values, Func<T, double> func) { 52 return ArgMax(values, x => -func.Invoke(x)); 53 } 54 public static double GetEstimation(this IRegressionModel model, RealVector r) { 55 var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) }, false); 56 return model.GetEstimatedValues(dataset, new[] { 0 }).First(); 57 } 58 public static double GetVariance(this IConfidenceRegressionModel model, RealVector r) { 59 var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) }, false); 60 return model.GetEstimatedVariances(dataset, new[] { 0 }).First(); 61 } 62 public static double GetDoubleValue(this IDataset dataset, int i, int j) { 63 return dataset.GetDoubleValue("input" + j, i); 64 } 65 66 //Sub-Algorithms 67 public static ResultCollection SyncRunSubAlgorithm(IAlgorithm alg, int random) { 68 if (alg.Parameters.ContainsKey("SetSeedRandomly") && alg.Parameters.ContainsKey("Seed")) { 69 var setSeed = alg.Parameters["SetSeedRandomly"].ActualValue as BoolValue; 70 var seed = alg.Parameters["Seed"].ActualValue as IntValue; 71 if (seed == null || setSeed == null) throw new ArgumentException("wrong SeedParametertypes"); 72 setSeed.Value = false; 73 seed.Value = random; 74 75 } 76 EventWaitHandle trigger = new AutoResetEvent(false); 77 Exception ex = null; 78 EventHandler<EventArgs<Exception>> exhandler = (sender, e) => ex = e.Value; 79 EventHandler stoppedHandler = (sender, e) => trigger.Set(); 80 alg.ExceptionOccurred += exhandler; 81 alg.Stopped += stoppedHandler; 82 alg.Prepare(); 83 alg.Start(); 84 trigger.WaitOne(); 85 alg.ExceptionOccurred -= exhandler; 86 alg.Stopped -= stoppedHandler; 87 if (ex != null) throw ex; 88 return alg.Results; 89 } 90 public static IRegressionSolution BuildModel(IReadOnlyList<Tuple<RealVector, double>> samples, IDataAnalysisAlgorithm<IRegressionProblem> regressionAlgorithm, IRandom random, bool removeDuplicates = true, IRegressionSolution oldSolution = null) { 91 var dataset = GetDataSet(samples, removeDuplicates); 92 var problemdata = new RegressionProblemData(dataset, dataset.VariableNames.Where(x => !x.Equals("output")), "output"); 93 problemdata.TrainingPartition.Start = 0; 94 problemdata.TrainingPartition.End = dataset.Rows; 95 problemdata.TestPartition.Start = dataset.Rows; 96 problemdata.TestPartition.End = dataset.Rows; 97 98 if (regressionAlgorithm.Problem == null) regressionAlgorithm.Problem = new RegressionProblem(); 99 var problem = regressionAlgorithm.Problem; 100 problem.ProblemDataParameter.Value = problemdata; 101 var i = 0; 102 IRegressionSolution solution = null; 103 104 while (solution == null && i++ < 100) { 105 var results = SyncRunSubAlgorithm(regressionAlgorithm, random.Next(int.MaxValue)); 106 solution = results.Select(x => x.Value).OfType<IRegressionSolution>().SingleOrDefault(); 107 } 108 109 //special treatement for GaussianProcessRegression 110 var gp = regressionAlgorithm as GaussianProcessRegression; 111 var oldGaussian = oldSolution as GaussianProcessRegressionSolution; 112 if (gp != null && oldGaussian != null) { 113 const double noise = 0.0; 114 var n = samples.First().Item1.Length; 115 var mean = (IMeanFunction)oldGaussian.Model.MeanFunction.Clone(); 116 var cov = (ICovarianceFunction)oldGaussian.Model.CovarianceFunction.Clone(); 117 if (mean.GetNumberOfParameters(n) != 0 || cov.GetNumberOfParameters(n) != 0) throw new ArgumentException("DEBUG: assumption about fixed paramters wrong"); 118 double[] hyp = { noise }; 119 try { 120 var model = new GaussianProcessModel(problemdata.Dataset, problemdata.TargetVariable, problemdata.AllowedInputVariables, problemdata.TrainingIndices, hyp, mean, cov); 121 model.FixParameters(); 122 var sol = new GaussianProcessRegressionSolution(model, problemdata); 123 if (solution == null || solution.TrainingMeanSquaredError > sol.TrainingMeanSquaredError) solution = sol; 124 } 125 catch (ArgumentException) { } 126 } 127 if (solution == null) throw new ArgumentException("The algorithm didn't return a model"); 128 regressionAlgorithm.Runs.Clear(); 129 return solution; 130 } 131 132 //RegressionModel extensions 133 public const double DuplicateResolution = 0.000001; 134 public static Dataset GetDataSet(IReadOnlyList<Tuple<RealVector, double>> samples, bool removeDuplicates) { 135 if (removeDuplicates) samples = RemoveDuplicates(samples); //TODO duplicate removal leads to incorrect uncertainty values in models 136 var dimensions = samples[0].Item1.Length + 1; 137 var data = new double[samples.Count, dimensions]; 138 var names = new string[dimensions - 1]; 139 for (var i = 0; i < names.Length; i++) names[i] = "input" + i; 140 for (var j = 0; j < samples.Count; j++) { 141 for (var i = 0; i < names.Length; i++) data[j, i] = samples[j].Item1[i]; 142 data[j, dimensions - 1] = samples[j].Item2; 143 } 144 return new Dataset(names.Concat(new[] { "output" }).ToArray(), data); 145 } 146 private static IReadOnlyList<Tuple<RealVector, double>> RemoveDuplicates(IReadOnlyList<Tuple<RealVector, double>> samples) { 147 var res = new List<Tuple<RealVector, double, int>>(); 148 foreach (var sample in samples) { 149 if (res.Count == 0) { 150 res.Add(new Tuple<RealVector, double, int>(sample.Item1, sample.Item2, 1)); 151 continue; 152 } 153 var index = res.ArgMin(x => Euclidian(sample.Item1, x.Item1)); 154 var d = Euclidian(res[index].Item1, sample.Item1); 155 if (d > DuplicateResolution) res.Add(new Tuple<RealVector, double, int>(sample.Item1, sample.Item2, 1)); 156 else { 157 var t = res[index]; 158 res.RemoveAt(index); 159 res.Add(new Tuple<RealVector, double, int>(t.Item1, t.Item2 + sample.Item2, t.Item3 + 1)); 160 } 161 } 162 return res.Select(x => new Tuple<RealVector, double>(x.Item1, x.Item2 / x.Item3)).ToArray(); 163 } 164 private static double Euclidian(IEnumerable<double> a, IEnumerable<double> b) { 165 return Math.Sqrt(a.Zip(b, (d, d1) => d - d1).Sum(d => d * d)); 166 } 167 } 168 } -
HeuristicLab.Algorithms.SAPBA/Strategies/InfillStrategy.cs
1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 * 5 * This file is part of HeuristicLab. 6 * 7 * HeuristicLab is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * HeuristicLab is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 #endregion 21 22 using System; 23 using System.Linq; 24 using HeuristicLab.Algorithms.EGO; 25 using HeuristicLab.Common; 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.RealVectorEncoding; 29 using HeuristicLab.Optimization; 30 using HeuristicLab.Parameters; 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 33 namespace HeuristicLab.Algorithms.SAPBA { 34 [StorableClass] 35 public class InfillStrategy : StrategyBase { 36 #region Parameternames 37 public const string NoGenerationsParameterName = "Number of generations"; 38 public const string NoIndividualsParameterName = "Number of individuals"; 39 public const string InfillCriterionParameterName = "InfillCriterion"; 40 #endregion 41 #region Paramterproperties 42 public IFixedValueParameter<IntValue> NoGenerationsParameter => Parameters[NoGenerationsParameterName] as IFixedValueParameter<IntValue>; 43 public IFixedValueParameter<IntValue> NoIndividualsParameter => Parameters[NoIndividualsParameterName] as IFixedValueParameter<IntValue>; 44 public IConstrainedValueParameter<IInfillCriterion> InfillCriterionParameter => Parameters[InfillCriterionParameterName] as IConstrainedValueParameter<IInfillCriterion>; 45 #endregion 46 #region Properties 47 public IntValue NoGenerations => NoGenerationsParameter.Value; 48 public IntValue NoIndividuals => NoIndividualsParameter.Value; 49 public IInfillCriterion InfillCriterion => InfillCriterionParameter.Value; 50 [Storable] 51 public int Generations; 52 #endregion 53 54 #region Constructors 55 [StorableConstructor] 56 protected InfillStrategy(bool deserializing) : base(deserializing) { } 57 [StorableHook(HookType.AfterDeserialization)] 58 private void AfterDeserialization() { 59 AttachListeners(); 60 } 61 protected InfillStrategy(InfillStrategy original, Cloner cloner) : base(original, cloner) { 62 Generations = original.Generations; 63 AttachListeners(); 64 } 65 public InfillStrategy() { 66 var critera = new ItemSet<IInfillCriterion> { new ExpectedImprovement(), new AugmentedExpectedImprovement(), new ExpectedQuality(), new ExpectedQuantileImprovement(), new MinimalQuantileCriterium(), new PluginExpectedImprovement() }; 67 Parameters.Add(new FixedValueParameter<IntValue>(NoGenerationsParameterName, "The number of generations before a new model is constructed", new IntValue(3))); 68 Parameters.Add(new FixedValueParameter<IntValue>(NoIndividualsParameterName, "The number of individuals that are sampled each generation ", new IntValue(3))); 69 Parameters.Add(new ConstrainedValueParameter<IInfillCriterion>(InfillCriterionParameterName, "The infill criterion used to cheaply evaluate points.", critera, critera.First())); 70 AttachListeners(); 71 } 72 public override IDeepCloneable Clone(Cloner cloner) { 73 return new InfillStrategy(this, cloner); 74 } 75 #endregion 76 77 protected override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, ResultCollection globalResults, IRandom random) { } 78 protected override void ProcessPopulation(Individual[] individuals, double[] qualities, IRandom random) { 79 if (RegressionSolution != null && Generations < NoGenerations.Value) Generations++; 80 else { 81 //Select NoIndividuals best samples 82 var samples = individuals 83 .Zip(qualities, (individual, d) => new Tuple<Individual, double>(individual, d)) 84 .OrderBy(t => Problem.Maximization ? -t.Item2 : t.Item2) 85 .Take(NoIndividuals.Value) 86 .Select(t => t.Item1.RealVector()); 87 foreach (var indi in samples) EvaluateSample(indi, random); 88 BuildRegressionSolution(random); 89 Generations = 0; 90 } 91 } 92 protected override void Initialize() { 93 Generations = 0; 94 } 95 96 #region events 97 private void AttachListeners() { 98 ModelChanged += OnModelChanged; 99 } 100 private void OnModelChanged(object sender, EventArgs e) { 101 InfillCriterion.Encoding = Problem?.Encoding as RealVectorEncoding; 102 InfillCriterion.RegressionSolution = RegressionSolution; 103 InfillCriterion.ExpensiveMaximization = Problem?.Maximization ?? false; 104 if (RegressionSolution != null && InfillCriterion.Encoding != null) 105 InfillCriterion.Initialize(); 106 } 107 #endregion 108 109 protected override double Estimate(RealVector point, IRandom random) { 110 return InfillCriterion.Maximization() != Maximization() ? -InfillCriterion.Evaluate(point) : InfillCriterion.Evaluate(point); 111 } 112 } 113 } -
HeuristicLab.Algorithms.SAPBA/Strategies/LamarckianStrategy.cs
1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 * 5 * This file is part of HeuristicLab. 6 * 7 * HeuristicLab is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * HeuristicLab is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 #endregion 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 using HeuristicLab.Algorithms.DataAnalysis; 26 using HeuristicLab.Algorithms.EGO; 27 using HeuristicLab.Algorithms.SAPBA.Operators; 28 using HeuristicLab.Analysis; 29 using HeuristicLab.Common; 30 using HeuristicLab.Core; 31 using HeuristicLab.Data; 32 using HeuristicLab.Encodings.RealVectorEncoding; 33 using HeuristicLab.Optimization; 34 using HeuristicLab.Parameters; 35 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 36 using HeuristicLab.Problems.DataAnalysis; 37 38 namespace HeuristicLab.Algorithms.SAPBA { 39 [StorableClass] 40 public class LamarckianStrategy : InfillStrategy { 41 #region Parameternames 42 public const string NoTrainingPointsParameterName = "Number of Trainingpoints"; 43 public const string LocalInfillCriterionParameterName = "LocalInfillCriterion"; 44 public const string OptimizationAlgorithmParameterName = "Optimization Algorithm"; 45 public const string RegressionAlgorithmParameterName = "Regression Algorithm"; 46 #endregion 47 #region Parameters 48 public IFixedValueParameter<IntValue> NoTrainingPointsParameter => Parameters[NoTrainingPointsParameterName] as IFixedValueParameter<IntValue>; 49 public IValueParameter<IAlgorithm> OptimizationAlgorithmParameter => Parameters[OptimizationAlgorithmParameterName] as IValueParameter<IAlgorithm>; 50 public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>; 51 public IConstrainedValueParameter<IInfillCriterion> LocalInfillCriterionParameter => Parameters[LocalInfillCriterionParameterName] as IConstrainedValueParameter<IInfillCriterion>; 52 #endregion 53 #region Properties 54 public IntValue NoTrainingPoints => NoTrainingPointsParameter.Value; 55 public IAlgorithm OptimizationAlgorithm => OptimizationAlgorithmParameter.Value; 56 public IDataAnalysisAlgorithm<IRegressionProblem> RegressionAlgorithm => RegressionAlgorithmParameter.Value; 57 public IInfillCriterion LocalInfillCriterion => LocalInfillCriterionParameter.Value; 58 #endregion 59 60 #region Constructors 61 [StorableConstructor] 62 protected LamarckianStrategy(bool deserializing) : base(deserializing) { } 63 [StorableHook(HookType.AfterDeserialization)] 64 private void AfterDeserialization() { 65 RegisterParameterEvents(); 66 } 67 protected LamarckianStrategy(LamarckianStrategy original, Cloner cloner) : base(original, cloner) { 68 RegisterParameterEvents(); 69 } 70 public LamarckianStrategy() { 71 var localCritera = new ItemSet<IInfillCriterion> { new ExpectedQuality(), new ExpectedImprovement(), new AugmentedExpectedImprovement(), new ExpectedQuantileImprovement(), new MinimalQuantileCriterium(), new PluginExpectedImprovement() }; 72 var osEs = new OffspringSelectionEvolutionStrategy.OffspringSelectionEvolutionStrategy { 73 Problem = new InfillProblem(), 74 ComparisonFactor = { Value = 1.0 }, 75 MaximumGenerations = { Value = 1000 }, 76 MaximumEvaluatedSolutions = { Value = 100000 }, 77 PlusSelection = { Value = true }, 78 PopulationSize = { Value = 1 } 79 }; 80 osEs.MutatorParameter.Value = osEs.MutatorParameter.ValidValues.OfType<MultiRealVectorManipulator>().First(); 81 Parameters.Add(new FixedValueParameter<IntValue>(NoTrainingPointsParameterName, "The number of sample points used to create a local model", new IntValue(50))); 82 Parameters.Add(new ConstrainedValueParameter<IInfillCriterion>(LocalInfillCriterionParameterName, "The infill criterion used to cheaply evaluate points.", localCritera, localCritera.First())); 83 Parameters.Add(new ValueParameter<IAlgorithm>(OptimizationAlgorithmParameterName, "The algorithm used to solve the expected improvement subproblem", osEs)); 84 Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionAlgorithmParameterName, "The model used to approximate the problem", new GaussianProcessRegression { Problem = new RegressionProblem() })); 85 RegisterParameterEvents(); 86 } 87 public override IDeepCloneable Clone(Cloner cloner) { 88 return new LamarckianStrategy(this, cloner); 89 } 90 #endregion 91 92 //Short lived stores for analysis 93 private readonly List<double> LamarckValues = new List<double>(); 94 private readonly List<double> SampleValues = new List<double>(); 95 96 protected override void Initialize() { 97 base.Initialize(); 98 var infillProblem = OptimizationAlgorithm.Problem as InfillProblem; 99 if (infillProblem == null) throw new ArgumentException("InfillOptimizationAlgorithm does not have an InfillProblem."); 100 infillProblem.InfillCriterion = LocalInfillCriterion; 101 } 102 protected override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, ResultCollection globalResults, IRandom random) { 103 base.Analyze(individuals, qualities, results, globalResults, random); 104 const string plotName = "Lamarck Comparison"; 105 const string lamarckRow = "Lamarck Values"; 106 const string samplesRow = "Original Values"; 107 if (!globalResults.ContainsKey(plotName)) 108 globalResults.Add(new Result(plotName, new DataTable(plotName))); 109 110 var plot = (DataTable)globalResults[plotName].Value; 111 if (!plot.Rows.ContainsKey(lamarckRow)) plot.Rows.Add(new DataRow(lamarckRow)); 112 if (!plot.Rows.ContainsKey(samplesRow)) plot.Rows.Add(new DataRow(samplesRow)); 113 plot.Rows[lamarckRow].Values.AddRange(LamarckValues); 114 plot.Rows[samplesRow].Values.AddRange(SampleValues); 115 LamarckValues.Clear(); 116 SampleValues.Clear(); 117 118 //analyze Hypervolumes 119 const string volPlotName = "Hypervolumes Comparison"; 120 const string mainRowName = "Population Volume (log)"; 121 const string subspaceRowName = "Subspace Volume (log) for Lamarck Candidate "; 122 if (!globalResults.ContainsKey(volPlotName)) 123 globalResults.Add(new Result(volPlotName, new DataTable(volPlotName))); 124 125 plot = (DataTable)globalResults[volPlotName].Value; 126 if (!plot.Rows.ContainsKey(mainRowName)) plot.Rows.Add(new DataRow(mainRowName)); 127 var v = Math.Log(GetStableVolume(GetBoundingBox(individuals.Select(x => x.RealVector())))); 128 plot.Rows[mainRowName].Values.Add(v); 129 130 var indis = individuals 131 .Zip(qualities, (individual, d) => new Tuple<Individual, double>(individual, d)) 132 .OrderBy(t => SapbaAlgorithm.Problem.Maximization ? -t.Item2 : t.Item2) 133 .Take(NoIndividuals.Value) 134 .Select(t => t.Item1).ToArray(); 135 136 for (var i = 0; i < indis.Length; i++) { 137 var samples = GetNearestSamples(NoTrainingPoints.Value, indis[i].RealVector()); 138 var d = Math.Log(GetStableVolume(GetBoundingBox(samples.Select(x => x.Item1)))); 139 if (!plot.Rows.ContainsKey(subspaceRowName + i)) plot.Rows.Add(new DataRow(subspaceRowName + i)); 140 plot.Rows[subspaceRowName + i].Values.Add(d); 141 } 142 143 144 145 } 146 protected override void ProcessPopulation(Individual[] individuals, double[] qualities, IRandom random) { 147 if (RegressionSolution == null) return; 148 if (Generations < NoGenerations.Value) Generations++; 149 else { 150 //Select best Individuals 151 var indis = individuals 152 .Zip(qualities, (individual, d) => new Tuple<Individual, double>(individual, d)) 153 .OrderBy(t => Problem.Maximization ? -t.Item2 : t.Item2) 154 .Take(NoIndividuals.Value) 155 .Select(t => t.Item1).ToArray(); 156 //Evaluate individuals 157 foreach (var individual in indis) 158 SampleValues.Add(EvaluateSample(individual.RealVector(), random).Item2); 159 160 //Perform memetic replacement for all points 161 for (var i = 0; i < indis.Length; i++) { 162 var vector = indis[i].RealVector(); 163 var altVector = OptimizeInfillProblem(vector, random); 164 LamarckValues.Add(EvaluateSample(altVector, random).Item2); 165 if (LamarckValues[i] < SampleValues[i] == Problem.Maximization) continue; 166 for (var j = 0; j < vector.Length; j++) vector[j] = altVector[j]; 167 } 168 169 BuildRegressionSolution(random); 170 Generations = 0; 171 } 172 } 173 174 #region Events 175 private void RegisterParameterEvents() { 176 OptimizationAlgorithmParameter.ValueChanged += OnInfillAlgorithmChanged; 177 OptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged; 178 LocalInfillCriterionParameter.ValueChanged += OnInfillCriterionChanged; 179 } 180 private void OnInfillCriterionChanged(object sender, EventArgs e) { 181 ((InfillProblem)OptimizationAlgorithm.Problem).InfillCriterion = LocalInfillCriterion; 182 } 183 private void OnInfillAlgorithmChanged(object sender, EventArgs e) { 184 OptimizationAlgorithm.Problem = new InfillProblem { InfillCriterion = LocalInfillCriterion }; 185 OptimizationAlgorithm.ProblemChanged -= OnInfillProblemChanged; //avoid double attaching 186 OptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged; 187 } 188 private void OnInfillProblemChanged(object sender, EventArgs e) { 189 OptimizationAlgorithm.ProblemChanged -= OnInfillProblemChanged; 190 OptimizationAlgorithm.Problem = new InfillProblem { InfillCriterion = LocalInfillCriterion }; 191 OptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged; 192 } 193 #endregion 194 195 #region helpers 196 private RealVector OptimizeInfillProblem(RealVector point, IRandom random) { 197 var infillProblem = OptimizationAlgorithm.Problem as InfillProblem; 198 if (infillProblem == null) throw new ArgumentException("InfillOptimizationAlgorithm does not have an InfillProblem."); 199 if (infillProblem.InfillCriterion != LocalInfillCriterion) throw new ArgumentException("InfillCiriterion for Problem is not correctly set."); 200 201 var points = Math.Min(NoTrainingPoints.Value, Samples.Count); 202 var samples = GetNearestSamples(points, point); 203 var regression = SapbaUtilities.BuildModel(samples, RegressionAlgorithm, random); 204 var box = GetBoundingBox(samples.Select(x => x.Item1)); 205 206 infillProblem.Encoding.Length = ((RealVectorEncoding)Problem.Encoding).Length; 207 infillProblem.Encoding.Bounds = box; 208 infillProblem.Encoding.SolutionCreator = new FixedRealVectorCreator(point); 209 infillProblem.Initialize(regression, Problem.Maximization); 210 var res = SapbaUtilities.SyncRunSubAlgorithm(OptimizationAlgorithm, random.Next(int.MaxValue)); 211 if (!res.ContainsKey(InfillProblem.BestInfillSolutionResultName)) throw new ArgumentException("The InfillOptimizationAlgorithm did not return a best solution"); 212 var v = res[InfillProblem.BestInfillSolutionResultName].Value as RealVector; 213 if (v == null) throw new ArgumentException("The InfillOptimizationAlgorithm did not return the expected result types"); 214 if (!InBounds(v, box)) throw new ArgumentException("Vector not in bounds"); 215 OptimizationAlgorithm.Runs.Clear(); 216 return v; 217 } 218 private Tuple<RealVector, double>[] GetNearestSamples(int noSamples, RealVector point) { 219 return Samples.Select(sample => Tuple.Create(SquaredEuclidean(sample.Item1, point), sample)).OrderBy(x => x.Item1).Take(noSamples).Select(x => x.Item2).ToArray(); 220 } 221 private static DoubleMatrix GetBoundingBox(IEnumerable<RealVector> samples) { 222 DoubleMatrix m = null; 223 foreach (var sample in samples) 224 if (m == null) { 225 m = new DoubleMatrix(sample.Length, 2); 226 for (var i = 0; i < sample.Length; i++) m[i, 0] = m[i, 1] = sample[i]; 227 } else 228 for (var i = 0; i < sample.Length; i++) { 229 m[i, 0] = Math.Min(m[i, 0], sample[i]); 230 m[i, 1] = Math.Max(m[i, 1], sample[i]); 231 } 232 return m; 233 } 234 235 //the volume of a bounded-box whith slightly increased dimensions (Volume can never reach 0) 236 private static double GetStableVolume(DoubleMatrix bounds) { 237 var res = 1.0; 238 for (var i = 0; i < bounds.Rows; i++) res *= bounds[i, 1] - bounds[i, 0] + 0.1; 239 return res; 240 } 241 private static bool InBounds(RealVector r, DoubleMatrix bounds) { 242 return !r.Where((t, i) => t < bounds[i, 0] || t > bounds[i, 1]).Any(); 243 } 244 private static double SquaredEuclidean(RealVector a, RealVector b) { 245 return a.Select((t, i) => t - b[i]).Sum(d => d * d); 246 } 247 #endregion 248 } 249 } -
HeuristicLab.Algorithms.SAPBA/Strategies/StrategyBase.cs
22 22 using System; 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Threading;26 25 using HeuristicLab.Analysis; 27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Core; … … 32 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 32 using HeuristicLab.Problems.DataAnalysis; 34 33 35 namespace HeuristicLab.Algorithms.SAPBA .Strategies{34 namespace HeuristicLab.Algorithms.SAPBA { 36 35 [StorableClass] 37 36 public abstract class StrategyBase : ParameterizedNamedItem, ISurrogateStrategy { 38 37 #region Properties 39 38 [Storable] 40 protected SurrogateAssistedPopulationBasedAlgorithm Algorithm; 39 protected SurrogateAssistedPopulationBasedAlgorithm SapbaAlgorithm; 40 protected SingleObjectiveBasicProblem<IEncoding> Problem => SapbaAlgorithm?.Problem; 41 41 [Storable] 42 pr ivateList<Tuple<RealVector, double>> Samples;42 protected List<Tuple<RealVector, double>> Samples; 43 43 [Storable] 44 protected IRegressionSolution RegressionSolution; 45 protected CancellationToken Cancellation; 46 private IEnumerable<Tuple<RealVector, double>> TruncatedSamples => Samples.Count > Algorithm.MaximalDatasetSize && Algorithm.MaximalDatasetSize > 0 ? Samples.Skip(Samples.Count - Algorithm.MaximalDatasetSize) : Samples; 44 private IRegressionSolution regressionSolution; 45 46 public IRegressionSolution RegressionSolution 47 { 48 get { return regressionSolution; } 49 protected set 50 { 51 regressionSolution = value; 52 OnModelChanged(); 53 } 54 } 55 56 private List<Tuple<RealVector, double>> TruncatedSamples => Samples.Count > SapbaAlgorithm.MaximalDatasetSize && SapbaAlgorithm.MaximalDatasetSize > 0 ? Samples.Skip(Samples.Count - SapbaAlgorithm.MaximalDatasetSize).ToList() : Samples; 47 57 #endregion 48 58 59 #region Events 60 public event EventHandler ModelChanged; 61 private void OnModelChanged() { 62 ModelChanged?.Invoke(this, EventArgs.Empty); 63 OnToStringChanged(); 64 } 65 #endregion 66 49 67 #region ResultName 50 68 private const string BestQualityResultName = "Best Quality"; 51 69 private const string BestSolutionResultName = "Best Solution"; … … 64 82 [StorableConstructor] 65 83 protected StrategyBase(bool deserializing) : base(deserializing) { } 66 84 protected StrategyBase(StrategyBase original, Cloner cloner) : base(original, cloner) { 67 if (original.Samples != null) Samples = original.Samples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();85 Samples = original.Samples?.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList(); 68 86 RegressionSolution = cloner.Clone(original.RegressionSolution); 87 SapbaAlgorithm = cloner.Clone(original.SapbaAlgorithm); 69 88 } 70 89 protected StrategyBase() { } 71 90 #endregion 72 91 73 public abstract double Evaluate(RealVector r, IRandom random);74 92 protected abstract void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, ResultCollection globalResults, IRandom random); 75 93 protected abstract void ProcessPopulation(Individual[] individuals, double[] qualities, IRandom random); 76 protected abstract void Initialize(); 94 protected virtual void Initialize() { } 95 //protected virtual void OnModelChanged() { } 77 96 97 protected abstract double Estimate(RealVector r, IRandom random); 98 99 public double Evaluate(RealVector r, IRandom random) { 100 if (Samples.Count < SapbaAlgorithm.InitialEvaluations) return EvaluateSample(r, random).Item2; 101 if (Samples.Count == SapbaAlgorithm.InitialEvaluations && RegressionSolution == null) { 102 BuildRegressionSolution(random); 103 OnModelChanged(); 104 } 105 return Estimate(r, random); 106 } 78 107 public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 79 Algorithm.Problem.Analyze(individuals, qualities, results, random);108 SapbaAlgorithm.Problem.Analyze(individuals, qualities, results, random); 80 109 ProcessPopulation(individuals, qualities, random); 81 110 82 var globalResults = Algorithm.Results;111 var globalResults = SapbaAlgorithm.Results; 83 112 if (!globalResults.ContainsKey(EvaluatedSoultionsResultName)) globalResults.Add(new Result(EvaluatedSoultionsResultName, new IntValue(Samples.Count))); 84 113 else ((IntValue)globalResults[EvaluatedSoultionsResultName].Value).Value = Samples.Count; 85 114 if (!globalResults.ContainsKey(IterationsResultName)) globalResults.Add(new Result(IterationsResultName, new IntValue(0))); 86 115 else ((IntValue)globalResults[IterationsResultName].Value).Value++; 87 116 88 if (Samples.Count != 0) {89 var min = Samples.Min(x => x.Item2);90 var max = Samples.Max(x => x.Item2);91 var bestIdx = Algorithm.Problem.Maximization ? Samples.ArgMax(x => x.Item2) : Samples.ArgMin(x => x.Item2);92 117 93 if (!globalResults.ContainsKey(BestQualityResultName)) globalResults.Add(new Result(BestQualityResultName, new DoubleValue(0.0))); 94 ((DoubleValue)globalResults[BestQualityResultName].Value).Value = Samples[bestIdx].Item2; 95 if (!globalResults.ContainsKey(BestSolutionResultName)) globalResults.Add(new Result(BestSolutionResultName, new RealVector())); 96 globalResults[BestSolutionResultName].Value = Samples[bestIdx].Item1; 118 AnalyzeSamplesProgression(globalResults); 119 AnalyzeQualities(globalResults); 97 120 98 DataTable table;99 if (!globalResults.ContainsKey(QualityTableResultName)) {100 table = new DataTable("Qualites", "Qualites over iteration");101 globalResults.Add(new Result(QualityTableResultName, table));102 table.Rows.Add(new DataRow(BestQualityRowName, "Best Quality"));103 table.Rows.Add(new DataRow(WorstQualityRowName, "Worst Quality"));104 table.Rows.Add(new DataRow(CurrentQualityRowName, "Current Quality"));105 table.Rows.Add(new DataRow(MedianQualityRowName, "Median Quality"));106 table.Rows.Add(new DataRow(AverageQualityRowName, "Average Quality"));107 }108 table = (DataTable)globalResults[QualityTableResultName].Value;109 table.Rows[BestQualityResultName].Values.Add(Algorithm.Problem.Maximization ? max : min);110 table.Rows[WorstQualityRowName].Values.Add(Algorithm.Problem.Maximization ? min : max);111 table.Rows[CurrentQualityRowName].Values.Add(Samples[Samples.Count - 1].Item2);112 table.Rows[AverageQualityRowName].Values.Add(Samples.Average(x => x.Item2));113 table.Rows[MedianQualityRowName].Values.Add(Samples.Select(x => x.Item2).Median());114 }115 121 122 116 123 if (RegressionSolution != null) { 117 124 if (!globalResults.ContainsKey(RegressionSolutionResultName)) 118 125 globalResults.Add(new Result(RegressionSolutionResultName, RegressionSolution)); … … 122 129 123 130 Analyze(individuals, qualities, results, globalResults, random); 124 131 } 132 private void AnalyzeSamplesProgression(ResultCollection globalResults) { 133 const string samplesTableName = "SamplesProgression"; 134 const string minRowName = "Minimum"; 135 const string maxRowName = "Maximum"; 136 const string medianRowName = "Median"; 137 const string averageRowName = "Average"; 138 const string currentRowName = "Current"; 139 140 if (!globalResults.ContainsKey(samplesTableName)) { globalResults.Add(new Result(samplesTableName, new DataTable())); } 141 var table = (DataTable)globalResults[samplesTableName].Value; 142 143 if (!table.Rows.ContainsKey(minRowName)) table.Rows.Add(new DataRow(minRowName)); 144 if (!table.Rows.ContainsKey(maxRowName)) table.Rows.Add(new DataRow(maxRowName)); 145 if (!table.Rows.ContainsKey(medianRowName)) table.Rows.Add(new DataRow(medianRowName)); 146 if (!table.Rows.ContainsKey(averageRowName)) table.Rows.Add(new DataRow(averageRowName)); 147 if (!table.Rows.ContainsKey(currentRowName)) table.Rows.Add(new DataRow(currentRowName)); 148 149 for (var i = table.Rows[minRowName].Values.Count + 1; i < Samples.Count; i++) { 150 var subSamples = Samples.Take(i).Select(x => x.Item2).ToArray(); 151 table.Rows[minRowName].Values.Add(subSamples.Min()); 152 table.Rows[maxRowName].Values.Add(subSamples.Max()); 153 table.Rows[medianRowName].Values.Add(subSamples.Median()); 154 table.Rows[averageRowName].Values.Add(subSamples.Average()); 155 table.Rows[currentRowName].Values.Add(subSamples[subSamples.Length - 1]); 156 } 157 } 158 private void AnalyzeQualities(ResultCollection globalResults) { 159 if (Samples.Count == 0) return; 160 var min = Samples.Min(x => x.Item2); 161 var max = Samples.Max(x => x.Item2); 162 var bestIdx = SapbaAlgorithm.Problem.Maximization ? Samples.ArgMax(x => x.Item2) : Samples.ArgMin(x => x.Item2); 163 164 if (!globalResults.ContainsKey(BestQualityResultName)) globalResults.Add(new Result(BestQualityResultName, new DoubleValue(0.0))); 165 ((DoubleValue)globalResults[BestQualityResultName].Value).Value = Samples[bestIdx].Item2; 166 if (!globalResults.ContainsKey(BestSolutionResultName)) globalResults.Add(new Result(BestSolutionResultName, new RealVector())); 167 globalResults[BestSolutionResultName].Value = Samples[bestIdx].Item1; 168 169 DataTable table; 170 if (!globalResults.ContainsKey(QualityTableResultName)) { 171 table = new DataTable("Qualites", "Qualites over iteration"); 172 globalResults.Add(new Result(QualityTableResultName, table)); 173 table.Rows.Add(new DataRow(BestQualityRowName, "Best Quality")); 174 table.Rows.Add(new DataRow(WorstQualityRowName, "Worst Quality")); 175 table.Rows.Add(new DataRow(CurrentQualityRowName, "Current Quality")); 176 table.Rows.Add(new DataRow(MedianQualityRowName, "Median Quality")); 177 table.Rows.Add(new DataRow(AverageQualityRowName, "Average Quality")); 178 } 179 table = (DataTable)globalResults[QualityTableResultName].Value; 180 table.Rows[BestQualityResultName].Values.Add(SapbaAlgorithm.Problem.Maximization ? max : min); 181 table.Rows[WorstQualityRowName].Values.Add(SapbaAlgorithm.Problem.Maximization ? min : max); 182 table.Rows[CurrentQualityRowName].Values.Add(Samples[Samples.Count - 1].Item2); 183 table.Rows[AverageQualityRowName].Values.Add(Samples.Average(x => x.Item2)); 184 table.Rows[MedianQualityRowName].Values.Add(Samples.Select(x => x.Item2).Median()); 185 } 186 125 187 public void Initialize(SurrogateAssistedPopulationBasedAlgorithm algorithm) { 126 Algorithm = algorithm;127 Samples = algorithm .InitialSamples?.ToList() ?? new List<Tuple<RealVector, double>>();188 SapbaAlgorithm = algorithm; 189 Samples = algorithm?.InitialSamples?.ToList() ?? new List<Tuple<RealVector, double>>(); 128 190 RegressionSolution = null; 129 191 Initialize(); 130 192 } 193 public virtual bool Maximization() { 194 return SapbaAlgorithm?.Problem?.Maximization ?? false; 195 } 131 196 132 197 #region Helpers for Subclasses 133 198 protected void BuildRegressionSolution(IRandom random) { 134 RegressionSolution = EgoUtilities.BuildModel(Cancellation, TruncatedSamples, Algorithm.RegressionAlgorithm, random,Algorithm.RemoveDuplicates, RegressionSolution);199 RegressionSolution = SapbaUtilities.BuildModel(TruncatedSamples, SapbaAlgorithm.RegressionAlgorithm, random, SapbaAlgorithm.RemoveDuplicates, RegressionSolution); 135 200 } 136 201 protected Tuple<RealVector, double> EvaluateSample(RealVector point, IRandom random) { 137 Cancellation.ThrowIfCancellationRequested(); 138 if (Samples.Count >= Algorithm.MaximumEvaluations) { Algorithm.OptimizationAlgorithm.Stop(); return new Tuple<RealVector, double>(point, 0.0); } 139 var p = new Tuple<RealVector, double>(point, Algorithm.Problem.Evaluate(GetIndividual(point), random)); 202 if (Samples.Count >= SapbaAlgorithm.MaximumEvaluations) { SapbaAlgorithm.OptimizationAlgorithm.Stop(); return new Tuple<RealVector, double>(point, 0.0); } 203 var p = new Tuple<RealVector, double>((RealVector)point.Clone(), SapbaAlgorithm.Problem.Evaluate(GetIndividual(point), random)); 140 204 Samples.Add(p); 141 205 return p; 142 206 } 143 protected Tuple<RealVector, double> EstimateSample(RealVector point, IRandom random) { 144 if (Samples.Count == Algorithm.InitialEvaluations && RegressionSolution == null) BuildRegressionSolution(random); 145 return Samples.Count < Algorithm.InitialEvaluations ? EvaluateSample(point, random) : new Tuple<RealVector, double>(point, RegressionSolution.Model.GetEstimation(point)); 207 protected double EstimateSample(RealVector r) { 208 return RegressionSolution.Model.GetEstimation(r); 146 209 } 210 147 211 #endregion 148 212 149 213 #region Helpers 150 214 private Individual GetIndividual(RealVector r) { 151 215 var scope = new Scope(); 152 scope.Variables.Add(new Variable( Algorithm.Problem.Encoding.Name, r));153 return new SingleEncodingIndividual( Algorithm.Problem.Encoding, scope);216 scope.Variables.Add(new Variable(SapbaAlgorithm.Problem.Encoding.Name, r)); 217 return new SingleEncodingIndividual(SapbaAlgorithm.Problem.Encoding, scope); 154 218 } 155 156 public void UpdateCancellation(CancellationToken cancellationToken) {157 Cancellation = cancellationToken;158 }159 219 #endregion 160 220 } 161 221 } 162 No newline at end of file -
HeuristicLab.Algorithms.SAPBA/SurrogateAssistedPopulationBasedAlgorithm.cs
24 24 using System.Linq; 25 25 using System.Threading; 26 26 using HeuristicLab.Algorithms.DataAnalysis; 27 using HeuristicLab.Algorithms.SAPBA.Strategies;28 27 using HeuristicLab.Common; 29 28 using HeuristicLab.Core; 30 29 using HeuristicLab.Data; … … 33 32 using HeuristicLab.Parameters; 34 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 35 34 using HeuristicLab.Problems.DataAnalysis; 36 using HeuristicLab.Random;37 35 38 36 namespace HeuristicLab.Algorithms.SAPBA { 39 37 [StorableClass] … … 41 39 [Item("SurrogateAssistedPopulationBasedAlgorithm", "")] 42 40 public class SurrogateAssistedPopulationBasedAlgorithm : BasicAlgorithm, ISurrogateAlgorithm<RealVector> { 43 41 #region Basic-Alg-Essentials 44 public override bool SupportsPause => true;42 public override bool SupportsPause => false; 45 43 public override Type ProblemType => typeof(SingleObjectiveBasicProblem<IEncoding>); 46 44 public new SingleObjectiveBasicProblem<IEncoding> Problem 47 45 { … … 91 89 92 90 #region StorableProperties 93 91 [Storable] 94 private IRandom Random = new MersenneTwister();95 [Storable]96 92 public List<Tuple<RealVector, double>> InitialSamples { get; private set; } 97 93 [Storable] 98 public SurrogateProblem surrogateProblem; 99 public void SetInitialSamples(RealVector[] solutions, double[] qualities) { 100 InitialSamples = solutions.Zip(qualities, (vector, d) => new Tuple<RealVector, double>(vector, d)).ToList(); 101 } 94 public SurrogateProblem SurrogateProblem; 102 95 #endregion 103 96 104 97 #region HLConstructors … … 109 102 RegisterEventhandlers(); 110 103 } 111 104 protected SurrogateAssistedPopulationBasedAlgorithm(SurrogateAssistedPopulationBasedAlgorithm original, Cloner cloner) : base(original, cloner) { 112 Random = cloner.Clone(Random);113 if (original.InitialSamples != null) InitialSamples = original.InitialSamples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();105 InitialSamples = original.InitialSamples?.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList(); 106 SurrogateProblem = cloner.Clone(original.SurrogateProblem); 114 107 RegisterEventhandlers(); 115 108 } 116 109 public override IDeepCloneable Clone(Cloner cloner) { return new SurrogateAssistedPopulationBasedAlgorithm(this, cloner); } 117 110 public SurrogateAssistedPopulationBasedAlgorithm() { 118 surrogateProblem = new SurrogateProblem(); 119 var geneticAlgorithm = new GeneticAlgorithm.GeneticAlgorithm { 120 PopulationSize = { Value = 50 }, 121 Problem = surrogateProblem 122 }; 123 var model = new GaussianProcessRegression { 124 Problem = new RegressionProblem() 125 }; 111 SurrogateProblem = new SurrogateProblem(); 112 var geneticAlgorithm = new GeneticAlgorithm.GeneticAlgorithm { PopulationSize = { Value = 50 }, Problem = SurrogateProblem, Elites = { Value = 0 } }; 113 var model = new GaussianProcessRegression { Problem = new RegressionProblem() }; 114 var strategies = new ItemSet<ISurrogateStrategy> { new InfillStrategy(), new LamarckianStrategy() }; 115 126 116 model.CovarianceFunctionParameter.Value = new CovarianceRationalQuadraticIso(); 127 128 Parameters.Add(new FixedValueParameter<IntValue>(Maxim umEvaluationsParameterName, "", new IntValue(int.MaxValue)));129 Parameters.Add(new FixedValueParameter<IntValue>( InitialEvaluationsParameterName, "", new IntValue(10)));117 Parameters.Add(new FixedValueParameter<IntValue>(InitialEvaluationsParameterName, "The initial number of evaluations performed before the first model is constructed", new IntValue(10))); 118 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))); 119 Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "The maximum number of evaluations performed", new IntValue(int.MaxValue))); 130 120 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(-1))); 121 Parameters.Add(new ValueParameter<Algorithm>(OptimizationAlgorithmParameterName, "The algorithm used to solve the expected improvement subproblem", geneticAlgorithm)); 122 Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionAlgorithmParameterName, "The model used to approximate the problem", model)); 123 Parameters.Add(new FixedValueParameter<BoolValue>(RemoveDuplicatesParamterName, "Wether duplicate samples should be replaced by a single sample with an averaged quality. This GREATLY decreases the chance of ill conditioned models (unbuildable models) but is not theoretically sound as the model ignores the increasing certainty in this region")); 131 124 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 132 125 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 133 Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionAlgorithmParameterName, "The model used to approximate the problem", model));134 Parameters.Add(new ValueParameter<Algorithm>(OptimizationAlgorithmParameterName, "The algorithm used to solve the expected improvement subproblem", geneticAlgorithm));135 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)));136 Parameters.Add(new FixedValueParameter<BoolValue>(RemoveDuplicatesParamterName, "Wether duplicate samples should be replaced by a single sample with an averaged quality. This GREATLY decreases the chance of ill conditioned models (unbuildable models) but is not theoretically sound as the model ignores the increasing certainty in this region"));137 var strategies = new ItemSet<ISurrogateStrategy> { new GenerationalStrategy(), new IndividualStrategy() };138 126 Parameters.Add(new ConstrainedValueParameter<ISurrogateStrategy>(StrategyParameterName, "The surrogate strategy that dictates how the optimization alg is assisted", strategies, strategies.First())); 139 127 RegisterEventhandlers(); 140 128 } 141 129 #endregion 142 130 131 public void SetInitialSamples(RealVector[] solutions, double[] qualities) { 132 InitialSamples = solutions.Zip(qualities, (vector, d) => new Tuple<RealVector, double>(vector, d)).ToList(); 133 } 143 134 protected override void Initialize(CancellationToken cancellationToken) { 144 135 base.Initialize(cancellationToken); 145 //encoding146 136 var enc = Problem.Encoding as RealVectorEncoding; 147 137 if (enc == null) throw new ArgumentException("The SAPBA algorithm can only be applied to RealVectorEncodings"); 148 149 //random150 138 if (SetSeedRandomly) SeedParameter.Value.Value = new System.Random().Next(); 151 Random.Reset(Seed);152 153 //initialize Strategy and Problem154 139 SurrogateStrategy.Initialize(this); 155 SurrogateStrategy.UpdateCancellation(cancellationToken); 156 surrogateProblem.SetStrategy(SurrogateStrategy); 157 surrogateProblem.SetProblem(Problem); 140 SurrogateProblem.Initialize(Problem, SurrogateStrategy); 158 141 } 159 142 protected override void Run(CancellationToken cancellationToken) { 160 SurrogateStrategy.UpdateCancellation(cancellationToken); 161 try { EgoUtilities.SyncRunSubAlgorithm(OptimizationAlgorithm, Random.Next()); } 162 finally { Analyze(); } 143 try { SapbaUtilities.SyncRunSubAlgorithm(OptimizationAlgorithm, Seed); } 144 finally { 145 foreach (var surrogateStrategy in StrategyParameter.ValidValues) surrogateStrategy.Initialize(null); 146 OptimizationAlgorithm.Runs.Clear(); 147 } 163 148 } 164 private void Analyze() { }165 149 166 150 #region Eventhandling 167 151 private void RegisterEventhandlers() { … … 172 156 OptimizationAlgorithmParameter.ValueChanged -= OnOptimizationAlgorithmChanged; 173 157 } 174 158 private void OnOptimizationAlgorithmChanged(object sender, EventArgs e) { 175 OptimizationAlgorithm.Problem = surrogateProblem;159 OptimizationAlgorithm.Problem = SurrogateProblem; 176 160 } 177 161 178 162 protected override void OnExecutionTimeChanged() { … … 179 163 base.OnExecutionTimeChanged(); 180 164 if (CancellationTokenSource == null) return; 181 165 if (MaximumRuntime == -1) return; 182 if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();166 if (ExecutionTime.TotalSeconds > MaximumRuntime) OptimizationAlgorithm.Stop(); 183 167 } 184 168 public override void Pause() { 185 169 base.Pause(); 186 if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Pause(); 187 if (OptimizationAlgorithm.ExecutionState == ExecutionState.Started) OptimizationAlgorithm.Pause(); 188 170 if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Stop(); 171 if (OptimizationAlgorithm.ExecutionState == ExecutionState.Started) OptimizationAlgorithm.Stop(); 189 172 } 190 173 public override void Stop() { 191 174 base.Stop(); 192 if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Stop();193 if (OptimizationAlgorithm.ExecutionState == ExecutionState.Started ) OptimizationAlgorithm.Stop();175 //if (RegressionAlgorithm.ExecutionState == ExecutionState.Started || RegressionAlgorithm.ExecutionState == ExecutionState.Paused) RegressionAlgorithm.Stop(); 176 if (OptimizationAlgorithm.ExecutionState == ExecutionState.Started || OptimizationAlgorithm.ExecutionState == ExecutionState.Paused) OptimizationAlgorithm.Stop(); 194 177 } 195 protected override void OnProblemChanged() {196 base.OnProblemChanged();197 surrogateProblem.SetProblem(Problem);198 }199 178 #endregion 200 179 201 180 }
Note: See TracBrowser
for help on using the repository browser.