Changeset 14768
- Timestamp:
- 03/20/17 12:07:20 (8 years ago)
- 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
-
Property
svn:global-ignores
set to
-
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EfficientGlobalOptimizationAlgorithm.cs
r14741 r14768 24 24 using System.Linq; 25 25 using System.Threading; 26 using HeuristicLab.Algorithms.CMAEvolutionStrategy;27 26 using HeuristicLab.Algorithms.DataAnalysis; 28 using HeuristicLab.Algorithms.EGO;29 27 using HeuristicLab.Analysis; 30 28 using HeuristicLab.Common; … … 38 36 using HeuristicLab.Random; 39 37 40 namespace HeuristicLab. Problems.SurrogateProblem{38 namespace HeuristicLab.Algorithms.EGO { 41 39 [StorableClass] 42 40 [Creatable(CreatableAttribute.Categories.Algorithms, Priority = 95)] 43 41 [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> { 45 43 #region Basic-Alg-Essentials 46 44 public override bool SupportsPause => true; … … 64 62 private const string SeedParameterName = "Seed"; 65 63 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 64 private const string MaximalDataSetSizeParameterName = "MaximalDataSetSize"; 66 65 #endregion 67 66 … … 93 92 public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>; 94 93 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>; 96 96 #endregion 97 97 98 98 #region Properties 99 99 100 public int GenerationSize 100 101 { … … 137 138 get { return SetSeedRandomlyParameter.Value.Value; } 138 139 } 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 139 155 #endregion 140 156 … … 144 160 [Storable] 145 161 private List<Tuple<RealVector, double>> Samples; 162 [Storable] 163 private List<Tuple<RealVector, double>> InitialSamples; 146 164 #endregion 147 165 … … 201 219 Random = cloner.Clone(Random); 202 220 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(); 203 222 RegisterEventhandlers(); 204 223 } 205 224 public override IDeepCloneable Clone(Cloner cloner) { return new EfficientGlobalOptimizationAlgorithm(this, cloner); } 206 225 public EfficientGlobalOptimizationAlgorithm() { 207 var cmaes = new CMAEvolutionStrategy {226 var cmaes = new CMAEvolutionStrategy.CMAEvolutionStrategy { 208 227 MaximumGenerations = 300, 209 228 PopulationSize = 50 … … 226 245 InfillCriterionParameter.ValidValues.Add(new ExpectedQuality()); 227 246 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 228 249 SetInfillProblem(); 229 250 RegisterEventhandlers(); … … 240 261 if (SetSeedRandomly) SeedParameter.Value.Value = new System.Random().Next(); 241 262 Random.Reset(Seed); 242 Samples = new List<Tuple<RealVector, double>>();263 Samples = InitialSamples == null ? new List<Tuple<RealVector, double>>() : InitialSamples.ToList(); 243 264 244 265 //results 245 266 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))); 247 268 Results.Add(new Result(BestSolutionResultName, new RealVector(1))); 248 269 Results.Add(new Result(BestQualityResultName, new DoubleValue(Problem.Maximization ? double.MinValue : double.MaxValue))); … … 254 275 Results.Add(new Result(QualitiesChartResultName, table)); 255 276 256 ResultsQualities.Rows.Add(new DataRow("DEBUG:ModelBuildingIterations"));257 258 277 //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 } 263 284 } 264 285 … … 269 290 for (ResultsIterations = 0; ResultsIterations < MaximumIterations; ResultsIterations++) { 270 291 try { 271 ResultsModel = BuildModel( );292 ResultsModel = BuildModel(cancellationToken); 272 293 cancellationToken.ThrowIfCancellationRequested(); 273 294 for (var i = 0; i < GenerationSize; i++) { … … 285 306 } 286 307 308 public void SetInitialSamples(RealVector[] individuals, double[] qualities) { 309 InitialSamples = individuals.Zip(qualities, (individual, d) => new Tuple<RealVector, double>(individual, d)).ToList(); 310 } 311 287 312 #region Eventhandling 288 313 private void RegisterEventhandlers() { … … 324 349 if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel(); 325 350 } 326 protected override void OnPaused() { 327 base.OnPaused(); 351 public override void Pause() { 328 352 if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started) InfillOptimizationAlgorithm.Pause(); 329 353 if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Pause(); 330 331 } 332 p rotected 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(); 336 360 } 337 361 protected override void OnProblemChanged() { … … 351 375 InfillOptimizationAlgorithm.Problem = infillProblem; 352 376 } 353 private IRegressionSolution BuildModel( ) {354 var dataset = EgoUtilities.GetDataSet( Samples);377 private IRegressionSolution BuildModel(CancellationToken cancellationToken) { 378 var dataset = EgoUtilities.GetDataSet(DataSamples.ToList()); 355 379 var problemdata = new RegressionProblemData(dataset, dataset.VariableNames.Where(x => !x.Equals("output")), "output"); 356 380 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; 360 384 361 385 //train … … 369 393 solution = results.Select(x => x.Value).OfType<IRegressionSolution>().SingleOrDefault(); 370 394 r2 = solution?.TrainingRSquared ?? 0; 395 cancellationToken.ThrowIfCancellationRequested(); 371 396 } 372 ResultsQualities.Rows["DEBUG:ModelBuildingIterations"].Values.Add(i); 397 373 398 if (solution == null) throw new ArgumentException("The Algorithm did not return a Model"); 399 RegressionAlgorithm.Runs.Clear(); 374 400 return solution; 375 401 } … … 381 407 if (infillProblem.Problem != Problem) throw new ArgumentException("Expensive real problem is not correctly set in InfillProblem. Problem with Eventhandling?"); 382 408 infillProblem.RegressionSolution = ResultsModel; 409 if (MaximalDatasetSize > 0 && MaximalDatasetSize < Samples.Count) { infillProblem.Encoding.Bounds = EgoUtilities.GetBoundingBox(DataSamples.Select(x => x.Item1)); } 383 410 384 411 RealVector bestVector = null; … … 406 433 } 407 434 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)); 413 436 } 414 437 private void Analyze() { … … 422 445 ResultsQualitiesIteration.Values.Add(Samples[Samples.Count - 1].Item2); 423 446 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); 424 453 } 425 454 #endregion -
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EgoUtilities.cs
r14741 r14768 52 52 53 53 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 54 65 EventWaitHandle trigger = new AutoResetEvent(false); 55 66 Exception ex = null; … … 103 114 return new Dataset(names.Concat(new[] { "output" }).ToArray(), data); 104 115 } 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 } 105 130 } 106 131 } -
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/HeuristicLab.Algorithms.EGO-3.4.csproj
r14741 r14768 67 67 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Encodings.RealVectorEncoding-3.3.dll</HintPath> 68 68 </Reference> 69 <Reference Include="HeuristicLab.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec" /> 69 70 <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 70 71 <SpecificVersion>False</SpecificVersion> … … 112 113 <Compile Include="InfillCriteria\InfillCriterionBase.cs" /> 113 114 <Compile Include="InfillCriteria\ExpectedImprovement.cs" /> 115 <Compile Include="Interfaces\ISurrogateAlgorithm.cs" /> 114 116 <Compile Include="Interfaces\IInfillCriterion.cs" /> 115 117 <Compile Include="Plugin.cs" /> -
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/InfillCriteria/ConfidenceBound.cs
r14741 r14768 52 52 get { return ConfidenceWeightParameter.Value.Value; } 53 53 } 54 55 54 #endregion 56 55 -
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/InfillCriteria/ExpectedImprovement.cs
r14741 r14768 53 53 get { return ExploitationWeightParameter.Value.Value; } 54 54 } 55 56 55 #endregion 57 56 -
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/InfillCriteria/RobustImprovement.cs
r14741 r14768 52 52 get { return ConfidenceWeightParameter.Value.Value; } 53 53 } 54 55 54 #endregion 56 55 -
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Plugin.cs.frame
r14741 r14768 38 38 [PluginDependency("HeuristicLab.Data", "3.3")] 39 39 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 40 [PluginDependency("HeuristicLab.Operators","3.3")] 40 41 [PluginDependency("HeuristicLab.Optimization","3.3")] 41 42 [PluginDependency("HeuristicLab.Parameters","3.3")] -
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Problems/InfillProblem.cs
r14741 r14768 21 21 22 22 using System; 23 using HeuristicLab.Algorithms.EGO;24 23 using HeuristicLab.Common; 25 24 using HeuristicLab.Core; … … 30 29 using HeuristicLab.Problems.DataAnalysis; 31 30 32 namespace HeuristicLab. Problems.SurrogateProblem{31 namespace HeuristicLab.Algorithms.EGO { 33 32 [StorableClass] 34 33 [Item("InfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")] 35 34 public sealed class InfillProblem : SingleObjectiveBasicProblem<RealVectorEncoding> { 36 35 37 [Storable]38 36 public override bool Maximization => true; 39 37 … … 60 58 if (problem == null) return; 61 59 Encoding = problem.Encoding as RealVectorEncoding; 60 SolutionCreator = new UniformRandomRealVectorCreator();//ignore Problem specific Solution Creation 62 61 if (Encoding == null) throw new ArgumentException("EGO can not be performed on non-RealVectorEncodings"); 63 62 } -
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Properties
-
Property
svn:ignore
set to
AssemblyInfo.cs
-
Property
svn:ignore
set to
Note: See TracChangeset
for help on using the changeset viewer.