Shortest path in Answer Set Programming - logic-programming
I'm trying to find all the shortest path from one source node to all other destination node (so 1-3, 1-5, 1-4) with the relative cost for each shortest path.
I've tried with this code
node(1..5).
edge(1,2,1).
edge(2,3,9).
edge(3,4,4).
edge(4,1,4).
edge(1,3,1).
edge(3,5,7).
start(1).
end(3).
end(4).
end(5).
0{selected(X,Y)}1:-edge(X,Y,W).
path(X,Y):-selected(X,Y).
path(X,Z):-path(X,Y),path(Y,Z).
:-start(X),end(Y),not path(X,Y).
cost(C):-C=#sum{W,X,Y:edge(X,Y,W),selected(X,Y)}.
#minimize{C:cost(C)}.
#show selected/2.
but my code return this answer
> `clingo version 5.6.0 (c0a2cf99)
> Reading from stdin
> Solving...
> Answer: 1
> selected(3,4) selected(1,3) selected(3,5)
> Optimization: 12
> OPTIMUM FOUND
>
> Models : 1
> Optimum : yes
> Optimization : 12
> Calls : 1
> Time : 0.043s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
> CPU Time : 0.000s`
What is wrong? How can I enumerate all shortest paths with relative costs?
Surely an error is that you are aggregating all the costs in C but, if I have correctly understood, you need distinct costs depending on the ending node.
Then there may be also other errors, but I can't exactly understand what do you mean with that program.
I would write it as follows:
node(1..5) .
edge(1,2,1) .
edge(2,3,9) .
edge(3,4,4) .
edge(4,1,4) .
edge(1,3,1) .
edge(3,5,7) .
start(1) .
end(3) .
end(4) .
end(5) .
% For each destination E, some outgoing edge from the start node should be selected
:- start(S), end(E), not selected(S,_,E) .
% No edge pointing to the start node should be selected
:- start(S), selected(_,S,_) .
% If an edge points to the end node, then it may be (or not be) selected for reaching it
0{selected(X,E,E)}1 :- edge(X,E,_), end(E) .
% If an outgoing edge from Y has been selected for reaching E, then an incoming edge may be (or not be) selected for reaching E
0{selected(X,Y,E)}1 :- edge(X,Y,_), selected(Y,_,E) .
% Compute the cost for reaching E
cost(E,C) :- C=#sum{W : edge(X,Y,W), selected(X,Y,E)}, end(E) .
#minimize{C : cost(E,C)} .
#show selected/3 .
#show cost/2 .
The execution of the above program is as follows:
clingo version 5.3.0
Reading from test.lp
Solving...
Answer: 1
selected(3,5,5) selected(1,3,3) selected(3,4,4) selected(1,3,4) selected(1,3,5) cost(3,1) cost(4,5) cost(5,8)
Optimization: 14
OPTIMUM FOUND
Models : 1
Optimum : yes
Optimization : 14
Calls : 1
Time : 0.017s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time : 0.000s
where:
an atom select(X,Y,Z) indicates that the edge (X,Y) has been selected for reaching the node Z;
an atom cost(E,C) indicates that the minimum cost for reaching the end node E is C.
The starting node is implicit since it is unique.
Related
Writing my output into two columns in a text file .txt in C
I've looked around but couldn't find a satisfying solution... Basically I made a function that calculates the probability distribution of x number of loss in a portfolio of n credits... And I am trying to write the output in a text file into two columns where the first column would be the X (number of defaults) and second column would be the P(density function of each loss).. something like this: X P 1 0.005 2 0.003 3 0.005 4 0.005 5 0.005 etc. I've looked around and people suggested using negative- sign in front of my %d and %f when using fprintf but no luck.... Here's a sample of my code and the output it gives me... Code: for(i=0;i<d+1;i++) { Densite= gsl_ran_binomial_pdf(i,p,d); fprintf(pF,"%-5d %-20f .\n",i, Densite); } Output: 0 0.005921 . 1 0.031161 . 2 0.081182 . 3 0.139576 . 4 0.178143 . 5 0.180018 . 6 0.150015 . 7 0.106026 . 8 0.064871 . 9 0.034901 . 10 0.016716 . How to remedy? Thanks in advance! (complete noob that started coding in C like two days ago..)
Did you run the executable program on Windows or Linux? If Window please use \r\n for new line.
Prolog , endless loop
I did t he following code to go through all student Ids starting from 476 and ending at 520. schedule_errors(A,B,C):- Errors is 0, check_Courses(476,A,B,C,Errors). check_Courses(X,A,B,C,Errors):- . . . . . . Y is X+1, check_Courses(Y,A,B,C,Er). The problem is the programm keeps running indefinetly ignoring my exit loop predicate check_Courses(520,A,B,C,Er):- write('Check complete'). I can't understand what i am doing wrong. i Tried a similar easier version (just counting to 10) and it works fine loop(10):- write('cd finished'). loop(X):- write(X), nl, Y is X+1, loop(Y). What am i missing?
One important observation is that loop/1 does not terminate either. You can see this for example as follows: ?- loop(1), false. 1 2 3 ... 8 9 cd finished10 11 12 13 14 ... 49 50 51 ... 32394 32395 ... Note that the textual order in which you state your clauses in Prolog matters. If you exchange the two clauses of loop/1, then you do not get a single solution, only an endless stream of output: ?- loop(1). ... 42642 42643 ... So, in check_courses/5, if you put a more specific case after a case that subsumes it, then the textually first clause will always be tried first. Put simple cases before more complex cases!
Shortest path in labyrinth
Let us assume that we have a labyrinth represented by a m * n array of char . Our task is to find the fastest sequence of moves in order to transfer a carriage from starting position E to finish position S. Each element of the array has one of the following values : S , meaning starting position E , meaning finish position X , meaning obstacle . , meaning open space in which we can move T , meaning teleport. Each teleport is connected with all other teleports . Only the carriage can be sent through teleports , not the man himself. The man must later move to a teleport to get the carriage back. Each time , a man can either move left (L) , move right (R), move down (D) or move up (U) . When a man moves with the carriage , each step costs 2 seconds . If a man moves without carriage , each step costs 1 second . The use of the teleport system costs 2 seconds in total. Example : The array is: S . . . . . . . T . . T . . X E The optimal solution is : Down , Down , Right , Right , Right , Down The time cost is : 2 + 2 = 4 , for "Down , Down" 2 , in order to send the carriage with teleport. We continue without carriage. 1 + 1 + 1 = 3 , for "Right , Right, Right" . Note that each step costs 1 instead of 2 , because we move without carriage.We now pick the carriage again. 2 for "Down" . 2 because we have picked the carriage again. Total cost : 4 + 2 + 3 + 2 = 11. Final answer : Down , Down , Right , Right , Right , Down With is the most suitable algorithm for the task ? So far , I have considered the posibility of using dynamic programming .This would lead to a O(m*n) solution though... I also considered the possibility of applying some kind of Dijkstra , but I would need O(m*n) in order to create the graph...
algorithm for finding date in sorted array of dates
here is my problem. I have a sorted array of dates that is stored in a circular buffer. I have a pointer to last date in buffer. There is a possibility that some dates are missing. Client requires a range of dates. If low limit date is missing, program should return first closest date that is higher then required one and vice versa for upper limit date. Here is an example: Dates in circular buffer (int[18]): 1,2,3,4,5,11,12,13,14,15,21,22,23,24,25,26,27,28 and if client wants from 8 to 23, program should return 11,12,13,14,15,21,22,23. I tried like this : Notes: - number between two stars is current date, and diff is number of steps to go to find 8. - pointer can not be less then 0 or higher then 17. {1,2,3,4,5,11,12,13,14,15,21,22,23,24,25,26,27,*28*}, diff = -20 {*1*,2,3,4,5,11,12,13,14,15,21,22,23,24,25,26,27,28}, diff = +7 {1,2,3,4,5,11,12,*13*,14,15,21,22,23,24,25,26,27,28}, diff = -5 {1,2,*3*,4,5,11,12,13,14,15,21,22,23,24,25,26,27,28}, diff = +5 -> (5/2)+1=+3<br /> (if I detect that I will just go x steps forward and x steps backward I split x in half) {1,2,3,4,5,*11*,12,13,14,15,21,22,23,24,25,26,27,28}, diff = -3 -> (-3/2)-1 = -2 {1,2,3,*4*,5,11,12,13,14,15,21,22,23,24,25,26,27,28}, diff = 4 {1,2,3,4,5,11,12,*13*,14,15,21,22,23,24,25,26,27,28}, diff = -5 {1,2,*3*,4,5,11,12,13,14,15,21,22,23,24,25,26,27,28}, diff = +5 -> (5/2)+1=+3 If we continue like this we will get 13,3,11,4 over and over again. Notes: - It is only coincidence that we get 11 here. When I use some real examples, with more dates,this algorithm jumps over some other 4 (or 3) numbers. - Dates are stored in EEPROM of uC, so reading dates take a while, and I need to find date as quick as it possible (with minimum reads). Please help.
Set p1 to be the start of the buffer, p2 to be the end. X is what you're looking for. If the date of p1Date is after X, return p1. If p2Date is before X return p2. Look at the midpoint between p1 and p2, m. If mDate is after X then p1=m else p2=m. Repeat until p1=p2.
Npath and Cyclomatic complexity using Oclint
Can someone please explain me how to get the Npath and Cyclomatic complexity using the Oclint tool? I am currently using the command ./oclint /home/kyriakos/Measurements/base64.c -- -c and getting as output: OCLint Report Summary: TotalFiles=1 FilesWithViolations=1 P1=0 P2=0 P3=3 /home/kyriakos/Measurements/base64.c:18:5: short variable name P3 Variable name with 1 characters is shorter than the threshold of 3 /home/kyriakos/Measurements/base64.c:18:5: short variable name P3 Variable name with 1 characters is shorter than the threshold of 3 /home/kyriakos/Measurements//base64.c:18:5: short variable name P3 Variable name with 1 characters is shorter than the threshold of 3 [OCLint (http://oclint.org) v0.8.1]
OK just figure this out. You need to set the metric parameter to the minimum to force it throw a warning. For example: ./oclint -rc=CYCLOMATIC_COMPLEXITY=1 /home/kyriakos/Measurements/base64.c -- -g then I get: /home/kyriakos/base64.c:14:1: high cyclomatic complexity P2 Cyclomatic Complexity Number 7 exceeds limit of 1