Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anhduong89 committed Jun 10, 2024
0 parents commit d3389e7
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 0 deletions.
94 changes: 94 additions & 0 deletions 8.0.1-planning.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

avai(D, V, 0) :- init_loc(D, V).


% #program plan(T).
%--------generation
0 {as(D, N, T): avai(D, V, T), agent(D), time(T)} 1 :- request(N, _, _, _, T), time(T).

% each drone can be assigned once at each time step.
:- as(D, N1, T), as(D, N2, T), N1 != N2, time(T).

% if number requests > number avai drone: all avai drone must be assigned
:- C1 = #count{N:request(N, _, _, _, T), time(T)}, C2=#count{D:avai(D, _, T), time(T)}, C3 = #count{N: as(_, N, T), time(T)}, time(T), C1 > C2, C3 != C2.

% if number requests < number avai drone: all requests must be assigned
:- C1= #count{N:request(N, _, _, _, T), time(T)}, C2=#count{D:avai(D, _, T), time(T)}, C3 = #count{N: as(_, N, T), time(T)}, time(T), C1 <= C2, C3 != C1.


%-------change location
%location of assigned agent but not at origin
avai(D, V', T+2) :- as(D, N, T), request(N, V1, V', _, T), avai(D, V, T), V!= V1, time(T).

% location of not assign agent
avai(D, V, T+1) :- not as(D, _, T), avai(D, V, T), time(T).

%location of assigned agent at origin
avai(D, V', T+1) :- as(D, N, T), request(N, V, V', _, T), avai(D, V, T), time(T).

%---------cost
cost(N, C, T) :- as(D, N, T), request(N, V1, V', _, _), avai(D, V, T), operation_cost(V1, V, C).

iteration_cost(TC, T) :- TC = #sum {C, N: cost(N, C, T), time(T)}, time(T).

#minimize{TC: iteration_cost(TC, T), time(T)}.

%--------- auxiliary
incomplete_request(N, T+1) :- request(N, V, V', W, T), not as(_, N, _), time(T).

request(N,V, V', W, T) :- new_request(N, T), request(N, V, V', W, _), time(T).

complete :- C = #count {D, N: as(D, N, _)}, total_request(TR), C = TR.

#show as(D, N, T): as(D, N, T), time(T).
% #show cost/3.
% #show iteration_cost/2.
% #show incomplete_request/2.
% #show request/5.
% #show avai(D, V, T) : avai(D, V, T), time(T).
% #show test/4.
% #show total_cost/1.
% #show time/1.
% #show request(N, V, V', W, T) : request(N, V, V', W, T), time(T1), T = T1.

% time(0..3).

% agent(0..2).
% vertiport(0..5).
% horizon(2).
% init_loc(0, 3).
% init_loc(1, 0).
% init_loc(2, 2).
% request(0, 1, 4, 20, 1).
% request(2, 1, 0, 22, 1).
% request(3, 2, 3, 23, 1).
% request(1, 4, 0, 21, 2).
% % request(4, 4, 1, 24, 2).
% % request(5, 2, 0, 21, 2).
% % request(6, 1, 4, 22, 2).

% operation_cost(0, 1, 21).
% operation_cost(1, 0, 21).
% operation_cost(0, 2, 23).
% operation_cost(2, 0, 23).
% operation_cost(0, 3, 24).
% operation_cost(3, 0, 24).
% operation_cost(0, 4, 22).
% operation_cost(4, 0, 22).
% operation_cost(1, 2, 24).
% operation_cost(2, 1, 24).
% operation_cost(1, 3, 22).
% operation_cost(3, 1, 22).
% operation_cost(1, 4, 22).
% operation_cost(4, 1, 22).
% operation_cost(2, 3, 20).
% operation_cost(3, 2, 20).
% operation_cost(2, 4, 20).
% operation_cost(4, 2, 20).
% operation_cost(3, 4, 24).
% operation_cost(4, 3, 24).
% operation_cost(0, 0, 0).
% operation_cost(1, 1, 0).
% operation_cost(2, 2, 0).
% operation_cost(3, 3, 0).
% operation_cost(4, 4, 0).
100 changes: 100 additions & 0 deletions 8.0.1-planning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Dec 9 2023
import clingo
from clingo.symbol import Function
from clingo.symbol import Number
from clingo import symbol
import sys
import tempfile
import time

#

def on_model(model):
print("Found solution:", model)

def get_atom(name, arg):
atom_arg = []
for i in arg:
# if len(str(i)) > 1:
# atom_arg += .join(str(i))
# else:
# print(i)
atom_arg += [str(i)]
# if name == 'new_request':
# print('arg=', arg)

atom_full = name +'(' + ','.join(atom_arg) +'). '

return atom_full



#instance and program
input = 'rq.lp'
network = 'network.lp'
init = 'init.lp'
program = '8.0.1-planning.lp'



#INIT
total_cost = 0
avai = ''
assignment = ''
request = " "
start_time = time.process_time()
#asp
stop = False
i = 0
while stop == False:

atom = 'time(' + str(i) + ').'
prog1 = clingo.Control(["0", "--opt-mode=optN"])
prog1.add(request)
request = ''
prog1.add(atom)
prog1.add(avai)
prog1.add(assignment)
prog1.load(program)
prog1.load(input)
prog1.load(network)
prog1.load(init)
#display
# print('available agent round ', i, ':', avai)
# print('incomplete_request :', request)
lenAS = len(assignment)
prog1.ground([("base", [])])
with prog1.solve(yield_=True, async_=True) as models: # type: ignore
models.wait(600)

min_cost = float('inf')
for model in models:
# print(f'Optimal:{models.optimality_proven}\n')

if model.cost[0] < min_cost:
min_model = model

for atom in model.symbols(atoms=True):
if atom.name == "avai":
avai += get_atom(atom.name, atom.arguments)
if atom.name == "as":
assignment += get_atom(atom.name, atom.arguments)
if atom.name =='incomplete_request':
request += get_atom("new_request", atom.arguments)
if atom.name =='iteration_cost':
total_cost += (atom.arguments[0].number)
if atom.name =='complete':
stop = True
models.cancel()
i+=1
# if len(assignment) == lenAS: break
# if i ==11: break
# print('output:', avai)
end_time = time.process_time()
running_time = end_time - start_time
with open('log.txt', "a") as f:
f.write(f'running_time: {running_time}\n')




Empty file added README.md
Empty file.
29 changes: 29 additions & 0 deletions gen_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys
import random
import os

atom = ''
part=[]
def get(val, default):
return val if val != None else default


nAgents = int(sys.argv[1])


vPort_id = [0, 1, 2, 3, 4]

with open('init.lp',"w+") as f:

#gen agent
f.write(f'agent(0..{nAgents}).\n')
#gen init
for id in range(0, nAgents):
origin = random.choice(vPort_id)
f.write(f'init_loc({id},{origin}).\n')






21 changes: 21 additions & 0 deletions gen_rq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys
import random
import os
nb_request = int(sys.argv[1])
nb_time_step = int(sys.argv[2])
vPort_id = [0, 1, 2, 3, 4]

with open('rq.lp',"w+") as f:
#gen total request
f.write(f'total_request({nb_request}).\n')
#gen horizon
f.write(f'horizon({nb_time_step}).\n')
#gen request

for id in range(0, nb_request):
origin, destination = random.sample(vPort_id, 2)
time_step = random.choice(range(0, nb_time_step))
f.write(f'request({id},{origin},{destination}, 20, {time_step}).\n')



6 changes: 6 additions & 0 deletions init.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
agent(0..5).
init_loc(0,0).
init_loc(1,4).
init_loc(2,1).
init_loc(3,4).
init_loc(4,4).
4 changes: 4 additions & 0 deletions log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
running_time: 0.026445999999999997
running_time: 0.027538
running_time: 0.032479999999999995
running_time: 0.033680999999999996
26 changes: 26 additions & 0 deletions network.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
vertiport(0..4).
operation_cost(0, 1, 12).
operation_cost(1, 0, 12).
operation_cost(0, 2, 4).
operation_cost(2, 0, 4).
operation_cost(0, 3, 8).
operation_cost(3, 0, 8).
operation_cost(0, 4, 51).
operation_cost(4, 0, 51).
operation_cost(1, 2, 18).
operation_cost(2, 1, 18).
operation_cost(1, 3, 22).
operation_cost(3, 1, 22).
operation_cost(1, 4, 24).
operation_cost(4, 1, 24).
operation_cost(2, 3, 15).
operation_cost(3, 2, 15).
operation_cost(2, 4, 20).
operation_cost(4, 2, 20).
operation_cost(3, 4, 36).
operation_cost(4, 3, 36).
operation_cost(0, 0, 0).
operation_cost(1, 1, 0).
operation_cost(2, 2, 0).
operation_cost(3, 3, 0).
operation_cost(4, 4, 0).
12 changes: 12 additions & 0 deletions rq.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
total_request(10).
horizon(5).
request(0,1,3, 20, 0).
request(1,0,1, 20, 2).
request(2,4,2, 20, 3).
request(3,0,3, 20, 3).
request(4,2,4, 20, 1).
request(5,1,4, 20, 1).
request(6,1,3, 20, 1).
request(7,2,3, 20, 0).
request(8,3,1, 20, 3).
request(9,0,4, 20, 4).
13 changes: 13 additions & 0 deletions solver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
for nb_drone in 5 10 20 40
do
for nb_request in 15 30 45
do
echo "nb_drone: $nb_drone, nb_request: $nb_request\n" >> log.txt
for run in {1..5}
do
echo "Run $run :" >> log.txt
python3 gen_init.py nb_drone
python3 gen_request.py nb_request 5
python3 8.0.1-planning.py

0 comments on commit d3389e7

Please sign in to comment.