Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
dow_parser.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cstdint>
17#include <string>
18#include <vector>
19
20#include "absl/log/check.h"
21#include "absl/status/status.h"
22#include "absl/strings/numbers.h"
23#include "absl/strings/str_cat.h"
24#include "absl/strings/str_split.h"
25#include "absl/strings/string_view.h"
29#include "ortools/routing/parsers/capacity_planning.pb.h"
31
32namespace operations_research {
33::absl::Status ReadFile(absl::string_view file_name,
34 CapacityPlanningInstance* request) {
35 if (!file::Exists(file_name, file::Defaults()).ok()) {
36 return absl::NotFoundError(absl::StrCat(file_name, " not found"));
37 }
38 int line_num = 0;
39 int num_nodes = 0;
40 int num_arcs = 0;
41 int num_commodities = 0;
42 int arc_num = 0;
43 int commodity_num = 0;
44 for (const std::string& line :
46 if (line == "MULTIGEN.DAT:") {
47 CHECK_EQ(line_num, 0);
48 ++line_num;
49 continue;
50 }
51 const std::vector<std::string> fields =
52 absl::StrSplit(line, absl::ByAnyChar(" \t"), absl::SkipEmpty());
53 if (fields.size() == 3) {
54 if (line_num == 1) { // Sizes.
55 CHECK(absl::SimpleAtoi(fields[0], &num_nodes));
56 CHECK(absl::SimpleAtoi(fields[1], &num_arcs));
57 CHECK(absl::SimpleAtoi(fields[2], &num_commodities));
58 VLOG(1) << "num_nodes = " << num_nodes << ", num_arcs = " << num_arcs
59 << ", num_commodities = " << num_commodities;
60 } else { // Demand per commodity. The commodity number is implicit.
61 CHECK_GT(line_num, num_arcs + 1);
62 CHECK_LE(line_num, num_arcs + num_commodities + 1);
63 int from_node;
64 CHECK(absl::SimpleAtoi(fields[0], &from_node));
65 int to_node;
66 CHECK(absl::SimpleAtoi(fields[1], &to_node));
67 int64_t demand;
68 CHECK(absl::SimpleAtoi(fields[2], &demand));
69 CHECK_GT(demand, 0);
70 ++commodity_num;
71 request->mutable_commodities()->add_from_node(from_node);
72 request->mutable_commodities()->add_to_node(to_node);
73 request->mutable_commodities()->add_demand(demand);
74 }
75 } else if (fields.size() == 7) { // Information per arc. Arc number is
76 // implicit.
77 CHECK_GT(line_num, 1);
78 CHECK_LE(line_num, num_arcs + 1);
79 CHECK_LT(arc_num, num_arcs);
80 int from_node;
81 CHECK(absl::SimpleAtoi(fields[0], &from_node));
82 int to_node;
83 CHECK(absl::SimpleAtoi(fields[1], &to_node));
84 int variable_cost;
85 CHECK(absl::SimpleAtoi(fields[2], &variable_cost));
86 int capacity;
87 CHECK(absl::SimpleAtoi(fields[3], &capacity));
88 int fixed_cost;
89 CHECK(absl::SimpleAtoi(fields[4], &fixed_cost));
90 int unused;
91 CHECK(absl::SimpleAtoi(fields[5], &unused));
92 CHECK(absl::SimpleAtoi(fields[6], &unused));
93 ++arc_num;
94 request->mutable_topology()->add_from_node(from_node);
95 request->mutable_topology()->add_to_node(to_node);
96 request->mutable_topology()->add_variable_cost(variable_cost);
97 request->mutable_topology()->add_capacity(capacity);
98 request->mutable_topology()->add_fixed_cost(fixed_cost);
99 }
100 ++line_num;
101 }
102 CHECK_EQ(commodity_num, num_commodities);
103 return absl::OkStatus();
104}
105
106} // namespace operations_research
absl::Status Exists(absl::string_view path, Options options)
Definition file.cc:380
Options Defaults()
Definition file.h:109
In SWIG mode, we don't want anything besides these top-level includes.
::absl::Status ReadFile(absl::string_view file_name, CapacityPlanningInstance *request)
Definition dow_parser.cc:33
int line
int64_t demand
Definition resource.cc:126
int64_t fixed_cost
Contrarily to CostClass, here we need strict equivalence.
Definition routing.cc:1338