Before getting into your second question, look again at your solution for combining serial resistors. You correctly matched the pattern: where ( verb(N1,R1,N)
and verb(N,R2,N2)
form possible serial pair providing no other "verb
"s are connected to N
. But aren’t there two more possibilities, namely:
-
verb(N,R1,N1)
and verb(N,R2,N2)
==> verb(N1,R1+R2,N2)
-
verb(N1,R1,N)
and verb(N2,R2,N)
==> verb(N1,R1+R2,N2)
So I had an additional goal after the two select/3
goals which looked for the different configurations combining two resistors in a serial connection (using your verb
functor name:
series_connection(verb(N1,R1,Nc),verb(Nc,R2,N2),verb(N1,Rn,N2),Nc) :-
!,
Rn is R1+R2.
series_connection(verb(Nc,R1,N1), verb(Nc,R2,N2), verb(N1,Rn,N2),Nc) :-
N1@<N2, !,
Rn is R1+R2.
series_connection(verb(N1,R1,Nc), verb(N2,R2,Nc), verb(N1,Rn,N2),Nc) :-
N1@<N2, !,
Rn is R1+R2.
I think your member2
check is correct but i had too look at it for while to figure out what it really does. Rather than using ";
"s (three of them, I think it’s preferable to split the cases into separate clauses using unification in the head:
member2(N,[verb(N,_,_)|T]) :- !. % begin node = N
member2(N,[verb(_,_,N)|T]) :- !. % end node = N
member2(N,[_|T]) :- % not connected to N, continue looking
member2(N,T).
For testing the resistance between specific nodes, is there a reason why you can just use memberchk/2
on the reduced network:
?- res_network(Rs),res_reduce(Rs,RRs),memberchk(resistance(a,R,c),RRs).
Rs = [resistance(a, 100, c), resistance(a, 200, b), resistance(b, 300, c), resistance(c, 50, d)],
RRs = [resistance(a, 83.33333333333333, c), resistance(c, 50, d)],
R = 83.33333333333333 ;
Rs = [resistance(a, 100, c), resistance(a, 200, b), resistance(b, 300, c), resistance(c, 50, d)],
RRs = [resistance(a, 500, c), resistance(a, 100, c), resistance(c, 50, d)],
R = 500 ;
Rs = RRs, RRs = [resistance(a, 100, c), resistance(a, 200, b), resistance(b, 300, c), resistance(c, 50, d)],
R = 100.
This makes it a little clearer why there are multiple answers; there are multiple equivalent networks, each with a resistance between a
and c
. Is one better than another? Why?
FInal query:
?- res_network(Rs),res_reduce(Rs,RRs),memberchk(resistance(a,R,b),RRs).
Rs = RRs, RRs = [resistance(a, 100, c), resistance(a, 200, b), resistance(b, 300, c), resistance(c, 50, d)],
R = 200.
From looking at the equivalent network, it’s clearer why a-100-c,c-300-b can’t be combined to form a parallel path from a to b. There’s another connection to c (from d). So a-133.33…-b isn’t a valid solution. Looks to me like the program is correct.