Changeset 11893 for branches/ProgrammableProblem
- Timestamp:
- 02/04/15 21:38:28 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4
- Files:
-
- 4 added
- 3 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4
- Property svn:ignore
-
old new 5 5 *.vs10x 6 6 Plugin.cs 7 *.DotSettings
-
- Property svn:ignore
-
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs
r11892 r11893 29 29 using HeuristicLab.Core; 30 30 using HeuristicLab.Data; 31 using HeuristicLab.Optimization; 31 32 using HeuristicLab.Parameters; 32 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 54 55 get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; } 55 56 } 57 public IFixedValueParameter<SingleObjectiveOptimizationSupportScript> SupportScriptParameter { 58 get { return (IFixedValueParameter<SingleObjectiveOptimizationSupportScript>)Parameters["SupportScript"]; } 59 } 56 60 57 61 public EvaluationCache Cache { … … 64 68 get { return MessageBuilderParameter.Value; } 65 69 } 70 public SingleObjectiveOptimizationSupportScript OptimizationSupportScript { 71 get { return SupportScriptParameter.Value; } 72 } 73 private ISingleObjectiveOptimizationSupport OptimizationSupport { 74 get { return SupportScriptParameter.Value; } 75 } 66 76 67 77 [StorableConstructor] 68 78 private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { } 69 private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { } 79 80 private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) 81 : base(original, cloner) { 82 try { 83 OptimizationSupportScript.Compile(); 84 } catch (SingleObjectiveOptimizationSupportException ex) { 85 PluginInfrastructure.ErrorHandling.ShowErrorDialog("Support script doesn't compile.", ex); 86 } 87 } 70 88 public override IDeepCloneable Clone(Cloner cloner) { 71 89 return new ExternalEvaluationProblem(this, cloner); … … 76 94 Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() })); 77 95 Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder())); 96 Parameters.Add(new FixedValueParameter<SingleObjectiveOptimizationSupportScript>("SupportScript", "A script that can provide neighborhood and analyze the results of the optimization.", new SingleObjectiveOptimizationSupportScript())); 97 } 98 99 [StorableHook(HookType.AfterDeserialization)] 100 private void AfterDeserialization() { 101 try { 102 OptimizationSupportScript.Compile(); 103 } catch (SingleObjectiveOptimizationSupportException ex) { 104 PluginInfrastructure.ErrorHandling.ShowErrorDialog("Support script doesn't compile.", ex); 105 } 78 106 } 79 107 … … 85 113 return Cache == null ? EvaluateOnNextAvailableClient(BuildSolutionMessage(individual)).Quality 86 114 : Cache.GetValue(BuildSolutionMessage(individual), m => EvaluateOnNextAvailableClient(m).Quality); 115 } 116 117 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 118 OptimizationSupport.Analyze(individuals, qualities, results, random); 119 } 120 121 public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) { 122 return OptimizationSupport.GetNeighbors(individual, random); 87 123 } 88 124 -
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/HeuristicLab.Problems.ExternalEvaluation-3.4.csproj
r11892 r11893 105 105 <Private>False</Private> 106 106 </Reference> 107 <Reference Include="HeuristicLab.Scripting-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 108 <SpecificVersion>False</SpecificVersion> 109 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Scripting-3.3.dll</HintPath> 110 <Private>False</Private> 111 </Reference> 107 112 <Reference Include="System" /> 108 113 <Reference Include="System.Core"> … … 138 143 <Compile Include="Interfaces\IEvaluationChannel.cs" /> 139 144 <Compile Include="Interfaces\IItemToSolutionMessageConverter.cs" /> 145 <Compile Include="Interfaces\ISingleObjectiveOptimizationSupport.cs" /> 140 146 <Compile Include="Plugin.cs" /> 147 <Compile Include="Programmable\CompiledOptimizationSupport.cs" /> 148 <Compile Include="Programmable\CompiledSingleObjectiveOptimizationSupport.cs" /> 149 <Compile Include="Programmable\SingleObjectiveOptimizationSupportScript.cs" /> 150 <Compile Include="Programmable\SingleObjectiveOptimizationSupportScriptException.cs" /> 151 <Compile Include="Programmable\Templates.Designer.cs"> 152 <AutoGen>True</AutoGen> 153 <DesignTime>True</DesignTime> 154 <DependentUpon>Templates.resx</DependentUpon> 155 </Compile> 141 156 <Compile Include="Properties\AssemblyInfo.cs" /> 142 157 <None Include="Properties\AssemblyInfo.cs.frame" /> … … 234 249 <Install>true</Install> 235 250 </BootstrapperPackage> 251 </ItemGroup> 252 <ItemGroup> 253 <EmbeddedResource Include="Programmable\Templates.resx"> 254 <Generator>ResXFileCodeGenerator</Generator> 255 <LastGenOutput>Templates.Designer.cs</LastGenOutput> 256 </EmbeddedResource> 236 257 </ItemGroup> 237 258 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/CompiledOptimizationSupport.cs
r11886 r11893 20 20 #endregion 21 21 22 using System; 22 using System.Collections.Generic; 23 using HeuristicLab.Core; 24 using HeuristicLab.Optimization; 25 using HeuristicLab.Problems.Programmable; 23 26 24 namespace HeuristicLab.Problems.Programmable { 25 public abstract class CompiledProblemDefinition : IProblemDefinition { 26 private IEncoding encoding; 27 public IEncoding Encoding { 28 get { return encoding; } 29 protected set { 30 if (value == null) throw new ArgumentNullException("The encoding must not be null."); 31 encoding = value; 32 } 33 } 27 namespace HeuristicLab.Problems.ExternalEvaluation { 28 public abstract class CompiledOptimizationSupport { 34 29 35 30 public dynamic vars { get; set; } 36 public abstract void Initialize();37 38 protected CompiledProblemDefinition() { }39 protected CompiledProblemDefinition(IEncoding encoding)40 : base() {41 Encoding = encoding;42 }43 31 } 44 32 } -
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/CompiledSingleObjectiveOptimizationSupport.cs
r11886 r11893 5 5 using HeuristicLab.Core; 6 6 using HeuristicLab.Data; 7 using HeuristicLab.Encodings.PermutationEncoding;8 7 using HeuristicLab.Optimization; 9 8 using HeuristicLab.Problems.Programmable; 10 9 11 namespace HeuristicLab.Problems.Programmable { 12 public class CompiledSingleObjectiveProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition { 13 public bool Maximization { get { return false; } } 14 15 public override void Initialize() { 16 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 17 // Define the solution encoding which can also consist of multiple vectors, examples below 18 //Encoding = new BinaryEncoding("b", length: 5); 19 //Encoding = new IntegerEncoding("i", length: 5, min: 2, max: 14, step: 2); 20 //Encoding = new RealEncoding("r", length: 5, min: -1.0, max: 1.0); 21 //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute); 22 // The encoding can also be a combination 23 //Encoding = new MultiEncoding() 24 //.Add(new BinaryEncoding("b", length: 5)) 25 //.Add(new IntegerEncoding("i", length: 5, min: 2, max: 14, step: 4)) 26 //.Add(new RealEncoding("r", length: 5, min: -1.0, max: 1.0)) 27 //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute)) 28 ; 29 // Add additional initialization code e.g. private variables that you need for evaluating 30 } 31 32 public double Evaluate(Individual individual, IRandom random) { 33 // Use vars.yourVariable to access variables in the variable store i.e. yourVariable 34 var quality = 0.0; 35 //quality = individual.RealVector("r").Sum(x => x * x); 36 return quality; 37 } 10 namespace HeuristicLab.Problems.ExternalEvaluation { 11 public class CompiledSingleObjectiveOptimizationSupport : CompiledOptimizationSupport, ISingleObjectiveOptimizationSupport { 38 12 39 13 public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { -
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjectiveOptimizationSupportScript.cs
r11886 r11893 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using System.Reflection; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 28 using HeuristicLab.Optimization; 27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Problems.ExternalEvaluation.Programmable; 31 using HeuristicLab.Problems.Programmable; 28 32 using HeuristicLab.Scripting; 29 33 30 namespace HeuristicLab.Problems. Programmable{34 namespace HeuristicLab.Problems.ExternalEvaluation { 31 35 [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")] 32 36 [StorableClass] 33 public abstract class ProblemDefinitionScript : Script, IProblemDefinition { 34 protected bool SuppressEvents { get; set; } 37 public sealed class SingleObjectiveOptimizationSupportScript : Script, ISingleObjectiveOptimizationSupport { 35 38 36 39 [Storable] … … 40 43 } 41 44 45 protected override string CodeTemplate { 46 get { return Templates.CompiledSingleObjectiveOptimizationSupport; } 47 } 48 42 49 [StorableConstructor] 43 pr otected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }44 pr otected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)50 private SingleObjectiveOptimizationSupportScript(bool deserializing) : base(deserializing) { } 51 private SingleObjectiveOptimizationSupportScript(SingleObjectiveOptimizationSupportScript original, Cloner cloner) 45 52 : base(original, cloner) { 46 53 variableStore = cloner.Clone(original.variableStore); 47 54 } 48 p rotected ProblemDefinitionScript()55 public SingleObjectiveOptimizationSupportScript() 49 56 : base() { 50 57 variableStore = new VariableStore(); 51 58 } 52 59 53 IEncoding IProblemDefinition.Encoding{54 get { return CompiledProblemDefinition.Encoding; }60 public override IDeepCloneable Clone(Cloner cloner) { 61 return new SingleObjectiveOptimizationSupportScript(this, cloner); 55 62 } 56 63 57 private readonly object locker = new object(); 58 private volatile IProblemDefinition compiledProblemDefinition; 59 protected IProblemDefinition CompiledProblemDefinition { 64 private volatile ISingleObjectiveOptimizationSupport compiledInstance; 65 private ISingleObjectiveOptimizationSupport CompiledInstance { 60 66 get { 61 if (compiled ProblemDefinition== null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used.");62 return compiled ProblemDefinition;67 if (compiledInstance == null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used."); 68 return compiledInstance; 63 69 } 64 private set { 65 compiledProblemDefinition = value; 66 OnProblemDefinitionChanged(); 67 } 70 set { compiledInstance = value; } 68 71 } 69 72 … … 71 74 var assembly = base.Compile(); 72 75 var types = assembly.GetTypes(); 73 if (!types.Any(x => typeof(Compiled ProblemDefinition).IsAssignableFrom(x)))74 throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");75 if (types.Count(x => typeof(Compiled ProblemDefinition).IsAssignableFrom(x)) > 1)76 throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinitionis allowed.");76 if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x))) 77 throw new SingleObjectiveOptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport."); 78 if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1) 79 throw new SingleObjectiveOptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed."); 77 80 78 Compiled ProblemDefinitioninst;81 CompiledOptimizationSupport inst; 79 82 try { 80 inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x))); 83 inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x))); 84 inst.vars = new Variables(VariableStore); 81 85 } catch (Exception e) { 82 compiled ProblemDefinition= null;83 throw new ProblemDefinitionScriptException("Instantiating the problem definitionfailed." + Environment.NewLine + "Check your default constructor.", e);86 compiledInstance = null; 87 throw new SingleObjectiveOptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e); 84 88 } 85 89 86 try { 87 inst.vars = new Variables(VariableStore); 88 inst.Initialize(); 89 } catch (Exception e) { 90 compiledProblemDefinition = null; 91 throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e); 92 } 90 var soInst = inst as ISingleObjectiveOptimizationSupport; 91 if (soInst == null) 92 throw new SingleObjectiveOptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport."); 93 93 94 try { 95 CompiledProblemDefinition = inst; 96 } catch (Exception e) { 97 compiledProblemDefinition = null; 98 throw new ProblemDefinitionScriptException("Using the problem definition in the problem failed." + Environment.NewLine + "Examine this error message carefully (often there is an issue with the defined encoding).", e); 99 } 94 CompiledInstance = soInst; 100 95 101 96 return assembly; … … 104 99 protected override void OnCodeChanged() { 105 100 base.OnCodeChanged(); 106 compiled ProblemDefinition= null;101 compiledInstance = null; 107 102 } 108 103 109 public event EventHandler ProblemDefinitionChanged; 110 protected virtual void OnProblemDefinitionChanged() { 111 var handler = ProblemDefinitionChanged; 112 if (handler != null) handler(this, EventArgs.Empty); 104 void ISingleObjectiveOptimizationSupport.Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 105 CompiledInstance.Analyze(individuals, qualities, results, random); 106 } 107 108 IEnumerable<Individual> ISingleObjectiveOptimizationSupport.GetNeighbors(Individual individual, IRandom random) { 109 return CompiledInstance.GetNeighbors(individual, random); 113 110 } 114 111 } -
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjectiveOptimizationSupportScriptException.cs
r11886 r11893 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2015 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; 4 23 using System.Runtime.Serialization; 5 using System.Text;6 using System.Threading.Tasks;7 24 8 namespace HeuristicLab.Problems. Programmable{25 namespace HeuristicLab.Problems.ExternalEvaluation { 9 26 [Serializable] 10 public class ProblemDefinitionScriptException : Exception {11 public ProblemDefinitionScriptException() { }12 public ProblemDefinitionScriptException(string message) : base(message) { }13 public ProblemDefinitionScriptException(string message, Exception inner) : base(message, inner) { }27 public class SingleObjectiveOptimizationSupportException : Exception { 28 public SingleObjectiveOptimizationSupportException() { } 29 public SingleObjectiveOptimizationSupportException(string message) : base(message) { } 30 public SingleObjectiveOptimizationSupportException(string message, Exception inner) : base(message, inner) { } 14 31 15 protected ProblemDefinitionScriptException(SerializationInfo info, StreamingContext context)32 protected SingleObjectiveOptimizationSupportException(SerializationInfo info, StreamingContext context) 16 33 : base(info, context) { } 17 34 }
Note: See TracChangeset
for help on using the changeset viewer.