[pypy-svn] r24221 - pypy/dist/pypy/lib/logic/gecode_wrapper
ludal at codespeak.net
ludal at codespeak.net
Fri Mar 10 13:44:19 CET 2006
Author: ludal
Date: Fri Mar 10 13:44:16 2006
New Revision: 24221
Added:
pypy/dist/pypy/lib/logic/gecode_wrapper/README
Modified:
pypy/dist/pypy/lib/logic/gecode_wrapper/Makefile
pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.cc
pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.h
pypy/dist/pypy/lib/logic/gecode_wrapper/main.c
pypy/dist/pypy/lib/logic/gecode_wrapper/reine.py
pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.cc
pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.hh
Log:
(ludal, ale, Gregoire Dooms)
first draft implementation of a Python propagator called back from gecode engine
Modified: pypy/dist/pypy/lib/logic/gecode_wrapper/Makefile
==============================================================================
--- pypy/dist/pypy/lib/logic/gecode_wrapper/Makefile (original)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/Makefile Fri Mar 10 13:44:16 2006
@@ -1,6 +1,7 @@
-CXXFLAGS=$(shell pkg-config --cflags gecode gecode-minimodel gecode-search)
+CXXFLAGS=$(shell pkg-config --cflags gecode gecode-minimodel gecode-search) -g
+CFLAGS=-g
LDFLAGS=$(shell pkg-config --libs gecode gecode-minimodel gecode-search)
@@ -9,6 +10,6 @@
libgecode_wrap.so: gecode_wrap.o space_wrap.o gecode_wrap.h space_wrap.hh
- g++ -o $@ $(LDFLAGS) -shared $<
+ g++ -o $@ $(LDFLAGS) -shared gecode_wrap.o space_wrap.o
Added: pypy/dist/pypy/lib/logic/gecode_wrapper/README
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/README Fri Mar 10 13:44:16 2006
@@ -0,0 +1,8 @@
+make
+python reine.py 8
+
+or
+LD_LIBRARY_PATH=. ./reine 10
+
+(need to get ctypes, and gecode library)
+apt-get install libgecode0-dev
Modified: pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.cc
==============================================================================
--- pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.cc (original)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.cc Fri Mar 10 13:44:16 2006
@@ -45,6 +45,7 @@
return _eng->next();
}
+
void space_values( void* spc, int n, int* vars, int* values )
{
MySpace* _spc = static_cast<MySpace*>(spc);
@@ -56,3 +57,60 @@
MySpace* _spc = static_cast<MySpace*>(spc);
delete _spc;
}
+
+
+
+void* new_propagator( void* spc, PropagatorCallback cb )
+{
+ MySpace* _spc = static_cast<MySpace*>(spc);
+ return (void*)new (_spc) MyPropagator(_spc, cb);
+}
+
+int propagator_create_int_view( void* prp, int var )
+{
+ MyPropagator* _prp = static_cast<MyPropagator*>(prp);
+ return _prp->add_int_view( var );
+}
+
+int int_view_lq( void* prp, int view, int value )
+{
+ MyPropagator* _prp = static_cast<MyPropagator*>(prp);
+ IntView& _view = _prp->get_int_view( view );
+ return _view.lq( _prp->home, value );
+}
+
+int int_view_gq( void* prp, int view, int value )
+{
+ MyPropagator* _prp = static_cast<MyPropagator*>(prp);
+ IntView& _view = _prp->get_int_view( view );
+ return _view.gq( _prp->home, value );
+}
+
+int int_view_min( void* prp, int view )
+{
+ MyPropagator* _prp = static_cast<MyPropagator*>(prp);
+ IntView& _view = _prp->get_int_view( view );
+ return _view.min();
+}
+
+int int_view_max( void* prp, int view )
+{
+ MyPropagator* _prp = static_cast<MyPropagator*>(prp);
+ IntView& _view = _prp->get_int_view( view );
+ return _view.max( );
+}
+
+int int_view_val( void* prp, int view )
+{
+ MyPropagator* _prp = static_cast<MyPropagator*>(prp);
+ IntView& _view = _prp->get_int_view( view );
+ return _view.val( );
+}
+
+int int_view_assigned( void* prp, int view )
+{
+ MyPropagator* _prp = static_cast<MyPropagator*>(prp);
+ IntView& _view = _prp->get_int_view( view );
+ return _view.assigned( );
+}
+
Modified: pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.h
==============================================================================
--- pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.h (original)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.h Fri Mar 10 13:44:16 2006
@@ -25,6 +25,20 @@
void space_release( void* spc );
+ /* propagators */
+
+ typedef int (*PropagatorCallback)(void*);
+ void* new_propagator( void* spc, PropagatorCallback cb );
+
+ int propagator_create_int_view( void* prp, int var );
+
+ int int_view_lq( void* prp, int view, int value );
+ int int_view_gq( void* prp, int view, int value );
+ int int_view_min( void* prp, int view );
+ int int_view_max( void* prp, int view );
+ int int_view_val( void* prp, int view );
+ int int_view_assigned( void* prp, int view );
+
#ifdef __cplusplus
};
#endif
Modified: pypy/dist/pypy/lib/logic/gecode_wrapper/main.c
==============================================================================
--- pypy/dist/pypy/lib/logic/gecode_wrapper/main.c (original)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/main.c Fri Mar 10 13:44:16 2006
@@ -15,6 +15,9 @@
};
+
+
+
int main( int argc, char** argv )
{
int coefs[2] = { 1, -1};
Modified: pypy/dist/pypy/lib/logic/gecode_wrapper/reine.py
==============================================================================
--- pypy/dist/pypy/lib/logic/gecode_wrapper/reine.py (original)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/reine.py Fri Mar 10 13:44:16 2006
@@ -5,9 +5,27 @@
gecode = cdll.LoadLibrary("./libgecode_wrap.so")
IRT_NQ = 1
+ES_FAILED = -1 # < Execution has resulted in failure
+ES_NOFIX = 0 # < Propagation has not computed fixpoint
+ES_OK = 0 # < Execution is okay
+ES_FIX = 1 # < Propagation has computed fixpoint
+ES_SUBSUMED = 2 # < %Propagator is subsumed (entailed)
+
import sys
+PROPCB = CFUNCTYPE(c_int, c_void_p)
+
+def null_propagator( prop ):
+ x = gecode.int_view_assigned( prop, 0 )
+ y = gecode.int_view_assigned( prop, 1 )
+ print "Assigned", x, y
+ return ES_OK
+
+
+nullpropcb = PROPCB(null_propagator)
+
+
N = int(sys.argv[1])
spc = gecode.new_space()
@@ -29,6 +47,13 @@
gecode.space_linear( spc, 2, coefs, qpair, IRT_NQ, i-j )
gecode.space_linear( spc, 2, coefs, qpair, IRT_NQ, j-i )
+
+myprop = gecode.new_propagator( spc, nullpropcb )
+
+gecode.propagator_create_int_view( myprop, 0 )
+gecode.propagator_create_int_view( myprop, N-1 )
+
+
gecode.space_branch( spc )
engine = gecode.new_dfs( spc, 5, 2 )
@@ -40,7 +65,7 @@
sol = gecode.search_next( engine )
if not sol:
break
- if nsol%10 == 0:
+ if nsol%1 == 0:
print "Sol", nsol
gecode.space_values( sol, N, qvars, result )
for i in result:
Modified: pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.cc
==============================================================================
--- pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.cc (original)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.cc Fri Mar 10 13:44:16 2006
@@ -39,10 +39,54 @@
return new MyDFSEngine( spc, c_d, c_a );
}
-MySpace* search_next( void* _search )
+
+
+MyPropagator::MyPropagator(MySpace* _home, PropagatorCallback cb )
+ : Propagator(_home), _cb(cb), home(_home)
{
- MySearchEngine* search = static_cast<MySearchEngine*>(_search);
- return search->next();
}
+ExecStatus
+MyPropagator::post(Space* home) {
+ /* post domain reduction done from python */
+ return ES_OK;
+}
+
+
+MyPropagator::MyPropagator(Space* _home, bool share, MyPropagator& p)
+ : Propagator(_home,share,p), home(static_cast<MySpace*>(_home)), _cb(p._cb)
+{
+ IntViewVectIterator it;
+ for(it=p._int_views.begin();it!=p._int_views.end();++it) {
+ _int_views.push_back( IntView() );
+ _int_views.back().update(_home, share, *it );
+ }
+}
+
+Actor*
+MyPropagator::copy(Space* home, bool share) {
+ return new (home) MyPropagator(home,share,*this);
+}
+
+ExecStatus
+MyPropagator::propagate(Space* home) {
+ ExecStatus status;
+ status = (ExecStatus)_cb( this );
+ return status;
+}
+
+int
+MyPropagator::add_int_view( int var )
+{
+ IntVar& intvar = home->get_int_var( var );
+ _int_views.push_back( IntView( intvar ) );
+ _int_views.back().subscribe( home, this, PC_INT_BND );
+ return _int_views.size()-1;
+}
+
+PropCost
+MyPropagator::cost(void) const
+{
+ return PC_MAX;
+}
Modified: pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.hh
==============================================================================
--- pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.hh (original)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.hh Fri Mar 10 13:44:16 2006
@@ -11,10 +11,12 @@
#include "kernel.hh"
#include "int.hh"
#include "search.hh"
+#include "gecode_wrap.h"
/*
*/
using namespace Gecode;
+using namespace Gecode::Int;
using namespace std;
class Unimplemented : public exception {};
@@ -96,6 +98,8 @@
cout << endl;
}
+ IntVar& get_int_var( int n ) { return _int_vars[n]; }
+
protected:
vector< IntVar > _int_vars;
@@ -115,4 +119,29 @@
DFS<MySpace> dfs;
};
+
+typedef vector<IntView> IntViewVect;
+typedef IntViewVect::const_iterator IntViewVectConstIterator;
+typedef IntViewVect::iterator IntViewVectIterator;
+
+class MyPropagator : public Propagator {
+public:
+ MyPropagator(MySpace* home, PropagatorCallback cb );
+ MyPropagator(Space* home, bool share, MyPropagator& p);
+ virtual ExecStatus propagate (Space *);
+ virtual PropCost cost (void) const;
+ Actor* copy(Space* home, bool share);
+ ExecStatus post(Space* home);
+
+ int add_int_view( int var );
+ IntView& get_int_view( int view ) { return _int_views[view]; }
+
+ MySpace* home;
+protected:
+ IntViewVect _int_views;
+ PropagatorCallback _cb;
+};
+
+
+
#endif
More information about the Pypy-commit
mailing list