Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Problems/LinearProgrammingProblemDefinitionScript.cs @ 16233

Last change on this file since 16233 was 16233, checked in by ddorfmei, 6 years ago

#2931:

  • added all available parameters OR-Tools's linear_solver to LinearProgrammingAlgorithm
    • added necessary parameter enums
  • moved solving logic to Solver
    • created IncrementalSolver, ExternalSolver, ExternalIncrementalSolver
    • added logic for solvers that can be stopped and resumed
  • added SupportsStop property to BasicAlgorithm
  • added quality per time chart for incremental solvers
File size: 6.1 KB
Line 
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
24using System;
25using System.Linq;
26using System.Reflection;
27using Google.OrTools.LinearSolver;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.Programmable;
33using HeuristicLab.Scripting;
34
35namespace 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}
Note: See TracBrowser for help on using the repository browser.