clingo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
weight_constraint.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2011, Benjamin Kaufmann
3 //
4 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/
5 //
6 // Clasp is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Clasp is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Clasp; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 #ifndef CLASP_SMODELS_CONSTRAINTS_H_INCLUDED
21 #define CLASP_SMODELS_CONSTRAINTS_H_INCLUDED
22 
23 #ifdef _MSC_VER
24 #pragma warning (disable : 4200) // nonstandard extension used : zero-sized array
25 #pragma once
26 #endif
27 
28 #include <clasp/constraint.h>
29 
30 namespace Clasp {
31 
33 struct WeightLitsRep {
35 
43 
48  bool propagate(Solver& s, Literal W);
49  bool sat() const { return bound <= 0; }
50  bool unsat() const { return reach < bound; }
51  bool open() const { return bound > 0 && bound <= reach;}
52  bool hasWeights() const { return size && lits[0].second > 1; }
54  uint32 size;
57 };
58 
60 
78 class WeightConstraint : public Constraint {
79 public:
83  create_sat = 4u,
89  };
90  class CPair {
91  public:
92  CPair() { con[0] = con[1] = 0; }
93  bool ok() const { return con[0] != (WeightConstraint*)0x1 && con[1] != (WeightConstraint*)0x1; }
94  WeightConstraint* first() const { return con[0]; }
95  WeightConstraint* second()const { return con[1]; }
96  private:
97  friend class WeightConstraint;
98  WeightConstraint* con[2];
99  };
101 
114  static CPair create(Solver& s, Literal W, WeightLitVec& lits, weight_t bound, uint32 creationFlags = 0);
115  static CPair create(Solver& s, Literal W, WeightLitsRep rep , uint32 creationFlags);
116  // constraint interface
118  bool simplify(Solver& s, bool = false);
119  void destroy(Solver*, bool);
120  PropResult propagate(Solver& s, Literal p, uint32& data);
121  void reason(Solver&, Literal p, LitVec& lits);
122  bool minimize(Solver& s, Literal p, CCMinRecursive* r);
123  void undoLevel(Solver& s);
124  uint32 estimateComplexity(const Solver& s) const;
132  FFB_BTB = 0,
133  FTB_BFB = 1,
134  };
140  Literal lit(uint32 i, ActiveConstraint c) const { return Literal::fromIndex( lits_->lit(i).index() ^ c ); }
142  weight_t weight(uint32 i) const { return lits_->weight(i); }
144  uint32 size() const { return lits_->size(); }
146  bool isWeight() const { return lits_->weights(); }
147  // Returns the index of next literal to look at during backward propagation.
148  uint32 getBpIndex() const { return !isWeight() ? 1 : undo_[0].data>>1; }
149 private:
150  static WeightConstraint* createImpl(Solver& s, Literal W, WeightLitsRep& rep, uint32 flags);
151  bool integrateRoot(Solver& s);
152  struct WL {
153  WL(uint32 s, bool shared, bool w);
154  bool shareable() const { return rc != 0; }
155  bool unique() const { return rc == 0 || refCount() == 1; }
156  bool weights() const { return w != 0; }
157  uint32 size() const { return sz; }
158  Literal lit(uint32 i) const { return lits[(i<<w)]; }
159  Var var(uint32 i) const { return lits[(i<<w)].var(); }
160  weight_t weight(uint32 i) const { return !weights() ? weight_t(1) : (weight_t)lits[(i<<1)+1].asUint(); }
161  uint32 refCount() const;
162  WL* clone();
163  void release();
164  uint8* address();
165  uint32 sz : 30; // number of literals
166  uint32 rc : 1; // ref counted?
167  uint32 w : 1; // has weights?
168  Literal lits[0]; // Literals of constraint: ~B [Bw], l1 [w1], ..., ln-1 [Wn-1]
169  };
170  WeightConstraint(Solver& s, SharedContext* ctx, Literal W, const WeightLitsRep& , WL* out, uint32 act = 3u);
171  WeightConstraint(Solver& s, const WeightConstraint& other);
172  ~WeightConstraint();
173 
174  static const uint32 NOT_ACTIVE = 3u;
175 
176  // Represents a literal on the undo stack.
177  // idx() returns the index of the literal.
178  // constraint() returns the constraint that added the literal to the undo stack.
179  // Note: Only 31-bits are used for undo info.
180  // The remaining bit is used as a flag for marking processed literals.
181  struct UndoInfo {
182  explicit UndoInfo(uint32 d = 0) : data(d) {}
183  uint32 idx() const { return data >> 2; }
184  ActiveConstraint constraint() const { return static_cast<ActiveConstraint>((data&2) != 0); }
185  uint32 data;
186  };
187  // Is literal idx contained as reason lit in the undo stack?
188  bool litSeen(uint32 idx) const { return (undo_[idx].data & 1) != 0; }
189  // Mark/unmark literal idx.
190  void toggleLitSeen(uint32 idx) { undo_[idx].data ^= 1; }
191  // Add watch for idx'th literal of c to the solver.
192  void addWatch(Solver& s, uint32 idx, ActiveConstraint c);
193  // Updates bound_[c] and adds an undo watch to the solver if necessary.
194  // Then adds the literal at position idx to the reason set (and the undo stack).
195  void updateConstraint(Solver& s, uint32 idx, ActiveConstraint c);
196  // Returns the starting index of the undo stack.
197  uint32 undoStart() const { return isWeight(); }
198  UndoInfo undoTop() const { assert(up_ != undoStart()); return undo_[up_-1]; }
199  // Returns the decision level of the last assigned literal
200  // or 0 if no literal was assigned yet.
201  uint32 highestUndoLevel(Solver&) const;
202  void setBpIndex(uint32 n);
203  WL* lits_; // literals of constraint
204  uint32 up_ : 27; // undo position; [undoStart(), up_) is the undo stack
205  uint32 ownsLit_: 1; // owns lits_?
206  uint32 active_ : 2; // which of the two sub-constraints is currently unit?
207  uint32 watched_: 2; // which constraint is watched (3 both, 2 ignore, FTB_BFB, FFB_BTB)
208  weight_t bound_[2]; // FFB_BTB: (sumW-bound)+1 / FTB_BFB: bound
209  UndoInfo undo_[0]; // undo stack + seen flag for each literal
210 };
211 }
212 
213 #endif
bool open() const
Definition: weight_constraint.h:51
WeightConstraint * first() const
Definition: weight_constraint.h:94
weight_t weight(uint32 i) const
Returns the weight of the i'th literal or 1 if constraint is a cardinality constraint.
Definition: weight_constraint.h:142
weight_t bound
Definition: weight_constraint.h:55
Definition: weight_constraint.h:88
Definition: solver_types.h:845
Definition: weight_constraint.h:90
void undoLevel(Solver &s)
Called when the solver undid a decision level watched by this constraint.
Definition: weight_constraint.cpp:497
std::pair< Literal, weight_t > WeightLiteral
Definition: literal.h:197
ActiveConstraint
Definition: weight_constraint.h:131
static WeightLitsRep create(Solver &s, WeightLitVec &lits, weight_t bound)
Transforms the given literals to the normal form expected by WeightConstraint.
Definition: weight_constraint.cpp:34
void reason(Solver &, Literal p, LitVec &lits)
Definition: weight_constraint.cpp:463
bool propagate(Solver &s, Literal W)
Propagates the constraint W == *this.
Definition: weight_constraint.cpp:109
Base class for (boolean) constraints to be used in a Solver.
Definition: constraint.h:85
Definition: weight_constraint.h:87
CPair()
Definition: weight_constraint.h:92
bool unsat() const
Definition: weight_constraint.h:50
tuple s
Definition: server.py:45
uint32 Var
A variable is currently nothing more but an integer in the range [0..varMax).
Definition: literal.h:42
WeightLiteral * lits
Definition: weight_constraint.h:53
Type used as return type for Constraint::propagate.
Definition: constraint.h:88
Definition: weight_constraint.h:132
Definition: weight_constraint.h:82
PodVector< Literal >::type LitVec
Definition: literal.h:193
Definition: weight_constraint.h:84
static Literal fromIndex(uint32 idx)
Creates a literal from an index.
Definition: literal.h:105
static CPair create(Solver &s, Literal W, WeightLitVec &lits, weight_t bound, uint32 creationFlags=0)
Creates a new weight constraint from the given weight literals.
Definition: weight_constraint.cpp:162
Definition: weight_constraint.h:81
uint32 size
Definition: weight_constraint.h:54
std::tuple< T...> clone(const std::tuple< T...> &x, seq< S...>)
Definition: utility.hh:376
uint32 estimateComplexity(const Solver &s) const
Returns an estimate of the constraint's complexity relative to a clause (complexity = 1)...
Definition: weight_constraint.cpp:574
bool hasWeights() const
Definition: weight_constraint.h:52
int32 weight_t
A signed integer type used to represent weights.
Definition: literal.h:66
Bound & bound
Definition: output.cc:122
Class implementing smodels-like cardinality- and weight constraints.
Definition: weight_constraint.h:78
weight_t reach
Definition: weight_constraint.h:56
bool isWeight() const
Returns false if constraint is a cardinality constraint.
Definition: weight_constraint.h:146
bool minimize(Solver &s, Literal p, CCMinRecursive *r)
Definition: weight_constraint.cpp:478
tuple p
Definition: server.py:49
WeightConstraint * second() const
Definition: weight_constraint.h:95
bool sat() const
Definition: weight_constraint.h:49
tuple c
Definition: visualize.py:132
bool simplify(Solver &s, bool=false)
Definition: weight_constraint.cpp:517
Definition: weight_constraint.h:86
ScriptLiteralShared & shared
Definition: literals.cc:128
uint32 getBpIndex() const
Definition: weight_constraint.h:148
bool ok() const
Definition: weight_constraint.h:93
tuple d
Definition: pyclingo.py:49
Definition: weight_constraint.h:83
Constraint * cloneAttach(Solver &)
Returns a clone of this and adds necessary watches to the given solver.
Definition: weight_constraint.cpp:310
unsigned flags
Definition: program_options.cpp:588
uint32 size() const
Returns the number of literals in this constraint (including W).
Definition: weight_constraint.h:144
Value var
Definition: output.cc:70
Literal lit(uint32 i, ActiveConstraint c) const
Definition: weight_constraint.h:140
void destroy(Solver *, bool)
Default is to call delete this.
Definition: weight_constraint.cpp:354
Primitive representation of weight constraint literals in normal form.
Definition: weight_constraint.h:33
A literal is a variable or its negation.
Definition: literal.h:80
CreationFlags
Definition: weight_constraint.h:80
PodVector< WeightLiteral >::type WeightLitVec
Definition: literal.h:198
Definition: weight_constraint.h:133
PropResult propagate(Solver &s, Literal p, uint32 &data)
Definition: weight_constraint.cpp:415
LparseOutputter & out
Definition: output.cc:685
clasp's Solver class.
Definition: solver.h:82
Definition: weight_constraint.h:85