Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/18/15 17:23:48 (8 years ago)
Author:
ascheibe
Message:

#1674 merged r13180,r13183,r13203,r13212,r13257 into stable

Location:
stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/SingleObjectiveOptimizationSupportScript.cs

    r12009 r13259  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Reflection;
    2623using HeuristicLab.Common;
    2724using HeuristicLab.Core;
     
    2926using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3027using HeuristicLab.Problems.ExternalEvaluation.Programmable;
    31 using HeuristicLab.Scripting;
    3228
    3329namespace HeuristicLab.Problems.ExternalEvaluation {
    3430  [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
    3531  [StorableClass]
    36   public sealed class SingleObjectiveOptimizationSupportScript : Script, ISingleObjectiveOptimizationSupport {
    37 
    38     [Storable]
    39     private VariableStore variableStore;
    40     public VariableStore VariableStore {
    41       get { return variableStore; }
    42     }
     32  public sealed class SingleObjectiveOptimizationSupportScript : OptimizationSupportScript<ISingleObjectiveOptimizationSupport>, ISingleObjectiveOptimizationSupport {
    4333
    4434    protected override string CodeTemplate {
     
    4838    [StorableConstructor]
    4939    private SingleObjectiveOptimizationSupportScript(bool deserializing) : base(deserializing) { }
    50     private SingleObjectiveOptimizationSupportScript(SingleObjectiveOptimizationSupportScript original, Cloner cloner)
    51       : base(original, cloner) {
    52       variableStore = cloner.Clone(original.variableStore);
    53     }
    54     public SingleObjectiveOptimizationSupportScript()
    55       : base() {
    56       variableStore = new VariableStore();
    57     }
     40    private SingleObjectiveOptimizationSupportScript(SingleObjectiveOptimizationSupportScript original, Cloner cloner) : base(original, cloner) { }
     41    public SingleObjectiveOptimizationSupportScript() : base() { }
    5842
    5943    public override IDeepCloneable Clone(Cloner cloner) {
    6044      return new SingleObjectiveOptimizationSupportScript(this, cloner);
    61     }
    62 
    63     private readonly object compileLock = new object();
    64     private volatile ISingleObjectiveOptimizationSupport compiledInstance;
    65     private ISingleObjectiveOptimizationSupport CompiledInstance {
    66       get {
    67         if (compiledInstance == null) {
    68           lock (compileLock) {
    69             if (compiledInstance == null) {
    70               Compile();
    71             }
    72           }
    73         }
    74         return compiledInstance;
    75       }
    76       set { compiledInstance = value; }
    77     }
    78 
    79     public override Assembly Compile() {
    80       var assembly = base.Compile();
    81       var types = assembly.GetTypes();
    82       if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)))
    83         throw new SingleObjectiveOptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport.");
    84       if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1)
    85         throw new SingleObjectiveOptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed.");
    86 
    87       CompiledOptimizationSupport inst;
    88       try {
    89         inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));
    90         inst.vars = new Variables(VariableStore);
    91       } catch (Exception e) {
    92         compiledInstance = null;
    93         throw new SingleObjectiveOptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e);
    94       }
    95 
    96       var soInst = inst as ISingleObjectiveOptimizationSupport;
    97       if (soInst == null)
    98         throw new SingleObjectiveOptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
    99 
    100       CompiledInstance = soInst;
    101 
    102       return assembly;
    103     }
    104 
    105     protected override void OnCodeChanged() {
    106       base.OnCodeChanged();
    107       compiledInstance = null;
    10845    }
    10946
Note: See TracChangeset for help on using the changeset viewer.