i need to write code for an acedemic council elections
so far i have the below code but stuck now.
it takes in members with their number of votes from each depatment. there is a restriction in that a certain number of male and females employees must be selected (gender balancing)
the allocate i cannot get working.
any help folks??
thanking in advance!!
seats(12,7,5).
getsortedcandlist(x):- setof([A,B,C,D],
candidate(A,B,C,D),R),
bubblesortcands(R,X).
member(A,[A|_]).
member(A,[_|B]):- member(A,B).
bubblesortcands(L,L).
bublesortcands(L,R):- swap(L,L2),
!,
bubblesortcands(L2,R).
genderbalanceon.
genderbalance(true):- assert(genderbalanceon).
genderbalance(false):- abolish(genderbalanceon,0).
initall:- getsortedcandlist(X),
member([A,B,C,D],X),
assert(elected(A,B,C,D)),
fail.
swap([[A,B,C,D], [E,F,G,H]|T], [[E,F,G,H],[A,B,C,D]|T]):- D>H.
swap([X|R],[X|P]):- swap(R,P).
deptcount([],_,0).
deptcount([[_,D,_,_]|T],D,C):- !,
deptcount(T,D,X),
C is X+1.
deptcount([_|T],D,C):- deptcount(T,D,C).
deptmaxok(L,D):- deptcount(L,D,C),
department(D,_,M,_),
M>=C.
deptminok(L,D):- deptcount(L,D,C),
department(D,_,_,M),
C>=M.
schoolcount([],_,0).
schoolcount([[_,D,_,_]|T],S,C):- department(D,S,_,_),
!,
schoolcount(T,S,X),
C is X+1.
schoolcount([_|T],S,C):-schoolcount(T,S,C).
schoolmaxok(L,S):- schoolcount(L,S,C),
school(S,M,_),
M>=C.
schoolminok(L,S):- schoolcount(L,S,C),
school(S,_,M),
C>=M.
schoolminok(L,S):- getsortcandlist(R),
schoolcount(R,S,C),
schoolcount(L,S,X),
school(S,_,M),
C<M,
C==X.
gendercount([],_,0).
gendercount([[_,_,G,_]|T],G,C):- !,
gendercount(T<G<X),
C is X+1.
gendercount([_|T],G,C):-deptcount(T,G,C).
gendermaxok(L):- gendercount(L,m,MC),
gendercount(L,f,FC),
seats(_,MAX,_),
MAX>=MC,
MAX>=FC.
gendermaxok(_):- not(genderbalanceon).
genderminok(L):- gendercount(L,m,MC),
gendercount(L,f,FC),
seats(_,_,MIN),
MC>=MIN,
FC>=MIN.
genderminok(_):- not(genderbalanceon).
deptgenderminok(L,B):- setof([A,B,C,D],
member([A,B,C,D],L),X),
genderminok(X). /*not working yet*/
getdeptlist(L):- setof(A,B^C^D^department(A,B,C,D),L).
getschoollist(L):- setof(A,B^C^school(A,B,C),L).
allocminseats(R):- getsortedcandlist(CL),
getdeptlist(DL),
getschoollist(SL),
allocdeptmin(DL,[],R). /* not done*/
allocdeptmin([H|T],L,A):- deptminok(L,H),
!,
allocdeptmin(T,L,A).
allocdeptmin([H|T],L,A):- getsortedcandlist(R),
member(J,R),
j=[_,H,_,_],
not(member(J,L)),
allocatedeptmin([H|T],
[J|L],A).
allocatedeptmin([],A,A).
/*allocate([],A,A).
allocate([H|T],L,A):- not(member(H,L),
H=[N,D,G,V].*/
invalidlist(L):- not(gendermaxok(L)).
invalidlist(L):- not(genderminok(L)).
invalidlist(L):- department(D,_,_,_),
not(deptminok(L,D)).
invalidlist(L):- department(D,_,_,_),
not(deptmaxok(L,D)).
invalidlist(L):- school(S,_,_),
not(schoolminok(L,S)).
invalidlist(L):- school(S,_,_),
not(schoolmaxok(L,S)).
invalidlist(L):- seats(X,_,_),
length(L,Len),
Len\==X.
school(science,3,1).
school(business,3,1).
school(humanities,3,1).
school(engineering,3,1).
school(health,1,1).
school(adulted,2,1).
department(sp,science,2,1).
department(ee,engineering,2,1).
department(hl,humanities,2,1).
department(ha,humanities,2,1).
candidate(joe,sp,m,20).
candidate(ann,ee,f,16).
candidate(tony,ee,m,27).
candidate(lisa,hl,f,34).
candidate(barry,ha,m,22).
candidate(ben,ee,m,29).
so far i have the below code but stuck now.
it takes in members with their number of votes from each depatment. there is a restriction in that a certain number of male and females employees must be selected (gender balancing)
the allocate i cannot get working.
any help folks??
thanking in advance!!
seats(12,7,5).
getsortedcandlist(x):- setof([A,B,C,D],
candidate(A,B,C,D),R),
bubblesortcands(R,X).
member(A,[A|_]).
member(A,[_|B]):- member(A,B).
bubblesortcands(L,L).
bublesortcands(L,R):- swap(L,L2),
!,
bubblesortcands(L2,R).
genderbalanceon.
genderbalance(true):- assert(genderbalanceon).
genderbalance(false):- abolish(genderbalanceon,0).
initall:- getsortedcandlist(X),
member([A,B,C,D],X),
assert(elected(A,B,C,D)),
fail.
swap([[A,B,C,D], [E,F,G,H]|T], [[E,F,G,H],[A,B,C,D]|T]):- D>H.
swap([X|R],[X|P]):- swap(R,P).
deptcount([],_,0).
deptcount([[_,D,_,_]|T],D,C):- !,
deptcount(T,D,X),
C is X+1.
deptcount([_|T],D,C):- deptcount(T,D,C).
deptmaxok(L,D):- deptcount(L,D,C),
department(D,_,M,_),
M>=C.
deptminok(L,D):- deptcount(L,D,C),
department(D,_,_,M),
C>=M.
schoolcount([],_,0).
schoolcount([[_,D,_,_]|T],S,C):- department(D,S,_,_),
!,
schoolcount(T,S,X),
C is X+1.
schoolcount([_|T],S,C):-schoolcount(T,S,C).
schoolmaxok(L,S):- schoolcount(L,S,C),
school(S,M,_),
M>=C.
schoolminok(L,S):- schoolcount(L,S,C),
school(S,_,M),
C>=M.
schoolminok(L,S):- getsortcandlist(R),
schoolcount(R,S,C),
schoolcount(L,S,X),
school(S,_,M),
C<M,
C==X.
gendercount([],_,0).
gendercount([[_,_,G,_]|T],G,C):- !,
gendercount(T<G<X),
C is X+1.
gendercount([_|T],G,C):-deptcount(T,G,C).
gendermaxok(L):- gendercount(L,m,MC),
gendercount(L,f,FC),
seats(_,MAX,_),
MAX>=MC,
MAX>=FC.
gendermaxok(_):- not(genderbalanceon).
genderminok(L):- gendercount(L,m,MC),
gendercount(L,f,FC),
seats(_,_,MIN),
MC>=MIN,
FC>=MIN.
genderminok(_):- not(genderbalanceon).
deptgenderminok(L,B):- setof([A,B,C,D],
member([A,B,C,D],L),X),
genderminok(X). /*not working yet*/
getdeptlist(L):- setof(A,B^C^D^department(A,B,C,D),L).
getschoollist(L):- setof(A,B^C^school(A,B,C),L).
allocminseats(R):- getsortedcandlist(CL),
getdeptlist(DL),
getschoollist(SL),
allocdeptmin(DL,[],R). /* not done*/
allocdeptmin([H|T],L,A):- deptminok(L,H),
!,
allocdeptmin(T,L,A).
allocdeptmin([H|T],L,A):- getsortedcandlist(R),
member(J,R),
j=[_,H,_,_],
not(member(J,L)),
allocatedeptmin([H|T],
[J|L],A).
allocatedeptmin([],A,A).
/*allocate([],A,A).
allocate([H|T],L,A):- not(member(H,L),
H=[N,D,G,V].*/
invalidlist(L):- not(gendermaxok(L)).
invalidlist(L):- not(genderminok(L)).
invalidlist(L):- department(D,_,_,_),
not(deptminok(L,D)).
invalidlist(L):- department(D,_,_,_),
not(deptmaxok(L,D)).
invalidlist(L):- school(S,_,_),
not(schoolminok(L,S)).
invalidlist(L):- school(S,_,_),
not(schoolmaxok(L,S)).
invalidlist(L):- seats(X,_,_),
length(L,Len),
Len\==X.
school(science,3,1).
school(business,3,1).
school(humanities,3,1).
school(engineering,3,1).
school(health,1,1).
school(adulted,2,1).
department(sp,science,2,1).
department(ee,engineering,2,1).
department(hl,humanities,2,1).
department(ha,humanities,2,1).
candidate(joe,sp,m,20).
candidate(ann,ee,f,16).
candidate(tony,ee,m,27).
candidate(lisa,hl,f,34).
candidate(barry,ha,m,22).
candidate(ben,ee,m,29).