Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me for a simple question, please

Status
Not open for further replies.

ergunbagci

Programmer
Nov 29, 2004
1
TR
QUESTION IS BELOW
I AM OPEN TO ANY HELP...
THANKS ADVANCE...

""Your task is to write a Prolog program which supports the predicates “findOps” and “apprSol”, whose specifications are as follows. The predicate findOps takes three arguments: a “target” integer, a list of “usable” integers, and an initially free “solution” variable, to which any exact solution to the problem is to be bound. An exact solution is a list of arithmetic operations, which start by the usable integers, have integers as intermediate results, and end up by the target integer. Furthermore, findOps is supposed to find the shortest solution (i.e. the one containing the fewest operations) as its first answer. If the user is in a hurry, and findOps does not give a result in a reasonable time, the user can interrupt the run of findOps (using the pulldown menu of SWI-Prolog, or by pressing control-break, and then pressing “a” when asked for an action,) and he/she can then run the predicate apprSol to find the best approximate solution that has been found up to that time by the program. The two arguments of apprSol are the list representing that approximate solution, and an integer representing the “error” in that solution (defined by the absolute value of the difference of the result of the approximate solution and the target).

For example, consider the query

?- findOps(534,[12,54,3,7,19],Sol).

My version of this program responds

Sol = [multiply(3, 7):21, add(21, 19):40, multiply(40, 12):480, add(480, 54):534]



Yes



But for the query

?- findOps(534,[12,54,3,7,20],Sol).

it does not respond for some time, so I break the execution,

Action (h for help) ? abort

% Execution Aborted

and then I submit the query

?- apprSol(AS,Error).

to which it gives the answer

AS = [add(12, 3):15, add(54, 20):74, multiply(74, 7):518, add(518, 15):533]

Error = 1

Yes

I used the “:” operator in the syntax above, because it is already predefined as a binary operator in Prolog.

Hints: As mentioned in class, you can use the standard “append” predicate in the program in the form “append(L,_,_)” to bind the variable L to increasingly long list templates, in order to find shorter solutions earlier than longer solutions. The library predicate “integer” and the arithmetic function “abs” can be used when necessary. It may be a good idea to have a three-argument predicate called “makeAnOperation”, which takes the current list of usable integers as an input argument, and binds one of the other arguments to a representation of an arithmetic operation (something like, for example, add(12, 3):15) performed on two of the usable integers, and the other argument to a suitably updated version of the list of usable integers. This predicate would have the ability of returning another possible operation on the given list every time it is asked for an alternative solution. The “del” predicate from Bratko’s book might be useful when writing that predicate.""


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top