Re: [eclipse-clp-users] Q: Linear Programming with Disjunction constraints

From: Kish Shen <kisshen_at_cisco.com>
Date: Fri, 25 Apr 2008 22:29:35 +0100
Kim Lai wrote:

> 
> hi, I'm trying to use "eplex_var_get" in C++ and get trouble with the 
> EC_word's type.
> In eclipse , everything works fine.
> ----------------------
> Vars = [A1,A2]
> eplex_var_get( A1, solution, A1 ), eplex_var_get( A2, solution, A2 ).
> -------------------------
> But after I put this in C++, I can't figure out what is the exact type 
> of this EC_word.
> It should be a double. But .is_double(&d) doesn't work.
> -------like this one--------------
> EC_refs Vars(2); double d;
> for( int i=0; i<2; i++ ) {
>      post_goal( term( EC_functor( "eplex_var_get", 3), Vars[i], 
> EC_atom("solution"), Vars[i] ));
> *     if( EC_word(Vars[i]).is_double(&d) == EC_succeed ) {*
>             cout<< "Edge-"<< i<< "="<< d <<endl;
>        }
> }
> ------------------------------------------------------
> 
> thanks.
> 
> -- 
> ....Best Regards
>                       by Kim Lai, 賴廣甫
> Welcome to visit http://kimklai.blogspot.com

It looks like you based your code on some of the C++ examples that comes 
with ECLiPSe, and also available on the ECLiPSe website. Unfortunately, 
these examples are not really that good -- they try to present ECLiPSe 
in a similar way to a C++ constraint solving library (e.g. ILOG solver), 
but this really isn't the best way to use ECLiPSe from C++. In 
particular, it is generally not a good idea to build an ECLiPSe program 
using post_goal(). Instead, you should have your ECLiPSe program in 
ECLiPSe, which you can load (compile) into ECLiPSe, and from C++, you 
use post_goal to post a simple query to execute the program and get back 
the result.

So in your example, you should write an ECLiPSe program much like the 
one you showed, except that you also get the solution values. An example 
of this is shown in the eplex manual chapter in the Constraints manual:

mip_example2([X,Y], Cost) :-
      my_instance: (X+Y $>= 3),
      my_instance: (X-Y $= 0),
      my_instance: integers([X]),
      my_instance: eplex_solver_setup(min(X)),
      my_instance: eplex_solve(Cost),
      my_instance: eplex_var_get(X, typed_solution, X),
      my_instance: eplex_var_get(Y, typed_solution, Y).


and if you really want to run this from C++, you do a post_goal with 
mip_example2(Vars, Cost) as your goal.

The problem with with constructing your program (and indeed any ECLiPSe 
structure) in C++ is that this is error-prone, and hard to debug.

In addition, there is a problem with the code you are shwing -- posting 
goals with post_goal() does not cause the code to be run immediately. 
You need to call Ec_resume() to pass control to ECLiPSe to allow the 
posted goals to be executed -- your code does not call Ec_resume(), so 
you cannot get the results back from ECLiPSe, as the goals have not been 
executed yet.

Cheers,

Kish
Received on Fri Apr 25 2008 - 14:30:14 CEST

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:58 CET