Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10754 for branches/SimSharp


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

#2174: minor refactorings

Location:
branches/SimSharp
Files:
3 edited
3 copied
2 moved

Legend:

Unmodified
Added
Removed
  • branches/SimSharp/HeuristicLab.Encodings.ParameterVector/ParameterVectorBuilder.cs

    r10753 r10754  
    3232
    3333    public ParameterVectorBuilder() {
    34       booleanParameters = new HashSet<string>();
    35       integerParameters = new Dictionary<string, Tuple<int, int, int?>>();
    36       realParameters = new Dictionary<string, Tuple<double, double>>();
     34      booleanParameters = null;
     35      integerParameters = null;
     36      realParameters = null;
    3737      permutationParameter = null;
    3838    }
    3939
    4040    public ParameterVectorBuilder AddBoolean(string name) {
     41      if (booleanParameters == null) booleanParameters = new HashSet<string>();
    4142      booleanParameters.Add(name);
    4243      return this;
     
    4445
    4546    public ParameterVectorBuilder AddInteger(string name, int min, int max, int? step = null) {
     47      if (integerParameters == null) integerParameters = new Dictionary<string, Tuple<int, int, int?>>();
    4648      integerParameters.Add(name, Tuple.Create(min, max, step));
    4749      return this;
     
    4951
    5052    public ParameterVectorBuilder AddReal(string name, double min, double max) {
     53      if (realParameters == null) realParameters = new Dictionary<string, Tuple<double, double>>();
    5154      realParameters.Add(name, Tuple.Create(min, max));
    5255      return this;
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj

    r10753 r10754  
    139139    <Compile Include="Operators\ParameterVectorManipulator.cs" />
    140140    <Compile Include="Operators\ParameterVectorCrossover.cs" />
    141     <Compile Include="Operators\SimOptSingleObjectiveEvaluator.cs" />
     141    <Compile Include="Operators\SingleObjectiveEvaluator.cs" />
    142142    <Compile Include="Operators\ParameterVectorCreater.cs" />
    143     <Compile Include="ProgrammableProblemBase.cs" />
     143    <Compile Include="ProblemBase.cs" />
     144    <Compile Include="SingleObjectiveProblemBase.cs" />
    144145    <Compile Include="ProgrammableProblemScript.cs" />
     146    <Compile Include="SingleObjectiveScript.cs" />
    145147    <Compile Include="SingleObjectiveProgrammableProblem.cs" />
    146148    <None Include="HeuristicLab.snk" />
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveEvaluator.cs

    r10753 r10754  
    3131
    3232namespace HeuristicLab.Problems.Programmable {
    33   [Item("SimOptSingleObjectiveEvaluator", "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.")]
    3434  [StorableClass]
    35   public class SimOptSingleObjectiveEvaluator : SingleSuccessorOperator, ISingleObjectiveProgrammableProblemEvaluator, IStochasticOperator {
     35  public class SingleObjectiveEvaluator : SingleSuccessorOperator, ISingleObjectiveProgrammableProblemEvaluator, IStochasticOperator {
    3636
    3737    public ILookupParameter<IRandom> RandomParameter {
     
    3939    }
    4040
    41     public ILookupParameter<ProgrammableProblemScript> ScriptParameter {
    42       get { return (ILookupParameter<ProgrammableProblemScript>)Parameters["Script"]; }
     41    public ILookupParameter<SingleObjectiveScript> ScriptParameter {
     42      get { return (ILookupParameter<SingleObjectiveScript>)Parameters["Script"]; }
    4343    }
    4444
     
    5252
    5353    [StorableConstructor]
    54     protected SimOptSingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
    55     protected SimOptSingleObjectiveEvaluator(SimOptSingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
    56     public SimOptSingleObjectiveEvaluator() {
     54    protected SingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
     55    protected SingleObjectiveEvaluator(SingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
     56    public SingleObjectiveEvaluator() {
    5757      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."));
    5959      Parameters.Add(new LookupParameter<ParameterVector>("ParameterVector", "The vector that holds the parameters."));
    6060      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
     
    6262
    6363    public override IDeepCloneable Clone(Cloner cloner) {
    64       return new SimOptSingleObjectiveEvaluator(this, cloner);
     64      return new SingleObjectiveEvaluator(this, cloner);
    6565    }
    6666
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/ProblemBase.cs

    r10753 r10754  
    2020#endregion
    2121
    22 using HeuristicLab.Core;
    2322using HeuristicLab.Encodings.ParameterVectorEncoding;
    2423
    2524namespace HeuristicLab.Problems.Programmable {
    26   public abstract class ProgrammableProblemBase {
    27     public abstract bool IsMaximizationProblem { get; }
     25  public abstract class ProblemBase {
    2826    public abstract ParameterVector GetParametersToOptimize();
    29     public abstract void EvaluateFeasibility(ParameterVector vector);
    30     public abstract double Evaluate(IRandom random, ParameterVector vector);
    3127  }
    3228}
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/ProgrammableProblemScript.cs

    r10753 r10754  
    2929
    3030namespace 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.")]
    3232  [StorableClass]
    33   public class ProgrammableProblemScript : Script, IStorableContent {
    34     public string Filename { get; set; }
     33  public abstract class ProgrammableProblemScript : Script {
    3534    protected bool SuppressEvents { get; set; }
    3635
     
    4140    public ProgrammableProblemScript() { }
    4241
    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;
    4843    private object locker = new object();
    49     public ProgrammableProblemBase Instance {
     44    public ProblemBase Instance {
    5045      get {
    5146        SuppressEvents = true;
     
    7065      protected set {
    7166        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();
    11568      }
    11669    }
     
    12073      var types = assembly.GetTypes();
    12174      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)));
    12376      } catch {
    124         instance = null;
     77        Instance = null;
    12578      }
    126       if (!SuppressEvents) OnInstanceChanged();
    12779      return assembly;
    12880    }
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemBase.cs

    r10753 r10754  
    2424
    2525namespace HeuristicLab.Problems.Programmable {
    26   public abstract class ProgrammableProblemBase {
     26  public abstract class SingleObjectiveProblemBase : ProblemBase {
    2727    public abstract bool IsMaximizationProblem { get; }
    28     public abstract ParameterVector GetParametersToOptimize();
    29     public abstract void EvaluateFeasibility(ParameterVector vector);
    3028    public abstract double Evaluate(IRandom random, ParameterVector vector);
    3129  }
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r10753 r10754  
    4747    }
    4848
    49     public IValueParameter<ProgrammableProblemScript> ScriptParameter {
    50       get { return (IValueParameter<ProgrammableProblemScript>)Parameters["Script"]; }
     49    public IValueParameter<SingleObjectiveScript> ScriptParameter {
     50      get { return (IValueParameter<SingleObjectiveScript>)Parameters["Script"]; }
    5151    }
    5252
     
    8989    private SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
    9090      : base(original, cloner) {
     91      Crossover = cloner.Clone(original.Crossover);
     92      Manipulator = cloner.Clone(original.Manipulator);
    9193      RegisterEventHandlers();
    9294    }
    9395    public SingleObjectiveProgrammableProblem()
    94       : base(new SimOptSingleObjectiveEvaluator(), 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 }));
    9698      Parameters.Add(new OptionalValueParameter<IntValue>("BinaryVectorLength", "The length of the binary vector."));
    9799      Parameters.Add(new OptionalValueParameter<IntValue>("IntegerVectorLength", "The length of the integer vector."));
  • 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.