Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16405 was 16405, checked in by ddorfmei, 5 years ago

#2931:

  • moved views to separate plugin HeuristicLab.MathematicalOptimization.Views
  • added button in LinearProgrammingProblemView to select the problem definition type
  • added views for problem definitions
  • added ExportFile parameter to LinearProgrammingAlgorithm
  • extended FileValue and FileValueView by the option to save a file
  • code cleanup
File size: 3.7 KB
RevLine 
[16288]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
[16405]22using System.Drawing;
23using System.IO;
[16288]24using Google.OrTools.LinearSolver;
[16405]25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[16172]32
[16405]33namespace HeuristicLab.MathematicalOptimization.LinearProgramming {
[16233]34
[16405]35  [Item("File-Based Linear/Mixed Integer Programming Problem Definition", "File that defines the model for linear/mixed integer programming problem.")]
36  [StorableClass]
37  public sealed class FileBasedLinearProgrammingProblemDefinition : ParameterizedNamedItem, ILinearProgrammingProblemDefinition {
[16172]38
[16405]39    [Storable]
40    private readonly IFixedValueParameter<FileValue> fileNameParam;
41
42    [Storable]
43    private byte[] fileContent;
44
45    public FileBasedLinearProgrammingProblemDefinition() {
46      Parameters.Add(fileNameParam = new FixedValueParameter<FileValue>(nameof(FileName), new FileValue()));
47      fileNameParam.Value.FileDialogFilter =
48        "All Supported Files (*.mps;*.bin;*.prototxt)|*.mps;*.bin;*.prototxt|" +
49        "Mathematical Programming System Files (*.mps)|*.mps|" +
50        "Google OR-Tools Protocol Buffers Files (*.bin;*.prototxt)|*.bin;*.prototxt|" +
51        "All Files (*.*)|*.*";
52      fileNameParam.Value.PathChanged += (o, e) => {
53        if (File.Exists(FileName)) {
54          fileContent = File.ReadAllBytes(FileName);
55        }
56      };
57    }
58
59    private FileBasedLinearProgrammingProblemDefinition(FileBasedLinearProgrammingProblemDefinition original, Cloner cloner)
60      : base(original, cloner) {
61      fileNameParam = cloner.Clone(original.fileNameParam);
62    }
63
64    [StorableConstructor]
65    private FileBasedLinearProgrammingProblemDefinition(bool deserializing) : base(deserializing) { }
66
67    public new static Image StaticItemImage => VSImageLibrary.File;
68
69    public string FileName {
70      get => fileNameParam.Value.Value;
71      set => fileNameParam.Value.Value = value;
72    }
73
74    public IFixedValueParameter<FileValue> FileNameParam => fileNameParam;
75
76    public void Analyze(Solver solver, ResultCollection results) {
77    }
78
[16233]79    public void BuildModel(Solver solver) {
[16405]80      var fileInfo = new FileInfo(FileName);
81      var tempFileName = Path.GetTempFileName();
82      File.WriteAllBytes(tempFileName, fileContent);
83
84      var status = (SolverResponseStatus)(fileInfo.Extension == ".mps"
85        ? solver.ImportModelFromMpsFormat(tempFileName)
86        : solver.ImportModelFromProtoFormat(tempFileName));
87
88      if (status == SolverResponseStatus.Abnormal)
89        throw new FileFormatException($"'{FileName}' is not a valid MPS or Google OR-Tools Protocol Buffers file.");
90
91      File.Delete(tempFileName);
[16172]92    }
93
[16405]94    public override IDeepCloneable Clone(Cloner cloner) => new FileBasedLinearProgrammingProblemDefinition(this, cloner);
[16172]95  }
[16288]96}
Note: See TracBrowser for help on using the repository browser.