% Load and link an external predicate that will, in turn, call the % GNU Scientific Library to calculate the CDF (ie, area under the curve) % of the unit Gaussian distribution. :- load("gsl/gsl.so"). :- external(gsl_std_normal_cdf/2, std_normal_cdf). % -------------------------------------------------------------------------- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Dispatch Control % Please see accompanied documentation for details % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Pick a local car at the nearest base or the nearest car on the road, % depends on how closer to the incident the nearest car is. % % Doesn't use a reward function. % proc(allocResManual(Duration), pi(t, ?(now(t)) : if( % Work time remains t < Duration, % THEN limit( % Query all the cars pi(globalRangeName, pi(globalCarList, ?(carRange(all, globalRangeName)) : ?(range(globalRangeName, globalCarList)) : limit(queryAllCars(globalCarList)) )) : if( % At least one region has a pending request, % and one of its local car is mobilizable some(region, some(x, some(y, some(anycar, isARegion(region) & validXCoord(region, x) & validYCoord(region, y) & requestPending(loc(x, y)) & isACar(anycar) & homeRegion(anycar, region) & ready(anycar) & carLocKnown(anycar, t))))), %THEN pi(region, pi(x, pi(y, pi(nCar, pi(nDist, pi(nLoc, pi(bDist, % Locate that request, and find the nearest mobilizable % local car, along with its distance from the request ?( isARegion(region) & validXCoord(region, x) & validYCoord(region, y) & requestPending(loc(x, y)) & isACar(nCar) & homeRegion(nCar, region) & ready(nCar) & carLocKnown(nCar, t) & carLocation(nCar, nLoc, t) & distance(nLoc, loc(x,y), nDist) & all(otherCar, all(otherLoc, all(otherDist, ( (isACar(otherCar) & homeRegion(otherCar, region) & ready(otherCar) & -(otherCar=nCar) & carLocKnown(otherCar, t) & carLocation(otherCar, otherLoc, t) & distance(otherLoc, loc(x,y), otherDist)) => otherDist >= nDist) ))) ) : % Find the distance from the request to the base ?( some(base, some(bLoc, isABase(base) & in(base, region) & locOf(base,bLoc) & distance(loc(x,y), bLoc, bDist) )) ) : if( % Some cars at the base is mobilizable and, % the base is not very futher away from the incident % compares to the nearest car some(car, isACar(car) & homeRegion(car, region) & atBase(car) & ready(car) & carLocKnown(car, t) & bDist - nDist =< 2 ), %THEN % Mobilize the car from the base pi(car, ?( isACar(car) & homeRegion(car, region) & atBase(car) & ready(car) & carLocKnown(car, t) ): mobilize(car, loc(x, y), t) ), %ELSE % Mobilize the nearest car mobilize(nCar, loc(x, y), t) ) ))))))), %ELSE wait(t) ) ) : allocResManual(Duration), %ELSE noOp(t) ) ) ). % % Use the reward function to pick a car, anywhere in the city % proc(allocResAuto(Duration), pi(t, ?(now(t)) : if(t < Duration, % THEN limit( pi(rangeName, pi(carList, % Query all the cars ?(carRange(all, rangeName)) : ?(range(rangeName, carList)) : limit(queryAllCars(carList)) : if( % Request is pending, and a car is mobilizable some(region, some(x, some(y, some(anycar, isARegion(region) & validXCoord(region, x) & validYCoord(region, y) & requestPending(loc(x, y)) & isACar(anycar) & ready(anycar) & carLocKnown(anycar, t) )))), % THEN pi(region, pi(x, pi(y, % Find where the incident is ?(isARegion(region) & validXCoord(region, x) & validYCoord(region, y) & requestPending(loc(x, y)) ) : % Pick a car and mobilize it pickBest(bestCar, rangeName, mobilize(bestCar, loc(x, y), t)) ))), % ELSE wait(t) ) )) ) : allocResAuto(Duration), % ELSE noOp(t) ) ) ). /* proc(allocResOpt(Duration), pi(t, ?(now(t)) : if(t < Duration, %then limit( pi(rangeName, pi(carList, % Query all the cars ?(carRange(all, rangeName)) : ?(range(rangeName, carList)) : limit(queryAllCars(carList)) : pi(d, ?(write("Before first test: ")) : ?(date(d)) : ?(writeln(d))) : if( % There are two pending requests, and two mobilizable cars some(region1, some(region2, some(x1, some(x2, some(y1, some(y2, some(anycar1, some(anycar2, isARegion(region1) & validXCoord(region1, x1) & validYCoord(region1, y1) & requestPending(loc(x1, y1)) & isARegion(region2) & validXCoord(region2, x2) & validYCoord(region2, y2) & (-(x1 = x2) v -(y1 = y2)) & requestPending(loc(x2, y2)) & isACar(anycar1) & isACar(anycar2) & -(anycar1 = anycar2) & ready(anycar1) & carLocKnown(anycar1, t) & ready(anycar2) & carLocKnown(anycar2, t) )))))))), %then pi(d, ?(write("After first test - Before horizon 2 decision making ")) : ?(date(d)) : ?(writeln(d))) : pi(region1, pi(region2, pi(x1, pi(x2, pi(y1, pi(y2, % Find the two request locations ?(isARegion(region1) & validXCoord(region1, x1) & validYCoord(region1, y1) & requestPending(loc(x1, y1)) & isARegion(region2) & validXCoord(region2, x2) & validYCoord(region2, y2) & (-(x1 = x2) v -(y1 = y2)) & requestPending(loc(x2, y2)) ) : % Pick a pair of cars and mobilize them pickBest(bestCar1, rangeName, pickBest(bestCar2, rangeName, mobilize(bestCar1, loc(x1, y1), t) : mobilize(bestCar2, loc(x2, y2), t))) )))))), % : % pi(d, ?(write("After horizon 2 decision making ")) : ?(date(d)) : ?(writeln(d))), %else pi(d, ?(write("After first test - Before second test: ")) : ?(date(d)) : ?(writeln(d))) : if( % There is one pending request, and a mobilizable car some(region, some(x, some(y, some(anycar, isARegion(region) & validXCoord(region, x) & validYCoord(region, y) & requestPending(loc(x, y)) & isACar(anycar) & ready(anycar) & carLocKnown(anycar, t) )))), %then pi(d, ?(write("After second test - Before horizon 1 decision making: ")) : ?(date(d)) : ?(writeln(d))) : pi(region, pi(x, pi(y, % Find where the request is ?(isARegion(region) & validXCoord(region, x) & validYCoord(region, y) & requestPending(loc(x, y)) ) : % Pick a car and mobilize it pickBest(bestCar, rangeName, mobilize(bestCar, loc(x, y), t)) ))), % : % pi(d, ?(write("After horizon 1 decision making: ")) : ?(date(d)) : ?(writeln(d))), %else % There is no pending request, or no car is mobilizable pi(d, ?(write("After second test - No decision making: ")) : ?(date(d)) : ?(writeln(d))) : wait(t) ) ) )) ) : allocResOpt(Duration), %else noOp(t) ) ) ). */ proc(allocResOpt(Duration), pi(t, ?(now(t)) : if(t < Duration, %then limit( pi(rangeName, pi(carList, % Query all the cars ?(carRange(all, rangeName)) : ?(range(rangeName, carList)) : limit(queryAllCars(carList)) : ( % If possible, try to serve two requests simultaneously pi(region1, pi(region2, pi(x1, pi(x2, pi(y1, pi(y2, % Find the two different requests ?(isARegion(region1) & validXCoord(region1, x1) & validYCoord(region1, y1) & requestPending(loc(x1, y1)) & isARegion(region2) & validXCoord(region2, x2) & validYCoord(region2, y2) & (-(x1 = x2) v -(y1 = y2)) & requestPending(loc(x2, y2)) ) : % Pick a pair of cars and mobilize them pickBest(bestCar1, rangeName, ?(isACar(bestCar1) & ready(bestCar1) & carLocKnown(bestCar1, t)) : pickBest(bestCar2, rangeName, ?(isACar(bestCar2) & ready(bestCar2) & carLocKnown(bestCar2, t)) : mobilize(bestCar1, loc(x1, y1), t) : mobilize(bestCar2, loc(x2, y2), t) ) ) )))))) # % Otherwise, try to serve one pi(region, pi(x, pi(y, % Find where the request is ?(isARegion(region) & validXCoord(region, x) & validYCoord(region, y) & requestPending(loc(x, y)) ) : % Pick a car and mobilize it pickBest(bestCar, rangeName, ?(isACar(bestCar) & ready(bestCar) & carLocKnown(bestCar, t)) : mobilize(bestCar, loc(x, y), t) ) ))) # % Otherwise, wait wait(t) ) )) ) : allocResOpt(Duration), %else noOp(t) ) ) ). /* --------------------------------------- */ % % Helper Procedure % % Go through every car that belongs to this region and % make sure that if the car is ready, its location is known % proc(queryAllCars(List), pi(car, pi(otherCars, pi(t, ?(now(t)) : if( nonEmptyList(List), % THEN ?(firstCarInList(List, car, otherCars)) : % ?(writeln(List)) : % ?(write(car)) : if( % Car is ready but not mobilizable isACar(car) & ready(car) & -carLocKnown(car, t) & -commLost(car), % THEN % ?(writeln("is ready but not carLocKnown")) : % Ask the dispatcher for its location pi(loc, askPosition(car, loc, t) : queryAllCars(otherCars) ), % ELSE % ?(writeln("is either not ready or ready but locIsKnow")) : queryAllCars(otherCars) ), % ELSE noOp(t) ) ))) ).