Stable marriage problem: Difference between revisions

(Modified for standard library updates)
Line 1,170:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% rules
 
stable_mariage :-
new(LstMan, chain),
Line 1,188 ⟶ 1,189:
new(LstCouple, chain),
% creation of the couple.
send(LstWoman, for_all, and(message(@prolog, cree_couplecreate_couple, @arg1, LstCouple),
message(@pce, write_ln, @arg1?name, with, @arg1?elu?name))),
 
nl,
% test of the stability of couples
stability(LstCouple),
Line 1,211 ⟶ 1,213:
send(@pce, write_ln, NC2?second, with, NC2?first),
nl,
stability(LstCouple).
 
 
transform(PceCouple, couple(First, Second)):-
get(PceCouple?first, value, First),
get(PceCouple?second, value, Second).
 
 
 
get_two_random_couples(Len, C1, C2) :-
Line 1,226 ⟶ 1,222:
C1 \= C2.
 
cree_couplecreate_couple(Woman, LstCouple ) :-
send(LstCouple, append, new(_, tuple(Woman?elu?name, Woman?name))).
 
% iterations of the algorithm
 
cree_couple(Woman, LstCouple ) :-
send(LstCouple, append, new(_, tuple(Woman?elu, Woman?name))).
 
 
round(LstMan, LstWoman) :-
send(LstMan, for_some, message(@arg1, propose)),
Line 1,241 ⟶ 1,235:
true
).
 
% XPCE template class for a person
:-pce_begin_class(person, object, "description of a person").
variable(name, object, both, "name of the person").
variable(preference, chain, both, "list of priority").
variable(liste, chain, both, "list of persons of the other sex").
variable(status, object, both, "statut of engagement : maybe / free").
 
Line 1,252 ⟶ 1,245:
send(P, slot, name, Name),
send(P, slot, preference, Pref),
send(P, slot, liste, new(_, chain)),
send(P, slot, status, free).
 
% reception of the list of partners
init_liste(P, Lst) :->
% we replace the list of name of partners
send(P, slot, liste, Lst).
% with the list of persons partners.
new(NLP, chain),
sendget(P, slot, listepreference, LstLP).,
send(LP, for_all, message(@prolog, find_person,@arg1, Lst, NLP)),
send(P, slot, listepreference, new(_, chainNLP)),.
 
:- pce_end_class(person).
 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% XPCE class for a man
find_person(Name, LstPerson, LstPref) :-
get(LstLstPerson, find, @arg1?name == PrefName, Elem),
send(LstPref, append, Elem).
 
:-pce_begin_class(man, person, "description of a man").
 
Line 1,270 ⟶ 1,272:
propose(P) :->
get(P, slot, status, free),
get(P, slot, name, Name),
get(P, slot, preference, XPref),
get(XPref, delete_head, Pref),
send(P, slot, preference, XPref),
send(ElemPref, proposition, NameP).
get(P, slot, liste, Lst),
get(Lst, find, @arg1?name == Pref, Elem),
send(Elem, proposition, Name).
 
refuse(P) :->
Line 1,286 ⟶ 1,285:
:- pce_end_class(man).
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% XPCE class for a woman
:-pce_begin_class(woman, person, "description of a woman").
variable(elu, object, both, "name of the elu").
Line 1,312 ⟶ 1,309:
send(R, sort, ?(@arg1?first, compare, @arg2?first)),
get(R, delete_head, Tete),
send(ElemTete?second, accept),
get(P, slot, liste, Lst),
get(Lst, find, @arg1?name == Tete?second, Elem),
send(Elem, accept),
send(P, slot, status, maybe),
send(P, slot, elu, Tete?second),
send(R, for_some, message(P@arg1?second, send_refuse, @arg1refuse)),
send(P, slot, contact, new(_, chain)) .
 
 
% looking for the person of the given name Contact
Line 1,324 ⟶ 1,320:
fetch(P, Contact, Chain) :->
get(P, slot, preference, Lst),
sendget(Lst, for_somefind, message(@prolog,arg1?name search, @arg1,== Contact?name, @arg2Elem), Chain)).
get(PLst, slotindex, nameElem, NameInd),
send(Chain, append, new(_, tuple(IndexInd, NomContact))).
 
% a woman receive a proposition from a man
Line 1,331 ⟶ 1,329:
send(C, append, Name),
send(P, slot, contact, C).
 
% the man is "jilted"
send_refuse(P, Man) :->
get(P, slot, liste, Lst),
get(Lst, find, @arg1?name == Man?second, Elem),
send(Elem, refuse).
 
:- pce_end_class(woman).
 
search(Nom, Nom, Index, Chain) :-
send(Chain, append, new(_, tuple(Index, Nom))).
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computation of the stability od couple
stability(LstCouple) :-
chain_list(LstCouple, LstPceCouple),
maplist(transform, LstPceCouple, PrologLstCouple),
study_couples(PrologLstCouple, [], CoupleInstableUnstableCouple),
( CoupleInstableUnstableCouple = []
->
writeln('Couples are stable')
;
sort(CoupleInstableUnstableCouple, SortCoupleInstableSortUnstableCouple),
writeln('Unstable couples are'),
maplist(print_unstable_couple, SortCoupleInstableSortUnstableCouple),
nl
).
 
 
print_unstable_couple((C1, C2)) :-
format('~w and ~w~n', [C1, C2]).
 
transform(PceCouple, couple(First, Second)):-
get(PceCouple?first, value, First),
get(PceCouple?second, value, Second).
 
study_couples([], UnstableCouple, UnstableCouple).
Line 1,371 ⟶ 1,364:
append(CurrentUnstableCouple, Lst1,CurrentUnstableCouple1)
;
CurrentUnstableCouple1 = CurrentUnstableCouple
),
study_couples(T, CurrentUnstableCouple1, UnstableCouple).
 
 
build_one_couple(C1, C2, (C1, C2)).
Line 1,400 ⟶ 1,394:
( (IY12 < IY11 , IX21 < IX22);
(IY21 < IY22 , IX12 < IX11)).
 
</lang>
The output :
Anonymous user