A set of benchmarks to compare a new prototype MiniZinc implementation
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3/*
4 * Main authors:
5 * Guido Tack <guido.tack@monash.edu>
6 */
7
8/* This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11
12#ifndef __MINIZINC_OPTIMIZE_HH__
13#define __MINIZINC_OPTIMIZE_HH__
14
15#include <minizinc/flatten.hh>
16#include <minizinc/hash.hh>
17
18namespace MiniZinc {
19
20class VarOccurrences {
21public:
22 typedef std::unordered_set<Item*> Items;
23 IdMap<Items> _m;
24 IdMap<int> idx;
25
26 /// Add \a to the index
27 void add_idx(VarDeclI* i, int idx_i);
28 /// Add \a to the index
29 void add_idx(VarDecl* e, int idx_i);
30 /// Find index of \a vd
31 int find(VarDecl* vd);
32 /// Remove index of \a vd
33 void remove(VarDecl* vd);
34
35 /// Add \a i to the dependencies of \a v
36 void add(VarDecl* v, Item* i);
37
38 /// Remove \a i from map and return new number of occurrences
39 int remove(VarDecl* v, Item* i);
40
41 /// Remove all occurrences from map and return new number of occurrences
42 void removeAllOccurrences(VarDecl* v);
43
44 /// Return number of occurrences of \a v
45 int occurrences(VarDecl* v);
46
47 /// Return number of constraint usages of \a v
48 int usages(VarDecl* v);
49
50 /// Unify \a v0 and \a v1 (removing \a v0)
51 void unify(EnvI& env, Model* m, Id* id0, Id* id1);
52
53 /// Clear all entries
54 void clear(void);
55};
56
57class CollectOccurrencesE : public EVisitor {
58public:
59 VarOccurrences& vo;
60 Item* ci;
61 CollectOccurrencesE(VarOccurrences& vo0, Item* ci0) : vo(vo0), ci(ci0) {}
62 void vId(const Id& id) {
63 if (id.decl()) vo.add(id.decl(), ci);
64 }
65};
66
67class CollectOccurrencesI : public ItemVisitor {
68public:
69 VarOccurrences& vo;
70 CollectOccurrencesI(VarOccurrences& vo0) : vo(vo0) {}
71 void vVarDeclI(VarDeclI* v);
72 void vConstraintI(ConstraintI* ci);
73 void vSolveI(SolveI* si);
74};
75
76class CollectDecls : public EVisitor {
77public:
78 VarOccurrences& vo;
79 std::vector<VarDecl*>& vd;
80 Item* item;
81 CollectDecls(VarOccurrences& vo0, std::vector<VarDecl*>& vd0, Item* item0)
82 : vo(vo0), vd(vd0), item(item0) {}
83
84 static bool varIsFree(VarDecl* vd) {
85 if (vd->e() == NULL || vd->ti()->domain() == NULL || vd->ti()->computedDomain()) {
86 return true;
87 } else {
88 /// TODO: test if id's domain is a superset of the right hand side
89 /// this currently only tests for equality, and for Boolean domains
90 if (Id* ident = vd->e()->dyn_cast<Id>()) {
91 if (Expression::equal(ident->decl()->ti()->domain(), vd->ti()->domain())) {
92 return true;
93 }
94 } else if (vd->e() == vd->ti()->domain()) {
95 return true;
96 }
97 }
98 return false;
99 }
100
101 void vId(Id& id) {
102 if (id.decl() && vo.remove(id.decl(), item) == 0) {
103 if (varIsFree(id.decl())) {
104 vd.push_back(id.decl());
105 }
106 }
107 }
108};
109
110bool isOutput(VarDecl* vd);
111
112/// Simplyfy models in \a env
113void optimize(Env& env, bool chain_compression = true);
114
115} // namespace MiniZinc
116
117#endif