At uni we are studying concurrency using a very simple program which controls several robots within an area. They simulate processes/ threads. The whole thing comes bundled in a sort of mockup IDE which has a graphical console, all of this is programmed in Java, but we program in a sort of pseudocode.
(please excuse lack of knowledge about concurrency, we literally have three weeks to learn concepts and are graded in the fourth week).
We are asked to program robot types for different tasks. We may have several instances of the same type of robot running at once in order to have them doing the same thing in different places.
The robots must perform their tasks without bumping into other robots. They achieve this by:
- sending and receiving messages. The message sending is asynchronous and the receiving is synchronous (this blocks the robot which is receiving until the message arrives).
- blocking the part of the area which they need (the area is a 100 x 100 matrix with x,y coordinates). We can block a particular "corner" if we need to do something there, but must then unblock it after leaving so as to not lower concurrency.
- sectoring off parts of the whole area into smaller private ones (for one robot) or semi private areas (for a few robots). Areas can be private, partly shared or completely shared, with the idea of simulating the memory in concurrency.
The tasks the robots perform are to drop or pick up objects from a corner. They can also move in a straight line and turn the corner.
We were asked to make a program to resolve the following issue.
Three collector robots positioned in their own respective corners(3,3),(4,4),(5,5) need to collect from the same corner, let's say (10,10).
One boss robot, in (1,1), uses a random function to pick which of the three robots will go to the shared corner to collect the goods.
The program needs to consider that: - the robots must all know when there may be nothing to collect from the start or when there is no longer anything to collect. It is inevitable that a lot of message sending will need to happen, but we do need to consider that the less messages we send the better because they lower concurrency. Similarily with corner blocking.
Once all of the goods have been collected the boss will inform which of the three collector robots has collected the most.
First of all the collector robots are all of the same type. So they have the same programming. One of them may go to the corner first, find nothing, communicate to the boss robot that there is nothing and that then the rest of the collector robots must stop when the boss informs them that there is nothing to collect.
Then, the same applies for once they are collecting. At some point, they will all collect, one of them will collect the last thing, and needs to communicate that to the rest, probably communicating it to the boss who will communicate that to the rest. They must all stop.
Finally, the boss must inform which one of the robots got the most elements. A maximum function.
This has so far worked allright if not efficiently and I am not sure if completely correctly.
I am including the pseudocode, In order to get some feedback. I know it can be done better, but qhenever I change anything to try and refactor the code, it just breaks and something stops working.
I really can't see the forest for the trees right now and the exams are coming up next week. Any insight is welcomed.
program threeCollectors
functions
function max (ES max: integer; ES maxrobot: integer;
E aux: integer; E id: integer)
begin
if (max < aux)
max:= aux
maxrobot:=id
end
function sendID
begin
SendMsg(1, robot1)
SendMsg(2, robot2)
SendMsg(3, robot3)
end
function collect (E myPosX: integer; E myPosY: integer;
ES boxes: integer; ES plants:integer; ES goOn: integer; ES ok: boolean)
begin
BlockCorner(10,10)
Pos(10,10)
if ~(ThereIsBox) & ~(ThereIsPlant)
ok:= F
goOn:=0
Pos(myPosY,myPosX)
FreeCorner(10,10)
SendMsg(goOn, robot0)
Print('dontGoOn',goOn)
else
ok:= V
goOn:=1
if(ThereIsBox) & (ThereIsPlant)
getBox
boxes:=boxes+1
getPlant
plants:=plants+1
else
if (ThereIsBox)
getBox
boxes:=boxes+1
else
if (ThereIsPlant)
getPlant
plants:=plants+1
SendMsg(goOn, robot0)
Pos(myPosY,myPosX)
FreeCorner(10,10)
ReceiveMsg(goOn, robot0)
end
areas
areaJ : AreaP(1,1,1,1)
area: AreaPC(3,3,10,10)
robots
robot boss
variables
t:integer
id: integer
max:integer
maxrobot: integer
goOn: integer
total: integer
begin
max:= -1
total:= 0
goOn:= 1
sendID
while (goOn = 1)
Random(id, 1, 3)
Print('envio',id)
if(id = 3)
SendMsg(goOn, robot3)
ReceiveMsg(goOn, robot3)
else
if (id = 1)
SendMsg(goOn, robot1)
ReceiveMsg(goOn, robot1)
else
if(id = 2)
SendMsg(goOn, robot2)
ReceiveMsg(goOn, robot2)
if (goOn =0)
SendMsg(goOn, robot1)
SendMsg(goOn, robot2)
SendMsg(goOn, robot3)
for 3
ReceiveMsg(id, *)
if (id = 1)
ReceiveMsg(t, robot1)
else
if (id = 2)
ReceiveMsg(t, robot2)
else
if (id = 3)
ReceiveMsg (t, robot3)
max(max, maxrobot, t, id)
Print('gano',maxrobot)
Print('collected',t)
end
robot collector
variables
boxes: integer
plants: integer
myPosY:integer
myPosX:integer
whoami: integer
goOn: integer
total: integer
ok: boolean
begin
total:=0
boxes:= 0
plants:= 0
ok:=V
myPosY:=PosY
myPosX:=PosX
ReceiveMsg(whoami, robot0)
ReceiveMsg(goOn, robot0)
while (goOn = 1) & (ok = V)
collect (myPosX,myPosY, boxes,plants,goOn,ok)
total:= plants + boxes
SendMsg(whoami, robot0)
SendMsg(total, robot0)
end
variables
robot0: boss
robot1,robot2,robot3: collector
begin
AsignArea(robot0, areaJ)
AsignArea(robot1,area)
AsignArea(robot2,area)
AsignArea(robot3,area)
Init(robot0, 1,1)
Init(robot1, 3, 3)
Init(robot2, 4, 4)
Init(robot3, 5, 5)
Aucun commentaire:
Enregistrer un commentaire