Re: [eclipse-clp-users] Optimize

From: Marco Gavanelli <marco.gavanelli_at_unife.it>
Date: Fri, 06 Nov 2009 11:24:33 +0100
Ismael München wrote:
> Hi,
> 
> I have one question...
> 
> How do I for maximize the variable Valor in this example?
> 
> :- lib(ic).
> 
> fabrica(Tarefa) :-
>  Custo = []( [](5, 1, 3, 4, 6),
>      [](8, 8, 5, 7, 3),
>      [](4, 9, 1, 5, 2)),
> 
>  Tarefa = [T1, T2, T3],
>  Tarefa :: 1..5,
> 
>  labeling(Tarefa),
> 
>  Valor #= Custo[1, T1] + Custo[2, T2] + Custo[3, T3],
>  
>  Valor #>= 15,
>  
>  writeln(Valor).

Dear Ismael,

to optimize for some objective function, you can have a look at the 
manual, library branch_and_bound.

Please note that in your code you impose the constraints after the 
labeling. This will slow down the execution of your program, as, if the 
constraints are not satisfied, ECLiPSe will have to backtrack and choose 
another assignment. You are not exploiting constraint propagation, but 
you are doing a generate-and-test.

Indeed, if you move the labeling after the constraint

	Valor #= Custo[1, T1] + Custo[2, T2] + Custo[3, T3]

you get an error, because now the value of the indexes T1, T2 and T3 is 
unknown at this point. So, you could change your constraint model.
You could represent your matrix Custo as a list of lists:

Custo = [[5, 1, 3, 4, 6],
      [8, 8, 5, 7, 3],
      [4, 9, 1, 5, 2]],

Custo = [Row1,Row2,Row3],

and then use the element constraint to select a value from a list:

	element(T1,Row1,CostRow1),
	element(T2,Row2,CostRow2),
	...

(or, better, write a predicate that imposes the element constraint for 
every row in Custo). Then you can define the cost as:

	Valor #= CostRow1+VostRow2+CostRow3

(or, better, use sumlist, if you have a list of "CostRow" variables), 
and finally

	 minimize( labeling(Tarefa) , Valor).

Cheers,
Marco

-- 
Marco Gavanelli, Ph.D. in Computer Science
Dept of Engineering
University of Ferrara
http://www.ing.unife.it/docenti/MarcoGavanelli/
Received on Fri Nov 06 2009 - 10:24:52 CET

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