Changeset 13259 for stable/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjectiveOptimizationSupportScript.cs
- Timestamp:
- 11/18/15 17:23:48 (8 years ago)
- Location:
- stable
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 13180,13183,13203,13212,13257
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjectiveOptimizationSupportScript.cs
r12009 r13259 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 using System.Linq;25 using System.Reflection;26 23 using HeuristicLab.Common; 27 24 using HeuristicLab.Core; … … 29 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 27 using HeuristicLab.Problems.ExternalEvaluation.Programmable; 31 using HeuristicLab.Scripting;32 28 33 29 namespace HeuristicLab.Problems.ExternalEvaluation { 34 30 [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")] 35 31 [StorableClass] 36 public sealed class SingleObjectiveOptimizationSupportScript : Script, ISingleObjectiveOptimizationSupport { 37 38 [Storable] 39 private VariableStore variableStore; 40 public VariableStore VariableStore { 41 get { return variableStore; } 42 } 32 public sealed class SingleObjectiveOptimizationSupportScript : OptimizationSupportScript<ISingleObjectiveOptimizationSupport>, ISingleObjectiveOptimizationSupport { 43 33 44 34 protected override string CodeTemplate { … … 48 38 [StorableConstructor] 49 39 private SingleObjectiveOptimizationSupportScript(bool deserializing) : base(deserializing) { } 50 private SingleObjectiveOptimizationSupportScript(SingleObjectiveOptimizationSupportScript original, Cloner cloner) 51 : base(original, cloner) { 52 variableStore = cloner.Clone(original.variableStore); 53 } 54 public SingleObjectiveOptimizationSupportScript() 55 : base() { 56 variableStore = new VariableStore(); 57 } 40 private SingleObjectiveOptimizationSupportScript(SingleObjectiveOptimizationSupportScript original, Cloner cloner) : base(original, cloner) { } 41 public SingleObjectiveOptimizationSupportScript() : base() { } 58 42 59 43 public override IDeepCloneable Clone(Cloner cloner) { 60 44 return new SingleObjectiveOptimizationSupportScript(this, cloner); 61 }62 63 private readonly object compileLock = new object();64 private volatile ISingleObjectiveOptimizationSupport compiledInstance;65 private ISingleObjectiveOptimizationSupport CompiledInstance {66 get {67 if (compiledInstance == null) {68 lock (compileLock) {69 if (compiledInstance == null) {70 Compile();71 }72 }73 }74 return compiledInstance;75 }76 set { compiledInstance = value; }77 }78 79 public override Assembly Compile() {80 var assembly = base.Compile();81 var types = assembly.GetTypes();82 if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)))83 throw new SingleObjectiveOptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport.");84 if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1)85 throw new SingleObjectiveOptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed.");86 87 CompiledOptimizationSupport inst;88 try {89 inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));90 inst.vars = new Variables(VariableStore);91 } catch (Exception e) {92 compiledInstance = null;93 throw new SingleObjectiveOptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e);94 }95 96 var soInst = inst as ISingleObjectiveOptimizationSupport;97 if (soInst == null)98 throw new SingleObjectiveOptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");99 100 CompiledInstance = soInst;101 102 return assembly;103 }104 105 protected override void OnCodeChanged() {106 base.OnCodeChanged();107 compiledInstance = null;108 45 } 109 46
Note: See TracChangeset
for help on using the changeset viewer.