1
0
This repository has been archived on 2025-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
Jip J. Dekker 2c8ad0004a Squashed 'software/minizinc/' changes from 4f10c82056..93be33a6c2
93be33a6c2 Add complete predicate
REVERT: 4f10c82056 Merge branch 'MiniZinc:master' into feature/on_restart
REVERT: 0848ce7ec7 Add changelog for 2.5.5
REVERT: 44e2f770d5 Add test for insertion of ArrayLits into CSE
REVERT: 8a68d3dea8 Don't insert par expressions into CSE map unless they're an ArrayLit
REVERT: 6bf6f1180f Increase version number of development build
REVERT: dcaac92a74 Make min/max on array of opt vars return non-optional var. This is consistent with other functions on optional arrays like sum, product, exists, forall.
REVERT: 32aa288884 Update changelog
REVERT: a4edf0669f Fix flattening of all-par set literals
REVERT: 8c1c9605f6 Fix chain compressor, needs to ignore par constants
REVERT: 0cad1c6306 Use file_path on include paths to ensure separator consistency
REVERT: 05ad7d1931 Update changelog
REVERT: 22f5e2557b Define HAVE_CONFIG_H only for UNIX
REVERT: 81c7778d55 Define HAVE_CONFIG_H for CBC
REVERT: 1f56608e10 mzn-test: don't check symmetry breaking constraints
REVERT: 1a9767457e mzn-test: fix parsing %%%mzn-stat: ... output
REVERT: a41533fd54 MIP: report CPU time in %%%mzn-stat: solveTime=
REVERT: 9d490acd52 Updated docs on OR-Tools v8 installation
REVERT: c513f6599f Add changelog for 2.5.4
REVERT: b2eef2772b Follow ids to declarations when flattening par arrays. Fixes #448.
REVERT: c5c846d426 Check if result of flattening rhs of a vardecl is par.
REVERT: c496052767 Escape strings when pretty printing include items
REVERT: 9e379c995e Canonicalise file names before adding include items into "already seen" list
REVERT: d5d5d0d88c Use generic flattening inside generators. Fixes #451.
REVERT: dc8630a6e9 Small fix to multi-pass library change: use original include path if it is absolute
REVERT: 79c6092bd8 Strip library paths from includes in multi-pass compilation. Fixes #455.
REVERT: 897875d6d7 Compile infinite domains with holes into constraints. Fixes #457.
REVERT: b4e700dc67 Don't create copies of global declarations when creating par versions of functions
REVERT: 0e8cc42bb1 Fix typechecker to coerce bool to int in the objective.
REVERT: e05523b344 Add test for dzn output of arrays
REVERT: 1e0269000e Don't evaluate output_only arrays when generating dzn output.
REVERT: 57018c31d6 Fix matrix transposition in lex2 globals
REVERT: 2617c0c829 Fix output variables in lex_chain tests
REVERT: ef1a250c98 another efort to fix tests
REVERT: c00e199dfd Fix test globals_lex_chain.mzn
REVERT: b5c997d045 Fix code analysis
REVERT: 3352cf0bd5 SCIP constraint handler for lex_chain_..._orbitope
REVERT: 4e71a2cc97 Globals lex_chain_..., including lex_chain_..._orbitope
REVERT: d807428baf Move test specifcation into the correct folder
REVERT: 5be74bc74d MIP decompositions for lex_less_(bool, int, float)
REVERT: 36a554ba40 Don't modify infinte domain of optional variables. Fixes #456.
REVERT: f9e5306d75 Run clang-format
REVERT: 4b57667608 Fix comment reference to relevant test case
REVERT: 648f2ab36d Fix equality of indirection annotations
REVERT: ef7be5fd78 MIP decompositions for lex_lesseq_(bool=int, float)
REVERT: 6511b14e73 Propagate cv flag correctly.
REVERT: 6f27ecf1c0 Never insert par expressions into the CSE map.
REVERT: 7414f3ca0f Fix cplex id in example configuration
REVERT: 7ad7cec506 Update strictly_decreasing with documentation and opt version
REVERT: 8029f6e957 Support undefined enums in type checker.
REVERT: 79e0f0f546 Revert using mzn_in_symmetry_breaking_constraint() for SCIP's orbisack
REVERT: e88efda76c Fix format
REVERT: 4802031dc1 Added test mzn_in_symmetry_breaking_constraint()
REVERT: e21cc2515a More format fixes 02 ...
REVERT: 5bbd67c130 More format fixes...
REVERT: d5f9b777ea Format fixes
REVERT: 43757a09a0 Remove MIP-specific fzn_less(eq)_bool(_reif).mzn
REVERT: c93b5736a3 SCIP: orbisack constraint handler 'fzn_lex_lesseq__orbisack'
REVERT: 4516bb4e2c mzn-test.py: add OR-Tools as checker
REVERT: e2176f017d Add fix and test for crash with empty enum.
REVERT: ac7db35951 Fix documentation bugs.
REVERT: 47ba245832 Fix the incorrect renaming of key in model output interface
REVERT: 925796ed20 Fail on empty var domains in agenda
REVERT: 1ec19d7025 Fix error messages in CMake FindGurobi, FindCPlex
REVERT: 6d169475c1 mzn-test.py: Use -i for intermediate solutions
REVERT: df2f3e423a Allow coercion of JSON lists to enum definitions
REVERT: 2b0b8165e5 Fix clang-tidy errors
REVERT: 6597bc1920 Change the CI build image location
REVERT: 360c988452 Remove illegal duplicate keys in .gitlab-ci.yml
REVERT: 6a5d69c64b Add missing par opt versions of coercion functions
REVERT: 63014e3d8f Don't propagate annotations into annotation calls. Avoids infinite recursion.
REVERT: 54b19428ab Don't use GRB_INT_PAR_NONCONVEX if it's undefined
REVERT: a5bb56c47d Added piecewise_linear for non-continuous intervals

git-subtree-dir: software/minizinc
git-subtree-split: 93be33a6c254e54be7cd38abb7ebd6a6022f0c46
2021-07-12 20:08:41 +10:00

299 lines
9.8 KiB
C++

/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <guido.tack@monash.edu>
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma once
#include <minizinc/astexception.hh>
#include <minizinc/iter.hh>
#include <minizinc/model.hh>
#include <minizinc/prettyprinter.hh>
namespace MiniZinc {
/// Evaluate par int expression \a e
IntVal eval_int(EnvI& env, Expression* e);
/// Evaluate par bool expression \a e
bool eval_bool(EnvI& env, Expression* e);
/// Evaluate par float expression \a e
FloatVal eval_float(EnvI& env, Expression* e);
/// Evaluate an array expression \a e into an array literal
ArrayLit* eval_array_lit(EnvI& env, Expression* e);
/// Evaluate an access to array \a with indices \a idx and return whether
/// access succeeded in \a success
Expression* eval_arrayaccess(EnvI& env, ArrayLit* a, const std::vector<IntVal>& idx, bool& success);
/// Evaluate an array access \a e and return whether access succeeded in \a success
Expression* eval_arrayaccess(EnvI& env, ArrayAccess* e, bool& success);
/// Evaluate a set expression \a e into a set literal
SetLit* eval_set_lit(EnvI& env, Expression* e);
/// Evaluate a par integer set \a e
IntSetVal* eval_intset(EnvI& env, Expression* e);
/// Evaluate a par bool set \a e
IntSetVal* eval_boolset(EnvI& env, Expression* e);
/// Evaluate a par float set \a e
FloatSetVal* eval_floatset(EnvI& env, Expression* e);
/// Evaluate a par string \a e
std::string eval_string(EnvI& env, Expression* e);
/// Evaluate a par expression \a e and return it wrapped in a literal
Expression* eval_par(EnvI& env, Expression* e);
/// Check if variable declaration \a vd satisfies the domain and index set constraints
void check_par_declaration(EnvI& env, VarDecl* vd);
/// Representation for bounds of an integer expression
struct IntBounds {
/// Lower bound
IntVal l;
/// Upper bound
IntVal u;
/// Whether the bounds are valid
bool valid;
/// Constructor
IntBounds(IntVal l0, IntVal u0, bool valid0) : l(l0), u(u0), valid(valid0) {}
};
/// Compute bounds of an integer expression
IntBounds compute_int_bounds(EnvI& env, Expression* e);
/// Representation for bounds of a float expression
struct FloatBounds {
/// Lower bound
FloatVal l;
/// Upper bound
FloatVal u;
/// Whether the bounds are valid
bool valid;
/// Constructor
FloatBounds(FloatVal l0, FloatVal u0, bool valid0) : l(l0), u(u0), valid(valid0) {}
};
/// Compute bounds of an integer expression
FloatBounds compute_float_bounds(EnvI& env, Expression* e);
/**
* \brief Compute bounds of a set of int expression
*
* Returns NULL if bounds cannot be determined
*/
IntSetVal* compute_intset_bounds(EnvI& env, Expression* e);
class EvalBase {
public:
/// Evaluate bool expression that may contain variables
static bool evalBoolCV(EnvI& env, Expression* e);
};
template <class Eval>
void eval_comp_array(EnvI& env, Eval& eval, Comprehension* e, int gen, int id, KeepAlive in,
std::vector<typename Eval::ArrayVal>& a);
template <class Eval>
void eval_comp_set(EnvI& env, Eval& eval, Comprehension* e, int gen, int id, KeepAlive in,
std::vector<typename Eval::ArrayVal>& a);
template <class Eval>
void eval_comp_set(EnvI& env, Eval& eval, Comprehension* e, int gen, int id, IntVal i, KeepAlive in,
std::vector<typename Eval::ArrayVal>& a) {
{
GCLock lock;
GC::mark();
e->decl(gen, id)->trail();
e->decl(gen, id)->e(IntLit::a(i));
}
CallStackItem csi(env, e->decl(gen, id)->id(), i);
if (id == e->numberOfDecls(gen) - 1) {
bool where = true;
if (e->where(gen) != nullptr && !e->where(gen)->type().isvar()) {
where = eval.evalBoolCV(env, e->where(gen));
}
if (where) {
if (gen == e->numberOfGenerators() - 1) {
a.push_back(eval.e(env, e->e()));
} else {
if (e->in(gen + 1) == nullptr) {
eval_comp_array<Eval>(env, eval, e, gen + 1, 0, 0, e->in(gen + 1), a);
} else {
KeepAlive nextin;
Expression* gen_in = e->in(gen + 1);
if (gen_in->type().isvar() || gen_in->type().cv()) {
gen_in = eval.flatten(env, e->in(gen + 1));
}
if (gen_in->type().dim() == 0) {
GCLock lock;
nextin = new SetLit(Location(), eval_intset(env, gen_in));
} else {
GCLock lock;
nextin = eval_array_lit(env, gen_in);
}
if (e->in(gen + 1)->type().dim() == 0) {
eval_comp_set<Eval>(env, eval, e, gen + 1, 0, nextin, a);
} else {
eval_comp_array<Eval>(env, eval, e, gen + 1, 0, nextin, a);
}
}
}
}
} else {
eval_comp_set<Eval>(env, eval, e, gen, id + 1, in, a);
}
GC::untrail();
e->decl(gen, id)->flat(nullptr);
}
template <class Eval>
void eval_comp_array(EnvI& env, Eval& eval, Comprehension* e, int gen, int id, IntVal i,
KeepAlive in, std::vector<typename Eval::ArrayVal>& a) {
GC::mark();
e->decl(gen, id)->trail();
CallStackItem csi(env, e->decl(gen, id)->id(), i);
if (in() == nullptr) {
// this is an assignment generator
Expression* asn = e->where(gen)->type().isPar() ? eval_par(env, e->where(gen))
: eval.flatten(env, e->where(gen));
e->decl(gen, id)->e(asn);
e->rehash();
} else {
auto* al = in()->cast<ArrayLit>();
e->decl(gen, id)->e((*al)[static_cast<int>(i.toInt())]);
e->rehash();
}
if (id == e->numberOfDecls(gen) - 1) {
bool where = true;
if (e->in(gen) != nullptr && e->where(gen) != nullptr && !e->where(gen)->type().isvar()) {
where = eval.evalBoolCV(env, e->where(gen));
}
if (where) {
if (gen == e->numberOfGenerators() - 1) {
a.push_back(eval.e(env, e->e()));
} else {
if (e->in(gen + 1) == nullptr) {
eval_comp_array<Eval>(env, eval, e, gen + 1, 0, 0, e->in(gen + 1), a);
} else {
KeepAlive nextin;
Expression* gen_in = e->in(gen + 1);
if (gen_in->type().isvar() || gen_in->type().cv()) {
gen_in = eval.flatten(env, e->in(gen + 1));
}
if (gen_in->type().dim() == 0) {
GCLock lock;
nextin = new SetLit(Location(), eval_intset(env, gen_in));
} else {
GCLock lock;
nextin = eval_array_lit(env, gen_in);
}
if (gen_in->type().dim() == 0) {
eval_comp_set<Eval>(env, eval, e, gen + 1, 0, nextin, a);
} else {
eval_comp_array<Eval>(env, eval, e, gen + 1, 0, nextin, a);
}
}
}
}
} else {
eval_comp_array<Eval>(env, eval, e, gen, id + 1, in, a);
}
GC::untrail();
e->decl(gen, id)->flat(nullptr);
}
/**
* \brief Evaluate comprehension expression
*
* Calls \a eval.e for every element of the comprehension \a e,
* where \a gen is the current generator, \a id is the current identifier
* in that generator, \a in is the expression of that generator, and
* \a a is the array in which to place the result.
*/
template <class Eval>
void eval_comp_set(EnvI& env, Eval& eval, Comprehension* e, int gen, int id, KeepAlive in,
std::vector<typename Eval::ArrayVal>& a) {
IntSetVal* isv = eval_intset(env, in());
if (isv->card().isPlusInfinity()) {
throw EvalError(env, in()->loc(), "comprehension iterates over an infinite set");
}
IntSetRanges rsi(isv);
Ranges::ToValues<IntSetRanges> rsv(rsi);
for (; rsv(); ++rsv) {
eval_comp_set<Eval>(env, eval, e, gen, id, rsv.val(), in, a);
}
}
/**
* \brief Evaluate comprehension expression
*
* Calls \a eval.e for every element of the comprehension \a e,
* where \a gen is the current generator, \a id is the current identifier
* in that generator, \a in is the expression of that generator, and
* \a a is the array in which to place the result.
*/
template <class Eval>
void eval_comp_array(EnvI& env, Eval& eval, Comprehension* e, int gen, int id, KeepAlive in,
std::vector<typename Eval::ArrayVal>& a) {
auto* al = in()->cast<ArrayLit>();
for (unsigned int i = 0; i < al->size(); i++) {
eval_comp_array<Eval>(env, eval, e, gen, id, i, in, a);
}
}
/**
* \brief Evaluate comprehension expression
*
* Calls \a eval.e for every element of the comprehension \a e and
* returns a vector with all the evaluated results.
*/
template <class Eval>
std::vector<typename Eval::ArrayVal> eval_comp(EnvI& env, Eval& eval, Comprehension* e) {
std::vector<typename Eval::ArrayVal> a;
if (e->in(0) == nullptr) {
eval_comp_array<Eval>(env, eval, e, 0, 0, 0, e->in(0), a);
} else {
KeepAlive in;
{
GCLock lock;
if (e->in(0)->type().dim() == 0) {
if (e->in(0)->type().isvar()) {
in = new SetLit(Location(), compute_intset_bounds(env, e->in(0)));
} else {
in = new SetLit(Location(), eval_intset(env, e->in(0)));
}
} else {
if (e->in(0)->type().isvar()) {
in = eval_array_lit(env, eval.flatten(env, e->in(0)));
} else {
in = eval_array_lit(env, e->in(0));
}
}
}
if (e->in(0)->type().dim() == 0) {
eval_comp_set<Eval>(env, eval, e, 0, 0, in, a);
} else {
eval_comp_array<Eval>(env, eval, e, 0, 0, in, a);
}
}
return a;
}
/**
* \brief Evaluate comprehension expression
*
* Calls \a Eval::e for every element of the comprehension \a e and
* returns a vector with all the evaluated results.
*/
template <class Eval>
std::vector<typename Eval::ArrayVal> eval_comp(EnvI& env, Comprehension* e) {
Eval eval;
return eval_comp(env, eval, e);
}
Expression* follow_id(Expression* e);
Expression* follow_id_to_decl(Expression* e);
Expression* follow_id_to_value(Expression* e);
} // namespace MiniZinc