/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Jip J. Dekker */ /* 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/. */ #include #include #include #include namespace MiniZinc { void ChainCompressor::removeItem(Item* i) { CollectDecls cd(env.vo, deletedVarDecls, i); if (auto ci = i->dyn_cast()) { topDown(cd, ci->e()); } else if (auto vdi = i->dyn_cast()) { topDown(cd, vdi->e()); } else { assert(false); // CURRENTLY NOT SUPPORTED } env.flat_removeItem(i); } int ChainCompressor::addItem(Item* i) { env.flat_addItem(i); int item_idx = env.flat()->size() - 1; trackItem(i); return item_idx; } void ChainCompressor::updateCount() { for (auto it = this->items.begin(); it != items.end();) { if (it->second->removed()) { it = this->items.erase(it); } else { ++it; } } } void ChainCompressor::replaceCallArgument(Item* i, Call* c, unsigned int n, Expression* e) { CollectDecls cd(env.vo, deletedVarDecls, i); topDown(cd, c->arg(n)); c->arg(n, e); CollectOccurrencesE ce(env.vo, i); topDown(ce, e); } bool ImpCompressor::trackItem(Item* i) { if (i->removed()) { return false; } if (auto ci = i->dyn_cast()) { if (auto c = ci->e()->dyn_cast()) { // clause([y], [x]); i.e. x -> y if (c->id() == constants().ids.clause) { auto positive = c->arg(0)->cast(); auto negative = c->arg(1)->cast(); if (positive->length() == 1 && negative->length() == 1) { auto var = (*negative)[0]->cast(); storeItem(var->decl(), i); return true; } } else if (c->id() == "mzn_reverse_map_var") { auto control = c->arg(0)->cast(); assert(control->type().isvarbool()); storeItem(control->decl(), i); return true; // pred_imp(..., b); i.e. b -> pred(...) } else if (c->id().endsWith("_imp")) { auto control = c->arg(c->n_args() - 1)->cast(); assert(control->type().isvarbool()); storeItem(control->decl(), i); return true; } } } else if (auto vdi = i->dyn_cast()) { if (vdi->e()->type().isvarbool() && vdi->e() && vdi->e()->e()) { if (auto c = vdi->e()->e()->dyn_cast()) { // x = forall([y,z,...]); potentially: x -> (y /\ z /\ ...) if (c->id() == constants().ids.forall) { storeItem(vdi->e(), i); return true; // x ::ctx_pos = pred(...); potentially: pred_imp(..., x); i.e. x -> pred(...) } else if (env.fopts.enable_imp && vdi->e()->ann().contains(constants().ctx.pos)) { GCLock lock; auto cid = env.halfReifyId(c->id()); std::vector args; args.reserve(c->n_args() + 1); for (int j = 0; j < c->n_args(); ++j) { args.push_back(c->arg(j)->type()); } args.push_back(Type::varbool()); FunctionI* decl = env.model->matchFn(env, cid, args, false); if (decl) { storeItem(vdi->e(), i); return true; } } } } } return false; } void ImpCompressor::compress() { for (auto it = items.begin(); it != items.end();) { VarDecl* lhs = nullptr; VarDecl* rhs = nullptr; // Check if compression is possible if (auto ci = it->second->dyn_cast()) { auto c = ci->e()->cast(); if (c->id() == constants().ids.clause) { auto positive = c->arg(0)->cast(); auto var = (*positive)[0]->cast(); int occurrences = env.vo.usages(var->decl()); unsigned long lhs_occurences = count(var->decl()); bool output_var = var->decl()->ann().contains(constants().ann.output_var); // Compress if: // - There is one occurrence on the RHS of a clause and the others are on the LHS of a // clause // - There is one occurrence on the RHS of a clause, that Id is a reified forall that has no // other occurrences // - There is one occurrence on the RHS of a clause, that Id is a reification in a positive // context, and all other occurrences are on the LHS of a clause bool compress = !output_var && lhs_occurences > 0; if (var->decl()->e() && var->decl()->e()->dyn_cast()) { auto call = var->decl()->e()->cast(); if (call->id() == constants().ids.forall) { compress = compress && (occurrences == 1 && lhs_occurences == 1); } else { compress = compress && (occurrences == lhs_occurences); } } else { compress = compress && (occurrences == lhs_occurences + 1); } if (compress) { rhs = var->decl(); auto negative = c->arg(1)->cast(); lhs = (*negative)[0]->cast()->decl(); if (lhs == rhs) { continue; } } // TODO: Detect equivalences for output variables. } } if (lhs && rhs) { assert(count(rhs) > 0); auto range = find(rhs); for (auto match = range.first; match != range.second;) { bool succes = compressItem(match->second, lhs); assert(succes); env.n_imp_del++; match = items.erase(match); } assert(!rhs->ann().contains(constants().ann.output_var)); removeItem(it->second); it = items.erase(it); } else { ++it; } } } bool ImpCompressor::compressItem(Item* i, VarDecl* newLHS) { GCLock lock; if (auto ci = i->dyn_cast()) { auto c = ci->e()->cast(); // Given (x -> y) /\ (y -> z), produce x -> z if (c->id() == constants().ids.clause) { auto positive = c->arg(0)->cast(); auto rhs = (*positive)[0]->cast(); if (rhs->decl() != newLHS) { ConstraintI* nci = constructClause(positive, newLHS->id()); boolConstraints.push_back(addItem(nci)); } removeItem(i); return true; // Given (x -> y) /\ (y -> pred(...)), produce x -> pred(...) } else if (c->id() == "mzn_reverse_map_var") { return true; } else if (c->id().endsWith("_imp")) { replaceCallArgument(i, c, c->n_args() - 1, newLHS->id()); trackItem(i); return true; } } else if (auto vdi = i->dyn_cast()) { auto c = vdi->e()->e()->dyn_cast(); // Given: (x -> y) /\ (y -> (a /\ b /\ ...)), produce (x -> a) /\ (x -> b) /\ ... if (c->id() == constants().ids.forall) { auto exprs = c->arg(0)->cast(); for (int j = 0; j < exprs->size(); ++j) { auto rhs = (*exprs)[j]->cast(); if (rhs->decl() != newLHS) { ConstraintI* nci = constructClause(rhs, newLHS->id()); boolConstraints.push_back(addItem(nci)); } } return true; // x ::ctx_pos = pred(...); potentially: pred_imp(..., x); i.e. x -> pred(...) } else if (vdi->e()->ann().contains(constants().ctx.pos)) { ConstraintI* nci = constructHalfReif(c, newLHS->id()); assert(nci); addItem(nci); return true; } } return false; } ConstraintI* ImpCompressor::constructClause(Expression* pos, Expression* neg) { assert(GC::locked()); std::vector args(2); if (pos->dyn_cast()) { args[0] = pos; } else { assert(neg->type().isbool()); std::vector eVec(1); eVec[0] = pos; args[0] = new ArrayLit(pos->loc().introduce(), eVec); args[0]->type(Type::varbool(1)); } if (neg->dyn_cast()) { args[1] = neg; } else { assert(neg->type().isbool()); std::vector eVec(1); eVec[0] = neg; args[1] = new ArrayLit(neg->loc().introduce(), eVec); args[1]->type(Type::varbool(1)); } // NEVER CREATE (a -> a) assert((*args[0]->dyn_cast())[0]->dyn_cast()->decl() != (*args[1]->dyn_cast())[0]->dyn_cast()->decl()); auto nc = new Call(MiniZinc::Location().introduce(), constants().ids.clause, args); nc->type(Type::varbool()); nc->decl(env.model->matchFn(env, nc, false)); assert(nc->decl()); return new ConstraintI(MiniZinc::Location().introduce(), nc); } ConstraintI* ImpCompressor::constructHalfReif(Call* call, Id* control) { assert(env.fopts.enable_imp); assert(GC::locked()); auto cid = env.halfReifyId(call->id()); std::vector args(call->n_args()); for (int i = 0; i < call->n_args(); ++i) { args[i] = call->arg(i); } args.push_back(control); FunctionI* decl = env.model->matchFn(env, cid, args, false); if (decl) { auto nc = new Call(call->loc().introduce(), cid, args); nc->decl(decl); nc->type(Type::varbool()); return new ConstraintI(call->loc().introduce(), nc); } return nullptr; } bool LECompressor::trackItem(Item* i) { if (i->removed()) { return false; } bool added = false; if (auto ci = i->dyn_cast()) { if (auto call = ci->e()->dyn_cast()) { // {int,float}_lin_le([c1,c2,...], [x, y,...], 0); if (call->id() == constants().ids.int_.lin_le || call->id() == constants().ids.float_.lin_le) { auto as = follow_id(call->arg(0))->cast(); auto bs = follow_id(call->arg(1))->cast(); assert(as->size() == bs->size()); for (int j = 0; j < as->size(); ++j) { if (as->type().isintarray()) { if (follow_id((*as)[j])->cast()->v() > IntVal(0)) { // Check if left hand side is a variable (could be constant) if (auto decl = follow_id_to_decl((*bs)[j])->dyn_cast()) { storeItem(decl, i); added = true; } } } else { if (follow_id((*as)[j])->cast()->v() > FloatVal(0)) { // Check if left hand side is a variable (could be constant) if (auto decl = follow_id_to_decl((*bs)[j])->dyn_cast()) { storeItem(decl, i); added = true; } } } } } assert(call->id() != constants().ids.int2float); } } else if (auto vdi = i->dyn_cast()) { assert(vdi->e()); if (Expression* vde = vdi->e()->e()) { if (auto call = vde->dyn_cast()) { if (call->id() == constants().ids.int2float) { if (auto vd = follow_id_to_decl(call->arg(0))->dyn_cast()) { auto alias = follow_id_to_decl(vdi->e())->cast(); aliasMap[vd] = alias; } } } } } return added; } void LECompressor::compress() { for (auto it = items.begin(); it != items.end();) { VarDecl* lhs = nullptr; VarDecl* rhs = nullptr; VarDecl* alias = nullptr; // Check if compression is possible if (auto ci = it->second->dyn_cast()) { auto call = ci->e()->cast(); if (call->id() == constants().ids.int_.lin_le) { auto as = follow_id(call->arg(0))->cast(); auto bs = follow_id(call->arg(1))->cast(); auto c = follow_id(call->arg(2))->cast(); if (bs->size() == 2 && c->v() == IntVal(0)) { auto a0 = follow_id((*as)[0])->cast()->v(); auto a1 = follow_id((*as)[1])->cast()->v(); if (a0 == -a1 && eqBounds((*bs)[0], (*bs)[1])) { int i = a0 < a1 ? 0 : 1; if (!(*bs)[i]->isa()) { break; } auto neg = follow_id_to_decl((*bs)[i])->cast(); bool output_var = neg->ann().contains(constants().ann.output_var); int occurrences = env.vo.usages(neg); unsigned long lhs_occurences = count(neg); bool compress = !output_var; auto search = aliasMap.find(neg); if (search != aliasMap.end()) { alias = search->second; int alias_occ = env.vo.usages(alias); unsigned long alias_lhs_occ = count(alias); // neg is only allowed to occur: // - once in the "implication" // - once in the aliasing // - on a lhs of other expressions // alias is only allowed to occur on a lhs of an expression. compress = compress && (lhs_occurences + alias_lhs_occ > 0) && (occurrences == lhs_occurences + 2) && (alias_occ == alias_lhs_occ); } else { // neg is only allowed to occur: // - once in the "implication" // - on a lhs of other expressions compress = compress && (lhs_occurences > 0) && (occurrences == lhs_occurences + 1); } auto pos = follow_id_to_decl((*bs)[1 - i])->dyn_cast(); if (pos && compress) { rhs = neg; lhs = pos; assert(lhs != rhs); } // TODO: Detect equivalences for output variables. } } } } if (lhs && rhs) { assert(count(rhs) + count(alias) > 0); auto range = find(rhs); for (auto match = range.first; match != range.second;) { LEReplaceVar(match->second, rhs, lhs); match = items.erase(match); } if (alias) { VarDecl* i2f_lhs; auto search = aliasMap.find(lhs); if (search != aliasMap.end()) { i2f_lhs = search->second; } else { // Create new int2float Call* i2f = new Call(lhs->loc().introduce(), constants().ids.int2float, {lhs->id()}); i2f->decl(env.model->matchFn(env, i2f, false)); assert(i2f->decl()); i2f->type(Type::varfloat()); auto domain = new SetLit(lhs->loc().introduce(), eval_floatset(env, lhs->ti()->domain())); auto i2f_ti = new TypeInst(lhs->loc().introduce(), Type::varfloat(), domain); i2f_lhs = new VarDecl(lhs->loc().introduce(), i2f_ti, env.genId(), i2f); i2f_lhs->type(Type::varfloat()); addItem(new VarDeclI(lhs->loc().introduce(), i2f_lhs)); } auto arange = find(alias); for (auto match = arange.first; match != arange.second;) { LEReplaceVar(match->second, alias, i2f_lhs); match = items.erase(match); } } assert(!rhs->ann().contains(constants().ann.output_var)); removeItem(it->second); env.n_lin_del++; it = items.erase(it); } else { ++it; } } } template void LECompressor::LEReplaceVar(Item* i, VarDecl* oldVar, VarDecl* newVar) { typedef typename LinearTraits::Val Val; GCLock lock; auto ci = i->cast(); auto call = ci->e()->cast(); assert(call->id() == constants().ids.int_.lin_le || call->id() == constants().ids.float_.lin_le); // Remove old occurrences CollectDecls cd(env.vo, deletedVarDecls, i); topDown(cd, ci->e()); ArrayLit* al_c = eval_array_lit(env, call->arg(0)); std::vector coeffs(al_c->size()); for (int j = 0; j < al_c->size(); j++) { coeffs[j] = LinearTraits::eval(env, (*al_c)[j]); } ArrayLit* al_x = eval_array_lit(env, call->arg(1)); std::vector x(al_x->size()); for (int j = 0; j < al_x->size(); j++) { Expression* decl = follow_id_to_decl((*al_x)[j]); if (decl && decl->cast() == oldVar) { x[j] = newVar->id(); } else { x[j] = (*al_x)[j]; } } Val d = LinearTraits::eval(env, call->arg(2)); simplify_lin(coeffs, x, d); if (coeffs.empty()) { env.flat_removeItem(i); env.n_lin_del++; return; } else { std::vector coeffs_e(coeffs.size()); std::vector x_e(coeffs.size()); for (unsigned int j = 0; j < coeffs.size(); j++) { coeffs_e[j] = Lit::a(coeffs[j]); x_e[j] = x[j](); Expression* decl = follow_id_to_decl(x_e[j]); if (decl && decl->cast() == newVar) { storeItem(newVar, i); } } if (auto arg0 = call->arg(0)->dyn_cast()) { arg0->setVec(coeffs_e); } else { auto al_c_new = new ArrayLit(al_c->loc().introduce(), coeffs_e); al_c_new->type(al_c->type()); call->arg(0, al_c_new); } if (auto arg1 = call->arg(1)->dyn_cast()) { arg1->setVec(x_e); } else { auto al_x_new = new ArrayLit(al_x->loc().introduce(), x_e); al_x_new->type(al_x->type()); call->arg(1, al_x_new); } call->arg(2, Lit::a(d)); } // Add new occurences CollectOccurrencesE ce(env.vo, i); topDown(ce, ci->e()); } bool LECompressor::eqBounds(Expression* a, Expression* b) { // TODO: (To optimise) Check lb(lhs) >= lb(rhs) and enforce ub(lhs) <= ub(rhs) IntSetVal* dom_a = nullptr; IntSetVal* dom_b = nullptr; if (auto a_decl = follow_id_to_decl(a)->dyn_cast()) { if (a_decl->ti()->domain()) { dom_a = eval_intset(env, a_decl->ti()->domain()); } } else { assert(a->dyn_cast()); auto a_val = a->cast(); dom_a = IntSetVal::a(a_val->v(), a_val->v()); } if (auto b_decl = follow_id_to_decl(b)->dyn_cast()) { if (b_decl->ti()->domain()) { dom_b = eval_intset(env, b_decl->ti()->domain()); } } else { assert(b->dyn_cast()); auto b_val = b->cast(); dom_b = IntSetVal::a(b_val->v(), b_val->v()); } return (dom_a && dom_b && (dom_a->min() == dom_b->min()) && (dom_a->max() == dom_b->max())) || (!dom_a && !dom_b); } } // namespace MiniZinc