Changeset 11893 for branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable
- Timestamp:
- 02/04/15 21:38:28 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4
- Files:
-
- 3 added
- 1 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/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.