Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2931: solved the issues found during the review

File size: 3.7 KB
Line 
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
22using System.Drawing;
23using System.IO;
24using Google.OrTools.LinearSolver;
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;
32
33namespace HeuristicLab.ExactOptimization.LinearProgramming {
34
35  [Item("Linear Problem Definition File (MPS, OR-Tools Protocol Buffers Files)", "File that defines the model for linear/mixed integer programming problem.")]
36  [StorableClass]
37  public sealed class FileBasedLinearProblemDefinition : ParameterizedNamedItem, ILinearProblemDefinition {
38
39    [Storable]
40    private readonly IFixedValueParameter<FileValue> fileNameParam;
41
42    [Storable]
43    private byte[] fileContent;
44
45    public FileBasedLinearProblemDefinition() {
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 FileBasedLinearProblemDefinition(FileBasedLinearProblemDefinition original, Cloner cloner)
60      : base(original, cloner) {
61      fileNameParam = cloner.Clone(original.fileNameParam);
62    }
63
64    [StorableConstructor]
65    private FileBasedLinearProblemDefinition(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
79    public void BuildModel(Solver solver) {
80      var fileInfo = new FileInfo(FileName);
81      var tempFileName = Path.GetTempFileName();
82
83      try {
84        File.WriteAllBytes(tempFileName, fileContent);
85
86        var status = (SolverResponseStatus)(fileInfo.Extension == ".mps"
87          ? solver.ImportModelFromMpsFormat(tempFileName)
88          : solver.ImportModelFromProtoFormat(tempFileName));
89
90        if (status == SolverResponseStatus.Abnormal)
91          throw new FileFormatException($"'{FileName}' is not a valid MPS or Google OR-Tools Protocol Buffers file.");
92      } finally {
93        File.Delete(tempFileName);
94      }
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) => new FileBasedLinearProblemDefinition(this, cloner);
98  }
99}
Note: See TracBrowser for help on using the repository browser.