76 lines
2.2 KiB
Text
76 lines
2.2 KiB
Text
// Agent operator in project airSeaRescue.mas2j
|
|
|
|
/* quadrant allocation */
|
|
|
|
@quads[atomic]
|
|
+gsize(W,H) : true
|
|
<- // calculate the area of each quadrant and remember them
|
|
.print("Defining quadrants for ",W,"x",H," simulation ",S);
|
|
+quad(1, 0, 0, W div 2 - 1, H - 1);
|
|
+quad(2, W div 2, 0, W-1, H - 1);
|
|
.print("Finished all quadrs for ").
|
|
|
|
+init_pos(X,Y)[source(A)]
|
|
: // if all miners have sent their position
|
|
.count(init_pos(_,_),2)
|
|
<- .print("* InitPos ",A," is ",X,"x",Y);
|
|
// remember who doesn't have a quadrant allocated
|
|
// (initially all miners)
|
|
+~quad(searchDrone1); +~quad(searchDrone2);
|
|
!assign_all_quads([1,2]).
|
|
+init_pos(X,Y)[source(A)]
|
|
<- .print("- InitPos ",A," is ",X,"x",Y).
|
|
|
|
|
|
+!assign_all_quads([]).
|
|
+!assign_all_quads([Q|T])
|
|
<- !assign_quad(Q);
|
|
!assign_all_quads(T).
|
|
// assign quadrant Q to a miner
|
|
+!assign_quad(Q)
|
|
: quad(Q,X1,Y1,X2,Y2) &
|
|
~quad(_) // there still is a miner without quadrant
|
|
<- .findall(Ag, ~quad(Ag), LAgs);
|
|
!calc_ag_dist(Q,LAgs,LD);
|
|
.min(LD,d(Dist,Ag));
|
|
.print(Ag, "'s Quadrant is: ",Q);
|
|
-~quad(Ag);
|
|
.send(Ag,tell,quadrant(X1,Y1,X2,Y2)).
|
|
|
|
+!calc_ag_dist(Q,[],[]).
|
|
+!calc_ag_dist(Q,[Ag|RAg],[d(Dist,Ag)|RDist])
|
|
: quad(Q,X1,Y1,X2,Y2) & init_pos(AgX,AgY)[source(Ag)]
|
|
<- // get the distance between X1,Y1 and AgX,AgY
|
|
operator.dist(X1,Y1,AgX,AgY,Dist);
|
|
!calc_ag_dist(Q,RAg,RDist).
|
|
|
|
|
|
/* negotiation for found gold */
|
|
|
|
+bid(Victim,D,Ag)
|
|
: .count(bid(Victim,_,_),3) // three bids were received
|
|
<- .print("bid from ",Ag," for ",Victim," is ",D);
|
|
!allocate_miner(Victim);
|
|
.abolish(bid(Victim,_,_)).
|
|
+bid(Victim,D,Ag)
|
|
<- .print("bid from ",Ag," for ",Victim," is ",D).
|
|
|
|
+!allocate_miner(Victim)
|
|
<- .findall(op(Dist,A),bid(Victim,Dist,A),LD);
|
|
.min(LD,op(DistCloser,Closer));
|
|
DistCloser < 10000;
|
|
.print("Victim ",Victim," was allocated to ",Closer, " options were ",LD);
|
|
.broadcast(tell,allocated(Victim,Closer)).
|
|
//-Gold[source(_)].
|
|
-!allocate_miner(Victim)
|
|
<- .print("could not allocate victim ",Victim).
|
|
|
|
|
|
/* end of simulation plans */
|
|
|
|
@end_of_simulation[atomic]
|
|
+end_of_simulation(_) : true
|
|
<- .print("-- END "," --");
|
|
.abolish(init_pos(_,_)).
|
|
|
|
|