1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #endregion License Information
|
---|
23 |
|
---|
24 | using System;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using Google.OrTools.LinearSolver;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.Programmable;
|
---|
33 | using HeuristicLab.Scripting;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.MathematicalOptimization.LinearProgramming.Problems {
|
---|
36 |
|
---|
37 | [Item("Single-objective Problem Definition Script", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class LinearProgrammingProblemDefinitionScript : Script, ILinearProgrammingProblemDefinition, IStorableContent {
|
---|
40 | protected bool SuppressEvents { get; set; }
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | private VariableStore variableStore;
|
---|
44 |
|
---|
45 | public VariableStore VariableStore => variableStore;
|
---|
46 |
|
---|
47 | [Storable]
|
---|
48 | private bool codeChanged;
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected LinearProgrammingProblemDefinitionScript(bool deserializing) : base(deserializing) { }
|
---|
52 |
|
---|
53 | protected LinearProgrammingProblemDefinitionScript(LinearProgrammingProblemDefinitionScript original, Cloner cloner)
|
---|
54 | : base(original, cloner) {
|
---|
55 | variableStore = cloner.Clone(original.variableStore);
|
---|
56 | codeChanged = original.codeChanged;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public LinearProgrammingProblemDefinitionScript()
|
---|
60 | : base(ScriptTemplates.CompiledLinearProgrammingProblemDefinition) {
|
---|
61 | variableStore = new VariableStore();
|
---|
62 | }
|
---|
63 |
|
---|
64 | private readonly object compileLock = new object();
|
---|
65 | private volatile ILinearProgrammingProblemDefinition compiledProblemDefinition;
|
---|
66 |
|
---|
67 | protected ILinearProgrammingProblemDefinition CompiledProblemDefinition {
|
---|
68 | get {
|
---|
69 | // double checked locking pattern
|
---|
70 | if (compiledProblemDefinition == null) {
|
---|
71 | lock (compileLock) {
|
---|
72 | if (compiledProblemDefinition == null) {
|
---|
73 | if (codeChanged)
|
---|
74 | throw new ProblemDefinitionScriptException("The code has been changed, but was not recompiled.");
|
---|
75 | Compile(false);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return compiledProblemDefinition;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public dynamic Instance => compiledProblemDefinition;
|
---|
84 |
|
---|
85 | public override Assembly Compile() => Compile(true);
|
---|
86 |
|
---|
87 | private Assembly Compile(bool fireChanged) {
|
---|
88 | var assembly = base.Compile();
|
---|
89 | var types = assembly.GetTypes();
|
---|
90 | if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)))
|
---|
91 | throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." +
|
---|
92 | Environment.NewLine +
|
---|
93 | "The problem definition must be a subclass of CompiledProblemDefinition.");
|
---|
94 | if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1)
|
---|
95 | throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." +
|
---|
96 | Environment.NewLine +
|
---|
97 | "Only one subclass of CompiledProblemDefinition is allowed.");
|
---|
98 |
|
---|
99 | CompiledProblemDefinition inst;
|
---|
100 | try {
|
---|
101 | inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x =>
|
---|
102 | typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
|
---|
103 | } catch (Exception e) {
|
---|
104 | compiledProblemDefinition = null;
|
---|
105 | throw new ProblemDefinitionScriptException(
|
---|
106 | "Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
|
---|
107 | }
|
---|
108 |
|
---|
109 | try {
|
---|
110 | inst.vars = new Variables(VariableStore);
|
---|
111 | inst.Initialize();
|
---|
112 | } catch (Exception e) {
|
---|
113 | compiledProblemDefinition = null;
|
---|
114 | throw new ProblemDefinitionScriptException(
|
---|
115 | "Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
|
---|
116 | }
|
---|
117 |
|
---|
118 | try {
|
---|
119 | compiledProblemDefinition = (ILinearProgrammingProblemDefinition)inst;
|
---|
120 | if (fireChanged) OnProblemDefinitionChanged();
|
---|
121 | } catch (Exception e) {
|
---|
122 | compiledProblemDefinition = null;
|
---|
123 | throw new ProblemDefinitionScriptException(
|
---|
124 | "Using the problem definition in the problem failed." + Environment.NewLine +
|
---|
125 | "Examine this error message carefully (often there is an issue with the defined encoding).", e);
|
---|
126 | }
|
---|
127 |
|
---|
128 | codeChanged = false;
|
---|
129 | return assembly;
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected override void OnCodeChanged() {
|
---|
133 | base.OnCodeChanged();
|
---|
134 | compiledProblemDefinition = null;
|
---|
135 | codeChanged = true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public event EventHandler ProblemDefinitionChanged;
|
---|
139 |
|
---|
140 | private void OnProblemDefinitionChanged() => ProblemDefinitionChanged?.Invoke(this, EventArgs.Empty);
|
---|
141 |
|
---|
142 | public string Filename { get; set; }
|
---|
143 |
|
---|
144 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
145 | return new LinearProgrammingProblemDefinitionScript(this, cloner);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void BuildModel(Solver solver) => CompiledProblemDefinition.BuildModel(solver);
|
---|
149 |
|
---|
150 | public void Analyze(Solver solver, ResultCollection results) => CompiledProblemDefinition.Analyze(solver, results);
|
---|
151 | }
|
---|
152 | } |
---|