source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs @ 11739

Last change on this file since 11739 was 11739, checked in by mkommend, 5 years ago

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

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Reflection;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Scripting;
30
31namespace HeuristicLab.Problems.Programmable {
32  [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
33  [StorableClass]
34  public abstract class ProblemDefinitionScript : Script, IProblemDefinition {
35    protected bool SuppressEvents { get; set; }
36
37    [Storable]
38    private VariableStore variableStore;
39    public VariableStore VariableStore {
40      get { return variableStore; }
41    }
42
43    [StorableConstructor]
44    protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
45    protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
46      : base(original, cloner) {
47      variableStore = cloner.Clone(original.variableStore);
48    }
49    protected ProblemDefinitionScript() :base(){
50      variableStore = new VariableStore();
51    }
52
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 {
63      get {
64          lock (locker) {
65            if (compiledProblemDefinition == null) {
66              Compile();
67            }
68          }
69        return compiledProblemDefinition;
70      }
71      private set {
72        compiledProblemDefinition = value;
73        OnProblemDefinitionChanged();
74      }
75    }
76
77    public override Assembly Compile() {
78      var assembly = base.Compile();
79      var types = assembly.GetTypes();
80      try {
81        var inst = (CompiledProblemDefinition)Activator.CreateInstance(types.First(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
82        inst.vars = new Variables(VariableStore);
83        inst.Initialize();
84        CompiledProblemDefinition = inst;
85      }
86      catch {
87        compiledProblemDefinition = null;
88      }
89      return assembly;
90    }
91
92    protected override void OnCodeChanged() {
93      base.OnCodeChanged();
94      compiledProblemDefinition = null;
95    }
96
97    public event EventHandler ProblemDefinitionChanged;
98    protected virtual void OnProblemDefinitionChanged() {
99      var handler = ProblemDefinitionChanged;
100      if (handler != null) handler(this, EventArgs.Empty);
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.