Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/ProgrammableProblemScript.cs @ 10856

Last change on this file since 10856 was 10856, checked in by abeham, 10 years ago

#2174: Worked on programmable problem

  • Changed ProblemBase to IProblemDefinition and SingleObjectiveProblemBase to ISingleObjectiveProblemDefinition
  • Derived ParameterVectorCreater, -Crossover, and -Manipulator from MultiOperator<> instead of InstrumentedOperator
  • Split the megamoth ScriptOnInstanceChanged to multiple methods dealing with single-vector and multi-vector encodings separately, it's still a lot of tedious code
  • Removed maximization from Configuration again (would not be consistent with multi-objective problems)
File size: 3.1 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("ProgrammableProblemScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
32  [StorableClass]
33  public abstract class ProgrammableProblemScript : Script {
34    protected bool SuppressEvents { get; set; }
35
36    [StorableConstructor]
37    protected ProgrammableProblemScript(bool deserializing) : base(deserializing) { }
38    protected ProgrammableProblemScript(ProgrammableProblemScript original, Cloner cloner)
39      : base(original, cloner) { }
40    public ProgrammableProblemScript() { }
41
42    private volatile IProblemDefinition instance;
43    private object locker = new object();
44    public IProblemDefinition Instance {
45      get {
46        SuppressEvents = true;
47        try {
48          var oldInstance = instance;
49          var compilationNecessary = false;
50          if (instance == null) {
51            lock (locker) {
52              if (instance == null) {
53                compilationNecessary = true;
54                Compile();
55              }
56            }
57          }
58          if (compilationNecessary && (oldInstance != null || instance != null))
59            OnInstanceChanged();
60          return instance;
61        } finally {
62          SuppressEvents = false;
63        }
64      }
65      protected set {
66        instance = value;
67        if (!SuppressEvents) OnInstanceChanged();
68      }
69    }
70
71    public override Assembly Compile() {
72      var assembly = base.Compile();
73      var types = assembly.GetTypes();
74      try {
75        Instance = (IProblemDefinition)Activator.CreateInstance(types.First(x => typeof(IProblemDefinition).IsAssignableFrom(x)));
76      } catch {
77        Instance = null;
78      }
79      return assembly;
80    }
81
82    protected override void OnCodeChanged() {
83      base.OnCodeChanged();
84      instance = null;
85    }
86
87    public event EventHandler InstanceChanged;
88    protected void OnInstanceChanged() {
89      var handler = InstanceChanged;
90      if (handler != null) handler(this, EventArgs.Empty);
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.