Generated on for Gecode by doxygen 1.17.0
gecode-instance.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
5 *
6 * Copyright:
7 * Mikael Zayenz Lagerkvist, 2026
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34/*
35 * gecode-instance: read instance files with reader specs
36 *
37 */
38
39#include <gecode/instance.hh>
40
41#include <cstring>
42#include <iostream>
43
44using namespace Gecode;
45
46namespace {
47
48 void
49 usage(std::ostream& os) {
50 os << "usage: gecode-instance -spec SPEC [options] FILE...\n"
51 << "\n"
52 << "Reads instance files with the reader spec in file SPEC and\n"
53 << "prints the data they contain.\n"
54 << "\n"
55 << "options:\n"
56 << " -spec SPEC file with the reader spec\n"
57 << " -summary print names, types, and shapes (default)\n"
58 << " -dzn print the data as MiniZinc data\n"
59 << " -list list the instances in each file, and the\n"
60 << " first failure of each invalid one\n"
61 << " -param N=V give value V to spec parameter N; repeat,\n"
62 << " or separate by commas, for several; use\n"
63 << " instance=K or instance=NAME to select an\n"
64 << " instance in a file with several\n"
65 << " -check SPEC check that spec file SPEC parses\n"
66 << " -help print this help\n";
67 }
68
71 int
72 usage_error(const std::string& m) {
73 std::cerr << "gecode-instance: " << m << "\n";
74 usage(std::cerr);
75 return 2;
76 }
77
79 void
80 print_shape(std::ostream& os, const Instance::Data& d, const std::string& n) {
81 Instance::ValueType t = d.type(n);
82 const char* tn = (t == Instance::ValueType::INT) ? "int" : ((t == Instance::ValueType::FLOAT) ? "float" : "string");
83 switch (d.depth(n)) {
84 case 0:
85 os << tn << " ";
86 if (t == Instance::ValueType::INT) os << d.integer(n);
87 else if (t == Instance::ValueType::FLOAT) os << d.number(n);
88 else os << '"' << d.text(n) << '"';
89 return;
90 case 1:
91 os << tn << " array " << d.size(n);
92 return;
93 default: {
94 // Report the range of row lengths for the second level
95 int rows = d.size(n), lo = 0, hi = 0;
96 for (int i=0; i<rows; i++) {
97 int l = d.size(n, {i});
98 if ((i == 0) || (l < lo)) lo = l;
99 if ((i == 0) || (l > hi)) hi = l;
100 }
101 os << tn << " array " << rows << " x " << lo;
102 if (hi != lo) os << ".." << hi;
103 for (int k=2; k<d.depth(n); k++)
104 os << " x ...";
105 return;
106 }
107 }
108 }
109
111 std::string
112 option(const char* a) {
113 std::string o(a);
114 if ((o.size() > 2) && (o[0] == '-') && (o[1] == '-'))
115 o = o.substr(1);
116 return o;
117 }
118
120 bool
121 valued(const std::string& o) {
122 return (o == "-spec") || (o == "-param") || (o == "-check");
123 }
124
126 bool
127 is_file(const std::string& o) {
128 return (o.size() <= 1) || (o[0] != '-');
129 }
130
132 void
133 process(const std::string& f, const Instance::Spec& spec, const Instance::Params& params, const std::string& mode) {
134 if (mode == "list") {
135 Instance::Data c = spec.catalog(f, params);
136 int n = static_cast<int>(c.integer("instances"));
137 std::cout << f << ": " << n << " instance" << (n == 1 ? "" : "s") << "\n";
138 // Every instance if they have names, otherwise those that failed
139 bool names = c.has("name"), failed = c.has("failed");
140 for (int i=0; (names || failed) && (i<n); i++) {
141 std::string m = failed ? c.text("failed", {i}) : std::string();
142 if (!names && m.empty())
143 continue;
144 std::cout << " " << (i+1);
145 if (names)
146 std::cout << " " << c.text("name", {i});
147 if (!m.empty())
148 std::cout << " (failed: " << m << ")";
149 std::cout << "\n";
150 }
151 return;
152 }
153 Instance::Data d = spec.read(f, params);
154 if (mode == "dzn") {
155 d.dzn(std::cout);
156 return;
157 }
158 std::cout << f;
159 if (!spec.name().empty())
160 std::cout << " [" << spec.name() << "]";
161 std::cout << "\n";
162 for (int n=0; n<d.entries(); n++) {
163 std::cout << " " << d.name(n) << ": ";
164 print_shape(std::cout, d, d.name(n));
165 std::cout << "\n";
166 }
167 }
168
169}
170
171int
172main(int argc, char* argv[]) {
173 std::string mode = "summary", spec_file;
174 Instance::Params params;
175 int files = 0;
176 // All options apply to all files, wherever they are given
177 for (int i=1; i<argc; i++) {
178 std::string o = option(argv[i]);
179 if (valued(o) && (i+1 >= argc))
180 return usage_error("option " + o + " needs a value");
181 if ((o == "-summary") || (o == "-dzn") || (o == "-list")) {
182 mode = o.substr(1);
183 } else if (o == "-spec") {
184 spec_file = argv[++i];
185 } else if (o == "-param") {
186 try {
187 params.set(argv[++i]);
188 } catch (const Instance::Error& e) {
189 return usage_error(std::string("-param: ") + e.what());
190 }
191 } else if (o == "-check") {
192 files++; i++;
193 } else if (o == "-help") {
194 usage(std::cout);
195 return 0;
196 } else if (!is_file(o)) {
197 return usage_error(std::string("unknown option ") + argv[i]);
198 } else {
199 files++;
200 }
201 }
202 if (files == 0)
203 return usage_error("no files given");
204 try {
205 Instance::Spec spec;
206 bool loaded = false;
207 for (int i=1; i<argc; i++) {
208 std::string o = option(argv[i]);
209 if (o == "-check") {
210 std::string f = argv[++i];
212 std::cout << f << ": ok";
213 if (!c.name().empty())
214 std::cout << " (format " << c.name() << ")";
215 std::cout << "\n";
216 } else if (valued(o)) {
217 i++;
218 } else if (is_file(o)) {
219 if (spec_file.empty())
220 return usage_error("no reader spec given (use -spec SPEC)");
221 if (!loaded) {
222 spec = Instance::Spec::load(spec_file);
223 loaded = true;
224 }
225 process(argv[i], spec, params, mode);
226 }
227 }
228 } catch (const Instance::Error& e) {
229 std::cerr << e.what() << std::endl;
230 return 1;
231 } catch (const std::exception& e) {
232 // For example, running out of memory
233 std::cerr << "gecode-instance: " << e.what() << std::endl;
234 return 1;
235 }
236 return 0;
237}
238
239// STATISTICS: instance-any
Named values read from an instance.
Definition instance.hh:406
Exception for errors in specs and instance files
Definition instance.hh:337
virtual const char * what(void) const noexcept override
Return the full message.
Definition data.cpp:56
Values for the param statements of a spec.
Definition instance.hh:364
Reader spec for a textual instance format.
Definition instance.hh:470
static Spec load(const std::string &path)
Load the spec in file path.
Definition spec.cpp:1275
Data read(const std::string &path, const Params &p=Params()) const
Read instance file path.
Definition spec.cpp:1285
std::string name(void) const
Return the format name declared by the spec (may be empty).
Definition spec.cpp:1280
Data catalog(const std::string &path, const Params &p=Params()) const
List the instances in file path.
Definition spec.cpp:1295
int main(void)
Definition main.cpp:15
Reading instance files described by reader specs.
ValueType
Type of the values in a data entry.
Definition instance.hh:349
@ FLOAT
Floating point numbers.
Definition instance.hh:351
Gecode toplevel namespace
Gecode::IntArgs i({1, 2, 3, 4})
Gecode::IntSet d(v, 7)