Generated on for Gecode by doxygen 1.15.0
on_restart_last_val_int.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Jip J. Dekker <jip.dekker@monash.edu>
5 *
6 * Contributing authors:
7 * Nathan Tran <kieron.qtran@gmail.com>
8 *
9 * Copyright:
10 * Jip J. Dekker, 2023
11 * Nathan Tran, 2025
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.dev
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining
18 * a copy of this software and associated documentation files (the
19 * "Software"), to deal in the Software without restriction, including
20 * without limitation the rights to use, copy, modify, merge, publish,
21 * distribute, sublicense, and/or sell copies of the Software, and to
22 * permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be
26 * included in all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37
38#include "test/flatzinc.hh"
39
41
42namespace Test { namespace FlatZinc {
43
44 namespace {
46 class UniformIntInvalidRange : public Base {
47 public:
49 UniformIntInvalidRange(void)
50 : Base("FlatZinc::on_restart::uniform_int_invalid_range") {}
52 virtual bool run(void) {
53 std::stringstream source(R"FZN(
54predicate gecode_on_restart_uniform_int(int: low,int: high,var int: out);
55var 0..1: y;
56constraint gecode_on_restart_uniform_int(1,0,y);
57solve satisfy;
58)FZN");
59 Gecode::FlatZinc::Printer p;
60 std::ostringstream err;
61 Gecode::FlatZinc::FlatZincSpace* fg =
62 Gecode::FlatZinc::parse(source, p, err);
63 const std::string message = err.str();
64 return fg == nullptr &&
65 message.find("gecode_on_restart_uniform_int") != std::string::npos &&
66 message.find("low (1) <= high (0)") != std::string::npos;
67 }
68 };
69
71 class ScriptedRandom {
72 private:
73 const unsigned int* values;
74 unsigned int size;
75 unsigned int next;
76 public:
78 ScriptedRandom(const unsigned int* values0, unsigned int size0)
79 : values(values0), size(size0), next(0) {}
81 unsigned int operator ()(unsigned int n) {
82 if ((n != (1U << 31)) || (next >= size))
83 return 0;
84 return values[next++];
85 }
87 unsigned long long int operator ()(unsigned long long int) {
88 return 0;
89 }
91 bool done(void) const {
92 return next == size;
93 }
94 };
95
97 class WideUniformInt : public Base {
98 public:
100 WideUniformInt(void)
101 : Base("FlatZinc::on_restart::uniform_int_wide_endpoints") {}
103 virtual bool run(void) {
104 {
105 const unsigned int chunks[] = {
106 (1U << 31) - 1U, (1U << 31) - 1U, 1U, 0U
107 };
108 ScriptedRandom random(chunks, 4);
109 const unsigned long long int width = (1ULL << 31) + 1ULL;
110 const unsigned long long int offset =
112 if ((offset != width - 1ULL) ||
113 (static_cast<long long int>(INT_MIN) +
114 static_cast<long long int>(offset) != 0) || !random.done())
115 return false;
116 }
117 {
118 const unsigned int chunks[] = {1U, (1U << 31) - 1U};
119 ScriptedRandom random(chunks, 2);
120 const unsigned long long int width = 1ULL << 32;
121 const unsigned long long int offset =
123 if ((offset != width - 1ULL) ||
124 (static_cast<long long int>(INT_MIN) +
125 static_cast<long long int>(offset) != INT_MAX) || !random.done())
126 return false;
127 }
128 return true;
129 }
130 };
131
133 class Create {
134 public:
135
137 Create(void) {
138 (void) new FlatZincTest("on_restart::last_val_int",
139R"FZN(
140predicate gecode_on_restart_status(var int: s);
141predicate gecode_on_restart_last_val_int(var int: input,var int: out);
142predicate int_lin_eq_imp(array [int] of int: as,array [int] of var int: bs,int: c,var bool: r);
143predicate int_eq_imp(var int: a,var int: b,var bool: r);
144array [1..2] of int: X_INTRODUCED_15_ = [1,-1];
145var -1..4: x:: output_var;
146var int: y:: output_var;
147var 0..4: X_INTRODUCED_1_ ::var_is_introduced :: is_defined_var;
148var 1..5: X_INTRODUCED_3_ ::var_is_introduced ;
149var bool: X_INTRODUCED_4_ ::var_is_introduced :: is_defined_var;
150var int: X_INTRODUCED_6_ ::var_is_introduced ;
151var bool: X_INTRODUCED_16_ ::var_is_introduced :: is_defined_var;
152array [1..1] of var int: X_INTRODUCED_2_ ::var_is_introduced = [x];
153constraint int_abs(x,X_INTRODUCED_1_):: ctx_pos:: defines_var(X_INTRODUCED_1_);
154constraint gecode_on_restart_status(X_INTRODUCED_3_);
155constraint gecode_on_restart_last_val_int(y,X_INTRODUCED_6_);
156constraint bool_clause([X_INTRODUCED_4_,X_INTRODUCED_16_],[]);
157constraint int_eq_imp(y,0,X_INTRODUCED_4_);
158constraint int_eq_reif(X_INTRODUCED_3_,1,X_INTRODUCED_4_):: defines_var(X_INTRODUCED_4_);
159constraint int_lin_eq_imp(X_INTRODUCED_15_,[y,X_INTRODUCED_6_],1,X_INTRODUCED_16_):: defines_var(X_INTRODUCED_16_);
160solve :: int_search(X_INTRODUCED_2_,input_order,indomain_min,complete) maximize X_INTRODUCED_1_;
161)FZN", R"OUT(x = -1;
162y = 0;
163----------
164x = 2;
165y = 1;
166----------
167x = 3;
168y = 2;
169----------
170x = 4;
171y = 3;
172----------
173==========
174)OUT", true, {"--restart", "constant", "--restart-base", "100"});
175
176 (void) new FlatZincTest("on_restart::uniform_int_upper_bound",
177R"FZN(
178predicate gecode_on_restart_uniform_int(int: low,int: high,var int: out);
179var 0..1: y:: output_var;
180constraint gecode_on_restart_uniform_int(0,1,y);
181constraint int_eq(y,1);
182solve satisfy;
183)FZN", R"OUT(y = 1;
184----------
185)OUT", true, {"--restart", "constant", "--restart-base", "100", "--seed", "2"});
186 }
187 };
188
189 Create c;
190 UniformIntInvalidRange invalid_range;
191 WideUniformInt w;
192 }
193
194}}
195
196// STATISTICS: test-flatzinc
197
unsigned long long int uniform_int_offset(Random &random, unsigned long long int width)
Sample an offset for an inclusive integer restart range.
GECODE_FLATZINC_EXPORT FlatZincSpace * parse(const std::string &fileName, Printer &p, std::ostream &err=std::cerr, FlatZincSpace *fzs=nullptr, Rnd &rnd=defrnd)
Parse FlatZinc file fileName into fzs and return it.
Tests for FlatZinc.
Definition flatzinc.cpp:38
General test support.
Definition afc.cpp:39