Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11885 was 11885, checked in by abeham, 9 years ago

#2174:

  • Some refactorings and bug fixes
  • Renamed (Binary|Integer|Real)Encoding to (Binary|Integer|Real)VectorEncoding
  • Improved error messages when compiling programmable problems
File size: 4.7 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.Linq;
24using System.Reflection;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Scripting;
29
30namespace HeuristicLab.Problems.Programmable {
31  [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
32  [StorableClass]
33  public abstract class ProblemDefinitionScript : Script, IProblemDefinition {
34    protected bool SuppressEvents { get; set; }
35
36    [Storable]
37    private VariableStore variableStore;
38    public VariableStore VariableStore {
39      get { return variableStore; }
40    }
41
42    [StorableConstructor]
43    protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
44    protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
45      : base(original, cloner) {
46      variableStore = cloner.Clone(original.variableStore);
47    }
48    protected ProblemDefinitionScript()
49      : base() {
50      variableStore = new VariableStore();
51    }
52
53    IEncoding IProblemDefinition.Encoding {
54      get { return CompiledProblemDefinition.Encoding; }
55    }
56
57    private readonly object locker = new object();
58    private volatile IProblemDefinition compiledProblemDefinition;
59    protected IProblemDefinition CompiledProblemDefinition {
60      get {
61        if (compiledProblemDefinition == null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used.");
62        return compiledProblemDefinition;
63      }
64      private set {
65        compiledProblemDefinition = value;
66        OnProblemDefinitionChanged();
67      }
68    }
69
70    public override Assembly Compile() {
71      var assembly = base.Compile();
72      var types = assembly.GetTypes();
73      if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)))
74        throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");
75      if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1)
76        throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed.");
77
78      CompiledProblemDefinition inst;
79      try {
80        inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
81      } catch (Exception e) {
82        compiledProblemDefinition = null;
83        throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
84      }
85
86      try {
87        inst.vars = new Variables(VariableStore);
88        inst.Initialize();
89      } catch (Exception e) {
90        compiledProblemDefinition = null;
91        throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
92      }
93
94      try {
95        CompiledProblemDefinition = inst;
96      } catch (Exception e) {
97        compiledProblemDefinition = null;
98        throw new ProblemDefinitionScriptException("Using the problem definition in the problem failed." + Environment.NewLine + "Examine this error message carefully (often there is an issue with the defined encoding).", e);
99      }
100
101      return assembly;
102    }
103
104    protected override void OnCodeChanged() {
105      base.OnCodeChanged();
106      compiledProblemDefinition = null;
107    }
108
109    public event EventHandler ProblemDefinitionChanged;
110    protected virtual void OnProblemDefinitionChanged() {
111      var handler = ProblemDefinitionChanged;
112      if (handler != null) handler(this, EventArgs.Empty);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.