Reputation: 255
I started sicstus prolog recently and have this homework to solve with CLP (constraint logic programing), please help me understand the problem, what I should be looking for and what I'm doing wrong. So,
A building company:
The BC pretends to build a Skyscrapers with 150 floors above ground and 15 underground, how must the building be organized so profit is maximized and and time required minimized?
Define the problem as a constraint satisfaction problem and solve it with CLP so it is possible to solve it with different parameters (more or less floors, or adjusting teams sobreposition times).
And what I have so far:
:- use_module(library(clpfd)).
start(Fdigs,Floors,Vars):-
length(Vars,5),
%S -> start, E -> end, D -> duration
%t -> transport, e -> excavating, f -> floor (build)
Vars=[St,Et,Se,Ee,Cost],
Se #>= Et,
Ee #= Se + 10,
Et #>=6,
Cost #>= Ee * 80, %transport cost since it starts at 0 Ee will be the days needed
domain(Vars,0,2000),
construct(0,Fdigs,Floors,Vars),
labeling([minimize(Cost)],Vars).
construct(Stock,0,Floors,Vars).
construct(Stock,Fdigs,Floors,[St,Et,Se,Ee,Cost]):-
Tasks=[
task(St,6,Et,0,1), %transport task
task(Se,10,Ee,5,2) %dig task, 5 because I'm digging all then building (20-15)
%10 because build is 10 so dig is 10
],
cumulative(Tasks,[limit(Stock)]),
Nfdigs is Fdigs - 1,
Nstock is Stock + 25,
construct(Nstock,Nfdigs,[Et,_Et,Ee,_Ee,Cost]).
And it doesn't work of course, because I cannot use cumulative with a task requiring 5 resources and a limit of 0 resources is the first of many issues I can't get around...
Upvotes: 3
Views: 893
Reputation: 255
Here it's what I came up with so far, seems to be a lot easier and getting me the result expected:
:- use_module(library(clpfd)).
start(Floors,Fdigs,Vars):-
length(Vars,20),
domain(Vars,0,999999999), %all vars must be bounded
Vars=[ % S-start,D-duration,E-end,C-cost (money)
St,Dt,Et,Ct, % transport vars
Se,De,Ee,Ce, % excavation vars
Sb,Db,Eb,Cb, % building vars
Sc,Dc,Ec,Cc, % electricity vars
Machines,Crane,Cost,Time],
Machines in 1..2,
Crane in 0..1,
St is 0,
Dt is ceiling(((Floors*15)+(Fdigs*20))/25)*6,
Et is Dt,
Ct is 80 * Dt,
Se is 0,
De #= (13 - (Machines*3)) * Fdigs,
Ee #= De,
Ce #= De * (75*Machines),
Sb #>=6 #/\ Sb #>= Ee,
Db #= ((Fdigs * (10-(Crane*3))) + (Floors * (10-(Crane*3)))),
Eb #= Sb + Db,
Cb #= (Fdigs + Floors) * 150 + (Crane*Db*120),
Sc #= (Eb * 75 / 100),
Dc #>= (Eb * 25 / 100) + 5 #/\ Dc #=((Floors + Fdigs) * 5) + 5,
Ec #= Sc + Dc #/\ Ec #> Eb,
Cc is (Fdigs + Floors) * 200,
%Costs
Cost #= (Ct + Ce + Cb + Cc),
%Times (will be end time of final & nonoverlaping task)
Time #= Ec,
%labeling([minimize(Cost)],Vars), % - cost, + time
labeling([maximize(Cost)],Vars), % + cost, - time
writeVars(Vars).
writeVars([St,Dt,Et,Ct,Se,De,Ee,Ce,Sb,Db,Eb,Cb,Sc,Dc,Ec,Cc,Machines,Crane,Cost,Time]):-
write('1 '),write(St),write(' '),write(Dt),write(' '),write(Et),write(' '),write(Ct),write('\n'),
write('2 '),write(Se),write(' '),write(De),write(' '),write(Ee),write(' '),write(Ce),write('\n'),
write('3 '),write(Sb),write(' '),write(Db),write(' '),write(Eb),write(' '),write(Cb),write('\n'),
write('4 '),write(Sc),write(' '),write(Dc),write(' '),write(Ec),write(' '),write(Cc),write('\n'),
write('Cost: '),write(Cost),write('\n'),
write('Time: '),write(Time),write('\n'),
write('Crane: '),write(Crane),write('\n'),
write('Machines: '),write(Machines).
%write('4 ',St,' ', Dt,' ', Et,' ', Ct),
%write('5 ',St,' ', Dt,' ', Et,' ', Ct).
Any sugestions, comments that improve my answer are very much welcome.
Upvotes: 3