- Timestamp:
- 04/15/14 22:34:05 (10 years ago)
- Location:
- branches/SimSharp/HeuristicLab.Problems.Programmable/3.3
- Files:
-
- 2 edited
- 3 copied
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj
r10753 r10754 139 139 <Compile Include="Operators\ParameterVectorManipulator.cs" /> 140 140 <Compile Include="Operators\ParameterVectorCrossover.cs" /> 141 <Compile Include="Operators\Si mOptSingleObjectiveEvaluator.cs" />141 <Compile Include="Operators\SingleObjectiveEvaluator.cs" /> 142 142 <Compile Include="Operators\ParameterVectorCreater.cs" /> 143 <Compile Include="ProgrammableProblemBase.cs" /> 143 <Compile Include="ProblemBase.cs" /> 144 <Compile Include="SingleObjectiveProblemBase.cs" /> 144 145 <Compile Include="ProgrammableProblemScript.cs" /> 146 <Compile Include="SingleObjectiveScript.cs" /> 145 147 <Compile Include="SingleObjectiveProgrammableProblem.cs" /> 146 148 <None Include="HeuristicLab.snk" /> -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveEvaluator.cs
r10753 r10754 31 31 32 32 namespace HeuristicLab.Problems.Programmable { 33 [Item("Si mOptSingleObjectiveEvaluator", "Evaluates a parameter vector as received from a SimOptRunner.")]33 [Item("Single-objective Evaluator", "Calls the script's Evaluate method to get the quality value of the parameter vector.")] 34 34 [StorableClass] 35 public class Si mOptSingleObjectiveEvaluator : SingleSuccessorOperator, ISingleObjectiveProgrammableProblemEvaluator, IStochasticOperator {35 public class SingleObjectiveEvaluator : SingleSuccessorOperator, ISingleObjectiveProgrammableProblemEvaluator, IStochasticOperator { 36 36 37 37 public ILookupParameter<IRandom> RandomParameter { … … 39 39 } 40 40 41 public ILookupParameter< ProgrammableProblemScript> ScriptParameter {42 get { return (ILookupParameter< ProgrammableProblemScript>)Parameters["Script"]; }41 public ILookupParameter<SingleObjectiveScript> ScriptParameter { 42 get { return (ILookupParameter<SingleObjectiveScript>)Parameters["Script"]; } 43 43 } 44 44 … … 52 52 53 53 [StorableConstructor] 54 protected Si mOptSingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }55 protected Si mOptSingleObjectiveEvaluator(SimOptSingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }56 public Si mOptSingleObjectiveEvaluator() {54 protected SingleObjectiveEvaluator(bool deserializing) : base(deserializing) { } 55 protected SingleObjectiveEvaluator(SingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { } 56 public SingleObjectiveEvaluator() { 57 57 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 58 Parameters.Add(new LookupParameter< ProgrammableProblemScript>("Script", "The script that will execute the evaluation function and define the parameter vector."));58 Parameters.Add(new LookupParameter<SingleObjectiveScript>("Script", "The script that will execute the evaluation function and define the parameter vector.")); 59 59 Parameters.Add(new LookupParameter<ParameterVector>("ParameterVector", "The vector that holds the parameters.")); 60 60 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); … … 62 62 63 63 public override IDeepCloneable Clone(Cloner cloner) { 64 return new Si mOptSingleObjectiveEvaluator(this, cloner);64 return new SingleObjectiveEvaluator(this, cloner); 65 65 } 66 66 -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/ProblemBase.cs
r10753 r10754 20 20 #endregion 21 21 22 using HeuristicLab.Core;23 22 using HeuristicLab.Encodings.ParameterVectorEncoding; 24 23 25 24 namespace HeuristicLab.Problems.Programmable { 26 public abstract class ProgrammableProblemBase { 27 public abstract bool IsMaximizationProblem { get; } 25 public abstract class ProblemBase { 28 26 public abstract ParameterVector GetParametersToOptimize(); 29 public abstract void EvaluateFeasibility(ParameterVector vector);30 public abstract double Evaluate(IRandom random, ParameterVector vector);31 27 } 32 28 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/ProgrammableProblemScript.cs
r10753 r10754 29 29 30 30 namespace HeuristicLab.Problems.Programmable { 31 [Item("ProgrammableProblemScript", "Script that defines the parameter vector and evaluates the solution .")]31 [Item("ProgrammableProblemScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")] 32 32 [StorableClass] 33 public class ProgrammableProblemScript : Script, IStorableContent { 34 public string Filename { get; set; } 33 public abstract class ProgrammableProblemScript : Script { 35 34 protected bool SuppressEvents { get; set; } 36 35 … … 41 40 public ProgrammableProblemScript() { } 42 41 43 public override IDeepCloneable Clone(Cloner cloner) { 44 return new ProgrammableProblemScript(this, cloner); 45 } 46 47 private volatile ProgrammableProblemBase instance; 42 private volatile ProblemBase instance; 48 43 private object locker = new object(); 49 public Pro grammableProblemBase Instance {44 public ProblemBase Instance { 50 45 get { 51 46 SuppressEvents = true; … … 70 65 protected set { 71 66 instance = value; 72 OnInstanceChanged(); 73 } 74 } 75 76 protected override string CodeTemplate { 77 get { 78 return @"using System; 79 using System.Linq; 80 using System.Collections.Generic; 81 using HeuristicLab.Common; 82 using HeuristicLab.Core; 83 using HeuristicLab.Data; 84 using HeuristicLab.Encodings.ParameterVectorEncoding; 85 using HeuristicLab.Encodings.PermutationEncoding; 86 87 public class MyProblem : HeuristicLab.Problems.Programmable.ProgrammableProblemBase { 88 public MyProblem() { 89 // initialize private fields 90 } 91 public override bool IsMaximizationProblem { get { return false; } } 92 public override ParameterVector GetParametersToOptimize() { 93 return new ParameterVectorBuilder() 94 // .AddBoolean(""Parameter"") 95 // .AddInteger(""Parameter"", min: 5, max: 15, step: 3) 96 // .AddReal(""Parameter"", min: 0.0, max: 1.0) 97 // .SetPermutation(type: PermutationTypes.Absolute, length: 10) 98 .Build(); 99 } 100 101 public override void EvaluateFeasibility(ParameterVector vector) { 102 vector.IsFeasible = true 103 // && vector.Integer(""Parameter1"") < vector.Integer(""Parameter2""); 104 // && (vector.Real(""Parameter3"") >= 1.0 || (vector.Real(""Parameter3"") < 1.0 && vector.Integer(""Parameter1"") < 5)); 105 ; 106 } 107 108 public override double Evaluate(IRandom random, ParameterVector vector) { 109 var quality = 0.0; 110 return quality; 111 } 112 113 // implement further classes and methods 114 }"; 67 if (!SuppressEvents) OnInstanceChanged(); 115 68 } 116 69 } … … 120 73 var types = assembly.GetTypes(); 121 74 try { 122 instance = (ProgrammableProblemBase)Activator.CreateInstance(types.First(x => typeof(ProgrammableProblemBase).IsAssignableFrom(x)));75 Instance = (ProblemBase)Activator.CreateInstance(types.First(x => typeof(ProblemBase).IsAssignableFrom(x))); 123 76 } catch { 124 instance = null;77 Instance = null; 125 78 } 126 if (!SuppressEvents) OnInstanceChanged();127 79 return assembly; 128 80 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemBase.cs
r10753 r10754 24 24 25 25 namespace HeuristicLab.Problems.Programmable { 26 public abstract class ProgrammableProblemBase {26 public abstract class SingleObjectiveProblemBase : ProblemBase { 27 27 public abstract bool IsMaximizationProblem { get; } 28 public abstract ParameterVector GetParametersToOptimize();29 public abstract void EvaluateFeasibility(ParameterVector vector);30 28 public abstract double Evaluate(IRandom random, ParameterVector vector); 31 29 } -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r10753 r10754 47 47 } 48 48 49 public IValueParameter< ProgrammableProblemScript> ScriptParameter {50 get { return (IValueParameter< ProgrammableProblemScript>)Parameters["Script"]; }49 public IValueParameter<SingleObjectiveScript> ScriptParameter { 50 get { return (IValueParameter<SingleObjectiveScript>)Parameters["Script"]; } 51 51 } 52 52 … … 89 89 private SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner) 90 90 : base(original, cloner) { 91 Crossover = cloner.Clone(original.Crossover); 92 Manipulator = cloner.Clone(original.Manipulator); 91 93 RegisterEventHandlers(); 92 94 } 93 95 public SingleObjectiveProgrammableProblem() 94 : base(new Si mOptSingleObjectiveEvaluator(), new ParameterVectorCreater()) {95 Parameters.Add(new ValueParameter< ProgrammableProblemScript>("Script", "Defines the problem.", new ProgrammableProblemScript() { Name = ItemName }));96 : base(new SingleObjectiveEvaluator(), new ParameterVectorCreater()) { 97 Parameters.Add(new ValueParameter<SingleObjectiveScript>("Script", "Defines the problem.", new SingleObjectiveScript() { Name = ItemName })); 96 98 Parameters.Add(new OptionalValueParameter<IntValue>("BinaryVectorLength", "The length of the binary vector.")); 97 99 Parameters.Add(new OptionalValueParameter<IntValue>("IntegerVectorLength", "The length of the integer vector.")); -
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveScript.cs
r10753 r10754 20 20 #endregion 21 21 22 using System;23 using System.Linq;24 using System.Reflection;25 22 using HeuristicLab.Common; 26 23 using HeuristicLab.Core; 27 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Scripting;29 25 30 26 namespace HeuristicLab.Problems.Programmable { 31 [Item(" ProgrammableProblemScript", "Script that defines the parameter vector and evaluates the solution.")]27 [Item("Single-objective Script", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")] 32 28 [StorableClass] 33 public class ProgrammableProblemScript :Script, IStorableContent {29 public class SingleObjectiveScript : ProgrammableProblemScript, IStorableContent { 34 30 public string Filename { get; set; } 35 protected bool SuppressEvents { get; set; }36 31 37 32 [StorableConstructor] 38 protected ProgrammableProblemScript(bool deserializing) : base(deserializing) { }39 protected ProgrammableProblemScript(ProgrammableProblemScript original, Cloner cloner)33 protected SingleObjectiveScript(bool deserializing) : base(deserializing) { } 34 protected SingleObjectiveScript(SingleObjectiveScript original, Cloner cloner) 40 35 : base(original, cloner) { } 41 public ProgrammableProblemScript() { }36 public SingleObjectiveScript() { } 42 37 43 38 public override IDeepCloneable Clone(Cloner cloner) { 44 return new ProgrammableProblemScript(this, cloner);39 return new SingleObjectiveScript(this, cloner); 45 40 } 46 41 47 private volatile ProgrammableProblemBase instance; 48 private object locker = new object(); 49 public ProgrammableProblemBase Instance { 50 get { 51 SuppressEvents = true; 52 try { 53 var oldInstance = instance; 54 var compilationNecessary = false; 55 if (instance == null) { 56 lock (locker) { 57 if (instance == null) { 58 compilationNecessary = true; 59 Compile(); 60 } 61 } 62 } 63 if (compilationNecessary && (oldInstance != null || instance != null)) 64 OnInstanceChanged(); 65 return instance; 66 } finally { 67 SuppressEvents = false; 68 } 69 } 70 protected set { 71 instance = value; 72 OnInstanceChanged(); 73 } 42 public new SingleObjectiveProblemBase Instance { 43 get { return (SingleObjectiveProblemBase)base.Instance; } 44 protected set { base.Instance = value; } 74 45 } 75 46 … … 85 56 using HeuristicLab.Encodings.PermutationEncoding; 86 57 87 public class MyProblem : HeuristicLab.Problems.Programmable. ProgrammableProblemBase {58 public class MyProblem : HeuristicLab.Problems.Programmable.SingleObjectiveProblemBase { 88 59 public MyProblem() { 89 60 // initialize private fields … … 99 70 } 100 71 101 public override void EvaluateFeasibility(ParameterVector vector) {102 vector.IsFeasible = true103 // && vector.Integer(""Parameter1"") < vector.Integer(""Parameter2"");104 // && (vector.Real(""Parameter3"") >= 1.0 || (vector.Real(""Parameter3"") < 1.0 && vector.Integer(""Parameter1"") < 5));105 ;106 }107 108 72 public override double Evaluate(IRandom random, ParameterVector vector) { 109 73 var quality = 0.0; 74 // quality = vector.Boolean(""Parameter"") ? vector.Real(""Parameter"") : vector.Integer(""Parameter""); 110 75 return quality; 111 76 } … … 115 80 } 116 81 } 117 118 public override Assembly Compile() {119 var assembly = base.Compile();120 var types = assembly.GetTypes();121 try {122 instance = (ProgrammableProblemBase)Activator.CreateInstance(types.First(x => typeof(ProgrammableProblemBase).IsAssignableFrom(x)));123 } catch {124 instance = null;125 }126 if (!SuppressEvents) OnInstanceChanged();127 return assembly;128 }129 130 protected override void OnCodeChanged() {131 base.OnCodeChanged();132 instance = null;133 }134 135 public event EventHandler InstanceChanged;136 protected void OnInstanceChanged() {137 var handler = InstanceChanged;138 if (handler != null) handler(this, EventArgs.Empty);139 }140 82 } 141 83 }
Note: See TracChangeset
for help on using the changeset viewer.