Changeset 10754 for branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveScript.cs
- Timestamp:
- 04/15/14 22:34:05 (11 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
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.