[pypy-svn] r24192 - pypy/dist/pypy/lib/logic/gecode_wrapper

ludal at codespeak.net ludal at codespeak.net
Thu Mar 9 19:32:06 CET 2006


Author: ludal
Date: Thu Mar  9 19:32:05 2006
New Revision: 24192

Added:
   pypy/dist/pypy/lib/logic/gecode_wrapper/
   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:
a sample ctype wrapper for the gecode library


Added: pypy/dist/pypy/lib/logic/gecode_wrapper/Makefile
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/Makefile	Thu Mar  9 19:32:05 2006
@@ -0,0 +1,14 @@
+
+
+CXXFLAGS=$(shell pkg-config --cflags gecode gecode-minimodel gecode-search)
+
+LDFLAGS=$(shell pkg-config --libs gecode gecode-minimodel gecode-search)
+
+reine: main.o libgecode_wrap.so
+	gcc -o reine -L. -lgecode_wrap main.o
+
+
+libgecode_wrap.so: gecode_wrap.o space_wrap.o gecode_wrap.h space_wrap.hh
+	g++ -o $@ $(LDFLAGS) -shared $<
+
+

Added: pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.cc
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.cc	Thu Mar  9 19:32:05 2006
@@ -0,0 +1,58 @@
+
+#include "gecode_wrap.h"
+#include "space_wrap.hh"
+
+
+void* new_space()
+{
+	return (void*) new MySpace();
+}
+
+int new_int_var( void* spc, int is_temp, int _min, int _max )
+{
+	MySpace* _spc = static_cast<MySpace*>(spc);
+	return _spc->intVar( is_temp, _min, _max );
+}
+
+void space_alldiff( void* spc, int n, int* vars )
+{
+	MySpace* _spc = static_cast<MySpace*>(spc);
+	_spc->alldiff( vars, vars+n );
+}
+
+void space_linear( void* spc, int n, int* coefs, int* vars,
+	     int type, int val )
+{
+	MySpace* _spc = static_cast<MySpace*>(spc);
+	_spc->linear( coefs, coefs+n, vars, vars+n, (IntRelType)type, val );
+}
+
+void space_branch( void* spc )
+{
+	MySpace* _spc = static_cast<MySpace*>(spc);
+	_spc->branch();
+}
+
+void* new_dfs( void* spc, int d_c, int d_a )
+{
+	MySpace* _spc = static_cast<MySpace*>(spc);
+	return new MyDFSEngine( _spc, d_c, d_a );
+}
+
+void* search_next( void* search )
+{
+	MySearchEngine* _eng = static_cast<MySearchEngine*>(search);
+	return _eng->next();
+}
+
+void space_values( void* spc, int n, int* vars, int* values )
+{
+	MySpace* _spc = static_cast<MySpace*>(spc);
+	_spc->int_values( n, vars, values );
+}
+
+void space_release( void* spc )
+{
+	MySpace* _spc = static_cast<MySpace*>(spc);
+	delete _spc;
+}

Added: pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.h
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/gecode_wrap.h	Thu Mar  9 19:32:05 2006
@@ -0,0 +1,32 @@
+
+#ifndef GECODE_WRAP_H
+#define GECODE_WRAP_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+	void* new_space();
+	
+	int new_int_var( void* spc, int is_temp, int _min, int _max );
+
+	void space_alldiff( void* spc, int n, int* vars );
+
+	void space_linear( void* spc, int n, int* coefs, int* vars,
+			   int type, int val );
+
+	void space_branch( void* spc );
+	
+	void* new_dfs( void* spc, int d_c, int d_a );
+
+	void* search_next( void* search );
+
+	void space_values( void* spc, int n, int* vars /*in*/, int* values /*out*/ );
+
+	void space_release( void* spc );
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif

Added: pypy/dist/pypy/lib/logic/gecode_wrapper/main.c
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/main.c	Thu Mar  9 19:32:05 2006
@@ -0,0 +1,77 @@
+/* -*- c-style: stroustrup -*-
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "gecode_wrap.h"
+
+	enum IntRelType {
+		IRT_EQ, ///< Equality (\f$=\f$)
+		IRT_NQ, ///< Disequality (\f$\neq\f$)
+		IRT_LQ, ///< Less or equal (\f$\leq\f$)
+		IRT_LE, ///< Less (\f$<\f$)
+		IRT_GQ, ///< Greater or equal (\f$\geq\f$)
+		IRT_GR  ///< Greater (\f$>\f$)
+	};
+
+
+int main( int argc, char** argv )
+{
+    int coefs[2] = { 1, -1};
+    int q[2];
+    int n;
+    void *spc, *engine, *sol;
+    int* vars, *res;
+    int i,j;
+    int nsol = 0;
+
+    if (argc<2) {
+	printf( "reines N\n" );
+	exit(1);
+    }
+    n = atoi(argv[1]);
+ 
+    spc = new_space();
+    vars = (int*)malloc(n*sizeof(int));
+    res = (int*)malloc(n*sizeof(int));
+
+    for(i=0;i<n;++i) {
+	int v;
+	v = new_int_var(spc, 0, 0, n-1);
+	vars[i] = v ;
+    }
+
+    space_alldiff( spc, n, vars );
+
+    for(i=0;i<n;++i) {
+	for( j=i+1; j<n; ++j ) {
+	    /* */
+	    q[0] = i;
+	    q[1] = j;
+	    space_linear( spc, 2, coefs, q, IRT_NQ, i-j );
+	    space_linear( spc, 2, coefs, q, IRT_NQ, j-i );
+	}
+    }
+    space_branch(spc);
+
+    engine = new_dfs( spc, 5, 2 );
+
+    printf("Sols\n");
+    while (sol=search_next(engine)) {
+//	sol->print_vars();
+	nsol++;
+	if (nsol%100==0) {
+	    printf("sol:%d\n", nsol );
+	    space_values( sol, n, vars, res );
+	    for(j=0;j<n;++j) {
+		    printf("% 5d ", res[j]);
+	    }
+	    printf("\n");
+	}
+
+	space_release(sol);
+    }
+    printf("NB sols:%d\n", nsol );
+
+    free(vars);
+}

Added: pypy/dist/pypy/lib/logic/gecode_wrapper/reine.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/reine.py	Thu Mar  9 19:32:05 2006
@@ -0,0 +1,51 @@
+
+
+from ctypes import *
+
+gecode = cdll.LoadLibrary("./libgecode_wrap.so")
+
+IRT_NQ = 1
+
+import sys
+
+N = int(sys.argv[1])
+
+spc = gecode.new_space()
+
+Narray = c_int*N
+Pair = c_int*2
+
+queens = [ gecode.new_int_var( spc, 0, 0, N-1 ) for i in range(N) ]
+
+qvars = Narray(*queens)
+
+gecode.space_alldiff( spc, N, qvars )
+
+coefs = Pair( 1, -1 )
+
+for i in range(N):
+    for j in range(i+1,N):
+        qpair = Pair( i, j )
+        gecode.space_linear( spc, 2, coefs, qpair, IRT_NQ, i-j )
+        gecode.space_linear( spc, 2, coefs, qpair, IRT_NQ, j-i )
+
+gecode.space_branch( spc )
+
+engine = gecode.new_dfs( spc, 5, 2 )
+
+result = Narray( *([0]*N ) )
+
+nsol = 0
+while 1:
+    sol = gecode.search_next( engine )
+    if not sol:
+        break
+    if nsol%10 == 0:
+        print "Sol", nsol
+        gecode.space_values( sol, N, qvars, result )
+        for i in result:
+            print i,
+        print
+    gecode.space_release( sol )
+    nsol+=1
+

Added: pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.cc
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.cc	Thu Mar  9 19:32:05 2006
@@ -0,0 +1,48 @@
+#include <vector>
+#include <iostream>
+#include <stdlib.h>
+#include <exception>
+#include "kernel.hh"
+#include "int.hh"
+#include "search.hh"
+
+#include "space_wrap.hh"
+
+
+
+
+class MyVar {
+    virtual MyVar* copy();
+};
+/*
+class MyIntVar : public MyVar {
+public:
+    MyIntVar( Space* spc, int _min, int _max ):_var(spc, _min, _max ) {}
+    MyIntVar( Space* spc, MyIntVar& mvar ):_var( spc, mvar._var ) {}
+protected:
+    IntVar _var;
+};
+*/
+enum {
+    REL_EQ,
+    REL_NQ,
+};
+
+
+class Index {};
+
+
+
+
+void* new_dfs( MySpace* spc, int c_d, int c_a )
+{
+    return new MyDFSEngine( spc, c_d, c_a );
+}
+
+MySpace* search_next( void* _search )
+{
+    MySearchEngine* search = static_cast<MySearchEngine*>(_search);
+    return search->next();
+}
+
+

Added: pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.hh
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/logic/gecode_wrapper/space_wrap.hh	Thu Mar  9 19:32:05 2006
@@ -0,0 +1,118 @@
+
+
+#ifndef SPACE_WRAP__HH
+#define SPACE_WRAP__HH
+
+
+#include <vector>
+#include <iostream>
+#include <stdlib.h>
+#include <exception>
+#include "kernel.hh"
+#include "int.hh"
+#include "search.hh"
+
+/*
+ */
+using namespace Gecode;
+using namespace std;
+
+class Unimplemented : public exception {};
+
+class MySpace : public Space {
+public:
+    MySpace() {}
+
+    int intVar( bool temp, int _min, int _max ) {
+	if (!temp) {
+	    _int_vars.push_back( IntVar(this, _min, _max) );
+	    return _int_vars.size()-1;
+	} else {
+	    throw Unimplemented();
+	}
+    }
+
+    void alldiff( int* begin, int*end )
+    {
+	IntVarArgs vars( end-begin );
+	for(int* i=begin;i!=end;++i) {
+	    vars[i-begin] = _int_vars[*i];
+	}
+	distinct( this, vars );
+    }
+
+    void linear( int* cb, int* ce, int* vb, int* ve, IntRelType reltype, int val )
+    {
+	IntArgs c(ce-cb);
+	IntVarArgs v(ve-vb);
+
+	for(int* i=cb;i!=ce;++i) {
+	    c[i-cb]=*i;
+	}
+	for(int* j=vb;j!=ve;++j) {
+	    v[j-vb]=_int_vars[*j];
+	}
+	Gecode::linear(this, c, v, reltype, val );
+    }
+
+    void branch()
+    {
+	IntVarArgs v(_int_vars.size());
+	for(int i=0;i<_int_vars.size();++i) {
+	    v[i] = _int_vars[i];
+	}
+	Gecode::branch(this, v, BVAR_SIZE_MIN, BVAL_MIN);
+    }
+
+
+    void int_values( int n, int* vars, int* values )
+    {
+	for(int i=0;i<n;++i) {
+	    int val;
+	    cout << _int_vars[vars[i]] << " ";
+	    val = _int_vars[vars[i]].val(); // ???
+	    values[i] = val;
+	}
+	cout << endl;
+    }
+
+    /// Constructor for cloning \a s
+    MySpace(bool share, MySpace& s) : Space(share,s), _int_vars(s._int_vars.size()) {
+	vector<IntVar>::iterator its,itd;
+	for(itd=_int_vars.begin(),its=s._int_vars.begin();itd!=_int_vars.end();++itd,++its) {
+	    itd->update(this, share, *its);
+	}
+    }
+    
+    /// Perform copying during cloning
+    virtual Space* copy(bool share) {
+	return new MySpace(share,*this);
+    }
+    
+    
+    void print_vars() {
+	for(int i=0;i<_int_vars.size();++i)
+	    cout << _int_vars[i] << " ";
+	cout << endl;
+    }
+
+protected:
+    vector< IntVar >  _int_vars;
+
+};
+
+
+class MySearchEngine {
+public:
+    virtual MySpace* next() = 0;
+};
+
+class MyDFSEngine : public MySearchEngine {
+public:
+    MyDFSEngine( MySpace* spc, int c_d, int c_a ):dfs(spc,c_d,c_a) {}
+    virtual MySpace* next() { return dfs.next(); }
+protected:
+    DFS<MySpace> dfs;
+};
+
+#endif



More information about the Pypy-commit mailing list