This commit is contained in:
Kaushik Narayan R 2024-11-25 01:04:38 -07:00
parent d491a8504a
commit 0e791579bd
5 changed files with 126 additions and 57 deletions

BIN
Appendix.pdf Normal file

Binary file not shown.

BIN
Milestone - 2.docx Normal file

Binary file not shown.

BIN
Milestone - 2.pdf Normal file

Binary file not shown.

View File

@ -2,55 +2,59 @@
%%%%%%%%% House Reconfiguration Problem %%%%%%%%%% %%%%%%%%% House Reconfiguration Problem %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% given element domains % domain sets
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
person(P) :- legacyConfig(person(P)). % legacy - only short things
% HCP - only short
thing(T) :- legacyConfig(thing(T)). thing(T) :- legacyConfig(thing(T)).
% HRP - short and long % new - short and long
thing(T) :- thingLong(T). thing(T) :- thingLong(T).
thing(T) :- thingShort(T). thing(T) :- thingShort(T).
thingShort(T) :- legacyConfig(thing(T)), not thingLong(T). % a thing can't be both
% a thing can't be long AND short thingShort(T) :- thing(T), not thingLong(T).
:- thingLong(T), thingShort(T). :- thingLong(T), thingShort(T).
% all possible cabinets
cabinetDomain(C) :- legacyConfig(cabinet(C)). cabinetDomain(C) :- legacyConfig(cabinet(C)).
cabinetDomain(C) :- cabinetDomainNew(C). cabinetDomain(C) :- cabinetDomainNew(C).
% all possible rooms
roomDomain(R) :- legacyConfig(room(R)). roomDomain(R) :- legacyConfig(room(R)).
roomDomain(R) :- roomDomainNew(R). roomDomain(R) :- roomDomainNew(R).
%%%%%%%%%%%%%%%%%%% person(P) :- legacyConfig(person(P)).
% mappings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% solution mappings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% each thing is owned by a single person % each thing is owned by a single person
personTOthing(P, T) :- legacyConfig(personTOthing(P, T)). personTOthing(P, T) :- legacyConfig(personTOthing(P, T)).
% each thing is stored in a single cabinet % each thing is stored in a single cabinet
1 { cabinetTOthing(C, T) : cabinetDomain(C) } 1 :- thing(T). 1 { cabinetTOthing(C, T) : cabinetDomain(C) } 1 :- thing(T).
cabinetTOthing(C, T) :- legacyConfig(cabinetTOthing(C, T)).
% each cabinet is located in a single room % each cabinet is located in a single room
1 { roomTOcabinet(R, C) : roomDomain(R) } 1 :- cabinet(C). 1 { roomTOcabinet(R, C) : roomDomain(R) } 1 :- cabinet(C).
roomTOcabinet(R, C) :- legacyConfig(roomTOcabinet(R, C)).
% each room belongs to a single person, who owns things stored in cabinets that are located in that room % each room belongs to a single person, who owns things stored in cabinets that are located in that room
personTOroom(P, R) :- personTOthing(P, T), cabinetTOthing(C, T), roomTOcabinet(R, C). personTOroom(P, R) :- personTOthing(P, T), cabinetTOthing(C, T), roomTOcabinet(R, C).
legacyConfig(personTOroom(P, R)) :- legacyConfig(personTOthing(P, T)), legacyConfig(cabinetTOthing(C, T)), legacyConfig(roomTOcabinet(R, C)).
%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% derived solution domains % solution sets
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% cabinet
cabinet(C) :- cabinetTOthing(C, T). cabinet(C) :- cabinetTOthing(C, T).
cabinet(C) :- roomTOcabinet(R, C). cabinet(C) :- roomTOcabinet(R, C).
% subtypes
% cabinet subtypes
1 { cabinetSmall(C); cabinetHigh(C) } 1 :- cabinet(C). 1 { cabinetSmall(C); cabinetHigh(C) } 1 :- cabinet(C).
% long things can only be stored in high cabinets % long things can only be stored in high cabinets
% :- thingLong(T), cabinetTOthing(C, T), cabinetSmall(C).
cabinetHigh(C) :- thingLong(T), cabinetTOthing(C, T). cabinetHigh(C) :- thingLong(T), cabinetTOthing(C, T).
% room
room(R) :- roomTOcabinet(R, C). room(R) :- roomTOcabinet(R, C).
room(R) :- personTOroom(P, R). room(R) :- personTOroom(P, R).
@ -58,8 +62,9 @@ room(R) :- personTOroom(P, R).
cabinet(C1) :- cabinetDomainNew(C1), cabinetDomainNew(C2), cabinet(C2), C1 < C2. cabinet(C1) :- cabinetDomainNew(C1), cabinetDomainNew(C2), cabinet(C2), C1 < C2.
room(R1) :- roomDomainNew(R1), roomDomainNew(R2), room(R2), R1 < R2. room(R1) :- roomDomainNew(R1), roomDomainNew(R2), room(R2), R1 < R2.
%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% constraints % constraints
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% no shared ownership of things or rooms % no shared ownership of things or rooms
:- personTOthing(P1, T), personTOthing(P2, T), P1 != P2. :- personTOthing(P1, T), personTOthing(P2, T), P1 != P2.
@ -79,61 +84,112 @@ cabinetSize(C, 2) :- cabinetHigh(C).
roomSlotUsage(R, C, S) :- roomTOcabinet(R, C), cabinetSize(C, S). roomSlotUsage(R, C, S) :- roomTOcabinet(R, C), cabinetSize(C, S).
:- #sum { S, C : roomSlotUsage(R, C, S) } > 4, room(R). :- #sum { S, C : roomSlotUsage(R, C, S) } > 4, room(R).
%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% reconfiguration % reconfiguration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% legacy -> solution === reuse or delete elements % legacy -> solution === either reuse or remove elements
1 { reuse(room(R)); delete(room(R)) } 1 :- legacyConfig(room(R)). 1 { reuse(room(R)); remove(room(R)) } 1 :- legacyConfig(room(R)).
1 { reuse(cabinetHigh(C)); reuse(cabinetSmall(C)); delete(cabinet(C)) } 1 :- legacyConfig(cabinet(C)). 1 { reuse(cabinet(C)); remove(cabinet(C)) } 1 :- legacyConfig(cabinet(C)).
1 { reuse(personTOroom(P, R)); delete(personTOroom(P, R)) } 1 :- legacyConfig(personTOroom(P, R)). 1 { reuse(personTOroom(P, R)); remove(personTOroom(P, R)) } 1 :- legacyConfig(personTOroom(P, R)).
1 { reuse(roomTOcabinet(R, C)); delete(roomTOcabinet(R, C)) } 1 :- legacyConfig(roomTOcabinet(R, C)). 1 { reuse(roomTOcabinet(R, C)); remove(roomTOcabinet(R, C)) } 1 :- legacyConfig(roomTOcabinet(R, C)).
1 { reuse(cabinetTOthing(C, T)); delete(cabinetTOthing(C, T)) } 1 :- legacyConfig(cabinetTOthing(C, T)). 1 { reuse(cabinetTOthing(C, T)); remove(cabinetTOthing(C, T)) } 1 :- legacyConfig(cabinetTOthing(C, T)).
% reused elements are a part of the solution % reused elements MUST be a part of the solution
room(R) :- reuse(room(R)). room(R) :- reuse(room(R)).
cabinetHigh(C) :- reuse(cabinetHigh(C)). cabinet(C) :- reuse(cabinet(C)).
cabinetSmall(C) :- reuse(cabinetSmall(C)).
personTOroom(P, R) :- reuse(personTOroom(P, R)). personTOroom(P, R) :- reuse(personTOroom(P, R)).
roomTOcabinet(R, C) :- reuse(roomTOcabinet(R, C)). roomTOcabinet(R, C) :- reuse(roomTOcabinet(R, C)).
cabinetTOthing(C, T) :- reuse(cabinetTOthing(C, T)). cabinetTOthing(C, T) :- reuse(cabinetTOthing(C, T)).
% deleted elements are not a part of the solution % deleted elements CANNOT be a part of the solution
:- room(R), delete(room(R)). :- room(R), remove(room(R)).
:- cabinet(C), delete(cabinet(C)). :- cabinet(C), remove(cabinet(C)).
:- personTOroom(P, R), delete(personTOroom(P, R)). :- personTOroom(P, R), remove(personTOroom(P, R)).
:- roomTOcabinet(R, C), delete(roomTOcabinet(R, C)). :- roomTOcabinet(R, C), remove(roomTOcabinet(R, C)).
:- cabinetTOthing(C, T), delete(cabinetTOthing(C, T)). :- cabinetTOthing(C, T), remove(cabinetTOthing(C, T)).
%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% costs % cost defaults
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% creating cabinets and rooms cabinetTOthingCost(0).
cost(create(room(R)), W) :- room(R), roomCost(W), roomDomainNew(R). finalcabinetTOthingCost(W) :- cabinetTOthingCost(0), #max {X, 1 : cabinetTOthingCost(X)} = W.
cost(create(cabinetHigh(C)), W) :- cabinetHigh(C), cabinetHighCost(W), cabinetDomainNew(C). roomTOcabinetCost(0).
cost(create(cabinetSmall(C)), W) :- cabinetSmall(C), cabinetSmallCost(W), cabinetDomainNew(C). finalroomTOcabinetCost(W) :- roomTOcabinetCost(0), #max {X, 1 : roomTOcabinetCost(X)} = W.
personTOroomCost(0).
finalpersonTOroomCost(W) :- personTOroomCost(0), #max {X, 1 : personTOroomCost(X)} = W.
cabinetHighCost(0).
finalcabinetHighCost(W) :- cabinetHighCost(0), #max {X, 1 : cabinetHighCost(X)} = W.
cabinetSmallCost(0).
finalcabinetSmallCost(W) :- cabinetSmallCost(0), #max {X, 1 : cabinetSmallCost(X)} = W.
roomCost(0).
finalroomCost(W) :- roomCost(0), #max {X, 1 : roomCost(X)} = W.
reuseCabinetTOthingCost(0).
finalreuseCabinetTOthingCost(W) :- reuseCabinetTOthingCost(0), #max {X, 1 : reuseCabinetTOthingCost(X)} = W.
reuseRoomTOcabinetCost(0).
finalreuseRoomTOcabinetCost(W) :- reuseRoomTOcabinetCost(0), #max {X, 1 : reuseRoomTOcabinetCost(X)} = W.
reusePersonTOroomCost(0).
finalreusePersonTOroomCost(W) :- reusePersonTOroomCost(0), #max {X, 1 : reusePersonTOroomCost(X)} = W.
reuseCabinetAsSmallCost(0).
finalreuseCabinetAsSmallCost(W) :- reuseCabinetAsSmallCost(0), #max {X, 1 : reuseCabinetAsSmallCost(X)} = W.
reuseCabinetAsHighCost(0).
finalreuseCabinetAsHighCost(W) :- reuseCabinetAsHighCost(0), #max {X, 1 : reuseCabinetAsHighCost(X)} = W.
reuseRoomCost(0).
finalreuseRoomCost(W) :- reuseRoomCost(0), #max {X, 1 : reuseRoomCost(X)} = W.
removeCabinetTOthingCost(0).
finalremoveCabinetTOthingCost(W) :- removeCabinetTOthingCost(0), #max {X, 1 : removeCabinetTOthingCost(X)} = W.
removeRoomTOcabinetCost(0).
finalremoveRoomTOcabinetCost(W) :- removeRoomTOcabinetCost(0), #max {X, 1 : removeRoomTOcabinetCost(X)} = W.
removePersonTOroomCost(0).
finalremovePersonTOroomCost(W) :- removePersonTOroomCost(0), #max {X, 1 : removePersonTOroomCost(X)} = W.
removeCabinetCost(0).
finalremoveCabinetCost(W) :- removeCabinetCost(0), #max {X, 1 : removeCabinetCost(X)} = W.
removeRoomCost(0).
finalremoveRoomCost(W) :- removeRoomCost(0), #max {X, 1 : removeRoomCost(X)} = W.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% cost assignment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% reusing % reusing
cost(reuse(room(R)), W) :- reuse(room(R)), reuseRoomCost(W). cost(reuse(cabinetTOthing(C, T)), W) :- cabinetTOthing(C, T), legacyConfig(cabinetTOthing(C, T)), finalreuseCabinetTOthingCost(W).
cost(reuse(cabinetHigh(C)), W) :- reuse(cabinetHigh(C)), reuseCabinetAsHighCost(W). cost(reuse(roomTOcabinet(R, C)), W) :- roomTOcabinet(R, C), legacyConfig(roomTOcabinet(R, C)), finalreuseRoomTOcabinetCost(W).
cost(reuse(cabinetSmall(C)), W) :- reuse(cabinetSmall(C)), reuseCabinetAsSmallCost(W). cost(reuse(personTOroom(P, R)), W) :- personTOroom(P, R), legacyConfig(personTOroom(P, R)), finalreusePersonTOroomCost(W).
cost(reuse(personTOroom(P, R)), W) :- reuse(personTOroom(P, R)), reusePersonTOroomCost(W). cost(reuse(cabinetHigh(C)), W) :- cabinetHigh(C), legacyConfig(cabinet(C)), finalreuseCabinetAsHighCost(W).
cost(reuse(roomTOcabinet(R, C)), W) :- reuse(roomTOcabinet(R, C)), reuseRoomTOcabinetCost(W). cost(reuse(cabinetSmall(C)), W) :- cabinetSmall(C), legacyConfig(cabinet(C)), finalreuseCabinetAsSmallCost(W).
cost(reuse(cabinetTOthing(C, T)), W) :- reuse(cabinetTOthing(C, T)), reuseCabinetTOthingCost(W). cost(reuse(room(R)), W) :- room(R), legacyConfig(room(R)), finalreuseRoomCost(W).
% deleting % removing
cost(delete(room(R)), W) :- delete(room(R)), removeRoomCost(W). cost(remove(cabinetTOthing(C, T)), W) :- not cabinetTOthing(C, T), legacyConfig(cabinetTOthing(C, T)), finalremoveCabinetTOthingCost(W).
cost(delete(cabinet(C)), W) :- delete(cabinet(C)), removeCabinetCost(W). cost(remove(roomTOcabinet(R, C)), W) :- not roomTOcabinet(R, C), legacyConfig(roomTOcabinet(R, C)), finalremoveRoomTOcabinetCost(W).
cost(delete(personTOroom(P, R)), W) :- delete(personTOroom(P, R)), removePersonTOroomCost(W). cost(remove(personTOroom(P, R)), W) :- not personTOroom(P, R), legacyConfig(personTOroom(P, R)), finalremovePersonTOroomCost(W).
cost(delete(roomTOcabinet(R, C)), W) :- delete(roomTOcabinet(R, C)), removeRoomTOcabinetCost(W). cost(remove(cabinet(C)), W) :- not cabinet(C), legacyConfig(cabinet(C)), finalremoveCabinetCost(W).
cost(delete(cabinetTOthing(C, T)), W) :- delete(cabinetTOthing(C, T)), removeCabinetTOthingCost(W). cost(remove(room(R)), W) :- not room(R), legacyConfig(room(R)), finalremoveRoomCost(W).
% creating
cost(create(cabinetHigh(C)), W) :- cabinet(C), not legacyConfig(cabinet(C)), cabinetHigh(C), finalcabinetHighCost(W).
cost(create(cabinetSmall(C)), W) :- cabinet(C), not legacyConfig(cabinet(C)), cabinetSmall(C), finalcabinetSmallCost(W).
cost(create(room(R)), W) :- room(R), not legacyConfig(room(R)), finalroomCost(W).
cost(create(personTOroom(P, R)), W) :- personTOroom(P, R), not legacyConfig(personTOroom(P, R)), finalpersonTOroomCost(W).
cost(create(roomTOcabinet(R, C)), W) :- roomTOcabinet(R, C), not legacyConfig(roomTOcabinet(R, C)), finalroomTOcabinetCost(W).
cost(create(cabinetTOthing(C, T)), W) :- cabinetTOthing(C, T), not legacyConfig(cabinetTOthing(C, T)), finalcabinetTOthingCost(W).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% optimization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OPTIMIZATION
#minimize { W, X : cost(X, W) }. #minimize { W, X : cost(X, W) }.
% OUTPUT PREDICATES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#show cabinet/1.
#show cabinetHigh/1. #show cabinetHigh/1.
#show cabinetSmall/1. #show cabinetSmall/1.
#show room/1. #show room/1.
#show cabinetTOthing/2. #show cabinetTOthing/2.
#show roomTOcabinet/2. #show roomTOcabinet/2.
#show personTOroom/2. #show personTOroom/2.
% #show cost/2.

13
test.sh Normal file
View File

@ -0,0 +1,13 @@
#!/bin/bash
proj_dir='.'
input_dir="$proj_dir/simpleInstances"
output_dir="$proj_dir/output"
rm -r $output_dir
mkdir -p $output_dir
for f in $(ls $input_dir); do
clingo "$proj_dir/hrp_soln.asp" "$input_dir/$f" -t 10 | tee >(fold -w 80 > "$output_dir/${f}_soln.txt") 2>&1
done