Backtracking Problem

This is a discussion on Backtracking Problem within the General AI Programming forums, part of the Cprogramming.com and AIHorizon.com's Artificial Intelligence Boards category; Could someone please guide me through backtracking in Prolog-C using the simple example below? Here is my Prolog file: Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    26

    Backtracking Problem

    Could someone please guide me through backtracking in Prolog-C using the simple example below?

    Here is my Prolog file:
    Code:
        likes( john, mary ).
    
        likes( john, emma ).
    
        likes( john, ashley ).
    Here is my C file:
    Code:
        #include...
    
        term_t tx;
        term_t tv;
        term_t goal_term;
        functor_t goal_functor;
    
        int main( int argc, char** argv )
        {
            argv[0] = "libpl.dll";
            PL_initialise( argc, argv );
            PlCall( "consult( swi( 'plwin.rc' ) )" );
            PlCall( "consult( 'likes.pl' )" );
    
            tv = PL_new_term_ref( );
            PL_put_atom_chars( tv, "john" );
            tx = PL_new_term_ref( );
            goal_term = PL_new_term_ref( );
            goal_functor = PL_new_functor( PL_new_atom( "likes" ), 2 );
            PL_cons_functor( goal_term, goal_functor, tv, tx );
    
            PlQuery q( "likes", ??? );
            while ( q.next_solution( ) )
             {
              char* solution;
              PL_get_atom_chars( tx, &solution );
              cout << solution << endl;
             }
    
            PL_halt( PL_toplevel() ? 0 : 1 );
        }
    What should I replace ??? with?

    Or is this the right approach to get all the backtracking results generated and printed?

    Thank you,

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    26

    Backtracking plus Hanoi and Factorial

    I have included the complete programs for Hanoi, Factorial, and Backtracking below. I hope beginners will benefit from them. If anybody is not sure about VS2008 settings for interfacing C++ and Prolog, I would gladly provide them with.
    Code:
        #include <iostream>
        #include <fstream>
        #include <string>
        #include <math.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <stdafx.h>
        using namespace std;
        #include "Windows.h"
        #include "ctype.h"
        #include "SWI-cpp.h"
        #include "SWI-Prolog.h"
        #include "SWI-Stream.h"
     
        term_t t;
        predicate_t p;
     
        term_t tf;
        term_t tx;
     
        term_t tv;
        term_t tu;
        predicate_t pred;
     
        term_t goal_term;
        functor_t goal_functor;
     
        int main( int argc, char** argv )
        {
            argv[ 0 ] = "libpl.dll";
            PL_initialise( argc, argv );
            PlCall( "consult( swi( 'plwin.rc' ) )" );
            PlCall( "consult( 'myPrologFile.pl' )" );
     
    
            cout << "Enter your hanoi number: ";
            int n;
            cin >> n;
            cout << "Calculating hanoi of " << n << endl;
            PL_put_integer( t, n );
            p = PL_predicate( "hanoi", 1, NULL );
            PL_call_predicate( NULL, PL_Q_NORMAL, p, t );
     
    
            cout << "Enter your factorial number: ";
            int nf;
            cin >> nf;
            cout << "Calculating factorial of " << nf << endl;
            tf = PL_new_term_ref( );
            PL_put_integer( tf, nf );
            tx = PL_new_term_ref( );
            goal_term = PL_new_term_ref( );
            goal_functor = PL_new_functor( PL_new_atom( "factorial" ), 2 );
            PL_cons_functor( goal_term, goal_functor, tf, tx );
            int fact;
            if ( PL_call( goal_term, NULL ) )
            {
                PL_get_integer( tx, &fact );
                cout << fact << endl;
            }
            else
            {
                PL_fail;
            }
    
    
            cout << "Backtracking . . .";
            tv = PL_new_term_ref( );
            PL_put_atom_chars( tv, "john" );
            pred = PL_predicate( "likes", 2, NULL );
            tu = PL_new_term_ref( );
            qid_t qid = PL_open_query( NULL, PL_Q_NODEBUG, pred, tv );
            while ( int i = PL_next_solution( qid ) )
            {
                char* solution;
                PL_get_atom_chars( tu, &solution );
                cout << solution << endl;
            }
            PL_close_query( qid );
     
    
            PL_halt( PL_toplevel( ) ? 0 : 1 );
        }
    This is myPrologFile.pl
    Code:
        :- use_module( library( shlib ) ).
        :- use_module( library( lists ) ).
    
    
        hanoi( N ):-
            move( N, left, center, right ).
     
        move( 0, _, _, _ ):-
            !.
        move( N, A, B, C ):-
            M is N-1,
            move( M, A, C, B ),
            inform( A, B ),
            move( M, C, B, A ).
     
        inform( X, Y ):-
            write( 'move a disk from ' ),
            write( X ),
            write( ' to ' ),
            write( Y ),
            nl.
    
    
        factorial( 1, 1 ):-
            !.
        factorial( X, Fac ):-
            X > 1,
            Y is X - 1,
            factorial( Y, New_Fac ),
             Fac is X * New_Fac.
    
    
        likes( john, mary ).
        likes( john, emma ).
        likes( john, ashley ).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. backtracking problem
    By samsung in forum C Programming
    Replies: 2
    Last Post: 05-28-2008, 02:32 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21