Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programing in Prolog Electricity consumers are supplied with electricity from an

ID: 3852981 • Letter: P

Question

Programing in Prolog

Electricity consumers are supplied with electricity from an electricity generating station. Electricity is distributed from the main power station to the various consumers (C_i) through a network of transformers (T_j) as shown in the diagram below. Nodes C1 to C10 are consumers; nodes T1 to T6 are transformers. Each consumer has a direct connection to only one transformer. Sometimes a transformer may malfunction or need to be taken out of service temporarily. Devise a list data structure in Prolog to represent networks like this. Write a Prolog program to define predicate supplies such that goal supplies(X, Y) succeeds when there is an electricity supply from node X to node Y in the network. Run and test your Prolog code from (a) and (b). Document your results. Explain how to enhance your representation to permit multiple connections that can be used to make a supply around a transformer that has been taken out of service.

Explanation / Answer

A. list data structure which represents network like this-

head(H,[H|_]).
tail(T,[_|T]). % T is list

   first(F,[F|_]). % the same as head
   last(L,[L]).
   last(L,[H|T]):-last(L,T).

   prefix([],_).
   prefix([H|T1],[H|T2]):-prefix(T1,T2).
   suffix(S,S).
   suffix([H|T],L):-suffix(T,L).

       member(X,[X|_]).
       member(X,[_|T]):-member(X,T).
       nth_member(1,[M|_],M).
       nth_member(N,[_|T],M):-N>1, N1 is N-1, nth_member(N1,T,M).


       append([],L,L).
       append([H|T],L,[H|LT]):-append(T,L,LT).

       prefix(P,L):-append(P,_,L).
       suffix(S,L):-append(_,S,L).

           sublist(S,L):-append(_,S,P),append(P,_,L).
           sublist2(S,L):-prefix(P,L),suffix(S,P).
           sublist3(S,L):-prefix(S,L).
           sublist3(S,[_|T]):-sublist3(S,T).

B.Prolog program to define predicate supplies-

path(X, Y, Length, Path) :-
path(X, Y, Length, Path, []).
  
   list_max([X|Xs], Max) :-
list_max_SUFFIX(Xs, X, Max).
  
  
   list_max_1([], Max, Max).
   list_max_1([X|Xs], Max0, Max) :-
compare(Order, X, Max0),
list_max_2(Order, X, Max0, Xs, Max).

   list_max_2(<, _, Max0, Xs, Max) :- list_max_1(Xs, Max0, Max).
   list_max_2(=, _, Max0, Xs, Max) :- list_max_1(Xs, Max0, Max).
   list_max_2(>, X, _, Xs, Max) :- list_max_1(Xs, X, Max).

C.Run the above code and add the output from your system in this part.

D.Enhance representation to permit multiple connections-

Consider this Prolog formulation as a working starting point with which you can solve such tasks:

distribution([], [], []).
distribution([C-Ps|CPs], Rs0, [C-As|CAs]) :-
allocation(Ps, As, Rs0, Rs1),
As = [_|_],
distribution(CPs, Rs1, CAs).

allocation(_, [], Rs, Rs).
allocation(Ps0, [A|As], Rs0, Rs) :-
select(A, Ps0, Ps1),
select(A, Rs0, Rs1),
allocation(Ps1, As, Rs1, Rs).
distribution/3 expects as its first argument a list of pairs of the form Consumer-Preferences, and as its second argument a list of resources.
It relates this instance description to solutions in the form of pairs Consumer-Allocated resources. Example query with SWI-Prolog for the concrete
task you specified:

?- distribution([a-[x,w],b-[x,y,v],c-[x,z],d-[z]], [x,w,y,v,z], Ds).
Ds = [a-[w], b-[y, v], c-[x], d-[z]] ;
Ds = [a-[w], b-[v, y], c-[x], d-[z]] ;
false.
You can use this formulation for all tasks of this kind. The formulation is complete: It reports all solutions that exist (and some redundantly,
because the allocated resources may be stated in any order, as you already see in the example above; you can introduce symmetry breaking
constraints in allocation/4 to avoid this - one way to solve this is to insist that As be ascending with respect to the standard term order),
and hence there are no (further) solutions if it answers false.