Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/08/15 15:52:05 (9 years ago)
Author:
mkommend
Message:

#2174: Worked on operators and programmable problem base classes and scripts.

Location:
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New
Files:
2 added
1 moved

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs

    r11738 r11739  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using System.Reflection;
     
    3132  [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
    3233  [StorableClass]
    33   public abstract class ProblemScript : Script {
     34  public abstract class ProblemDefinitionScript : Script, IProblemDefinition {
    3435    protected bool SuppressEvents { get; set; }
    3536
     
    4142
    4243    [StorableConstructor]
    43     protected ProblemScript(bool deserializing) : base(deserializing) { }
    44     protected ProblemScript(ProblemScript original, Cloner cloner)
     44    protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
     45    protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
    4546      : base(original, cloner) {
    4647      variableStore = cloner.Clone(original.variableStore);
    4748    }
    48     protected ProblemScript() {
     49    protected ProblemDefinitionScript() :base(){
    4950      variableStore = new VariableStore();
    5051    }
    5152
    52     private volatile IProblemDefinition instance;
    53     private object locker = new object();
    54     public IProblemDefinition Instance {
     53    IEncoding IProblemDefinition.Encoding {
     54      get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Encoding : null; }
     55    }
     56    IEnumerable<Individual> IProblemDefinition.GetNeighbors(Individual individual, IRandom random) {
     57      return CompiledProblemDefinition.GetNeighbors(individual, random);
     58    }
     59
     60    private readonly object locker = new object();
     61    private volatile IProblemDefinition compiledProblemDefinition;
     62    protected IProblemDefinition CompiledProblemDefinition {
    5563      get {
    56         SuppressEvents = true;
    57         try {
    58           var oldInstance = instance;
    59           var compilationNecessary = false;
    60           if (instance == null) {
    61             lock (locker) {
    62               if (instance == null) {
    63                 compilationNecessary = true;
    64                 Compile();
    65               }
     64          lock (locker) {
     65            if (compiledProblemDefinition == null) {
     66              Compile();
    6667            }
    6768          }
    68           if (compilationNecessary && (oldInstance != null || instance != null))
    69             OnInstanceChanged();
    70           return instance;
    71         } finally {
    72           SuppressEvents = false;
    73         }
     69        return compiledProblemDefinition;
    7470      }
    75       protected set {
    76         instance = value;
    77         if (!SuppressEvents) OnInstanceChanged();
     71      private set {
     72        compiledProblemDefinition = value;
     73        OnProblemDefinitionChanged();
    7874      }
    7975    }
     
    8379      var types = assembly.GetTypes();
    8480      try {
    85         var inst = (ProblemScriptBase)Activator.CreateInstance(types.First(x => typeof(ProblemScriptBase).IsAssignableFrom(x)));
     81        var inst = (CompiledProblemDefinition)Activator.CreateInstance(types.First(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
    8682        inst.vars = new Variables(VariableStore);
    8783        inst.Initialize();
    88         Instance = inst;
    89       } catch {
    90         Instance = null;
     84        CompiledProblemDefinition = inst;
     85      }
     86      catch {
     87        compiledProblemDefinition = null;
    9188      }
    9289      return assembly;
     
    9592    protected override void OnCodeChanged() {
    9693      base.OnCodeChanged();
    97       instance = null;
     94      compiledProblemDefinition = null;
    9895    }
    9996
    100     public event EventHandler InstanceChanged;
    101     protected virtual void OnInstanceChanged() {
    102       var handler = InstanceChanged;
     97    public event EventHandler ProblemDefinitionChanged;
     98    protected virtual void OnProblemDefinitionChanged() {
     99      var handler = ProblemDefinitionChanged;
    103100      if (handler != null) handler(this, EventArgs.Empty);
    104101    }
Note: See TracChangeset for help on using the changeset viewer.