Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/15/14 22:34:05 (10 years ago)
Author:
abeham
Message:

#2174: minor refactorings

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveScript.cs

    r10753 r10754  
    2020#endregion
    2121
    22 using System;
    23 using System.Linq;
    24 using System.Reflection;
    2522using HeuristicLab.Common;
    2623using HeuristicLab.Core;
    2724using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using HeuristicLab.Scripting;
    2925
    3026namespace 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.")]
    3228  [StorableClass]
    33   public class ProgrammableProblemScript : Script, IStorableContent {
     29  public class SingleObjectiveScript : ProgrammableProblemScript, IStorableContent {
    3430    public string Filename { get; set; }
    35     protected bool SuppressEvents { get; set; }
    3631
    3732    [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)
    4035      : base(original, cloner) { }
    41     public ProgrammableProblemScript() { }
     36    public SingleObjectiveScript() { }
    4237
    4338    public override IDeepCloneable Clone(Cloner cloner) {
    44       return new ProgrammableProblemScript(this, cloner);
     39      return new SingleObjectiveScript(this, cloner);
    4540    }
    4641
    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; }
    7445    }
    7546
     
    8556using HeuristicLab.Encodings.PermutationEncoding;
    8657
    87 public class MyProblem : HeuristicLab.Problems.Programmable.ProgrammableProblemBase {
     58public class MyProblem : HeuristicLab.Problems.Programmable.SingleObjectiveProblemBase {
    8859  public MyProblem() {
    8960    // initialize private fields
     
    9970  }
    10071
    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 
    10872  public override double Evaluate(IRandom random, ParameterVector vector) {
    10973    var quality = 0.0;
     74    // quality = vector.Boolean(""Parameter"") ? vector.Real(""Parameter"") : vector.Integer(""Parameter"");
    11075    return quality;
    11176  }
     
    11580      }
    11681    }
    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     }
    14082  }
    14183}
Note: See TracChangeset for help on using the changeset viewer.