Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExactOptimization/3.3/LinearProgramming/Problems/FileBasedLinearProblemDefinition.cs @ 16988

Last change on this file since 16988 was 16910, checked in by abeham, 6 years ago

#2931: merged to trunk

  • Fixed references in plugins
  • Fixed output path
  • Removed app.config from views plugin
  • Reverted changes to PathValue and FileValue and their respective views
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;
[16910]25using HEAL.Attic;
[16405]26using HeuristicLab.Common;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
[16172]32
[16582]33namespace HeuristicLab.ExactOptimization.LinearProgramming {
[16233]34
[16582]35  [Item("Linear Problem Definition File (MPS, OR-Tools Protocol Buffers Files)", "File that defines the model for linear/mixed integer programming problem.")]
[16736]36  [StorableType("8779D26D-37DB-4595-A4E9-C7F29B870802")]
[16582]37  public sealed class FileBasedLinearProblemDefinition : ParameterizedNamedItem, ILinearProblemDefinition {
[16172]38
[16405]39    [Storable]
40    private readonly IFixedValueParameter<FileValue> fileNameParam;
41
42    [Storable]
43    private byte[] fileContent;
44
[16582]45    public FileBasedLinearProblemDefinition() {
[16405]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 (*.*)|*.*";
[16910]52      fileNameParam.Value.StringValue.ValueChanged += (o, e) => {
[16405]53        if (File.Exists(FileName)) {
54          fileContent = File.ReadAllBytes(FileName);
55        }
56      };
57    }
58
[16582]59    private FileBasedLinearProblemDefinition(FileBasedLinearProblemDefinition original, Cloner cloner)
[16405]60      : base(original, cloner) {
61      fileNameParam = cloner.Clone(original.fileNameParam);
62    }
63
64    [StorableConstructor]
[16736]65    private FileBasedLinearProblemDefinition(StorableConstructorFlag _) : base(_) { }
[16405]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
[16582]83      try {
84        File.WriteAllBytes(tempFileName, fileContent);
[16405]85
[16582]86        var status = (SolverResponseStatus)(fileInfo.Extension == ".mps"
87          ? solver.ImportModelFromMpsFormat(tempFileName)
88          : solver.ImportModelFromProtoFormat(tempFileName));
[16405]89
[16582]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      }
[16172]95    }
96
[16582]97    public override IDeepCloneable Clone(Cloner cloner) => new FileBasedLinearProblemDefinition(this, cloner);
[16172]98  }
[16288]99}
Note: See TracBrowser for help on using the repository browser.