diff --git a/Appendix.pdf b/Appendix.pdf new file mode 100644 index 0000000..8c5e54a Binary files /dev/null and b/Appendix.pdf differ diff --git a/Milestone - 2.docx b/Milestone - 2.docx new file mode 100644 index 0000000..8db68b0 Binary files /dev/null and b/Milestone - 2.docx differ diff --git a/Milestone - 2.pdf b/Milestone - 2.pdf new file mode 100644 index 0000000..a4bd0ab Binary files /dev/null and b/Milestone - 2.pdf differ diff --git a/hrp_soln.asp b/hrp_soln.asp index cb63e91..ae9bba8 100644 --- a/hrp_soln.asp +++ b/hrp_soln.asp @@ -2,55 +2,59 @@ %%%%%%%%% House Reconfiguration Problem %%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%% -% given element domains +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% domain sets +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -person(P) :- legacyConfig(person(P)). - -% HCP - only short +% legacy - only short things thing(T) :- legacyConfig(thing(T)). -% HRP - short and long +% new - short and long thing(T) :- thingLong(T). thing(T) :- thingShort(T). -thingShort(T) :- legacyConfig(thing(T)), not thingLong(T). -% a thing can't be long AND short +% a thing can't be both +thingShort(T) :- thing(T), not thingLong(T). :- thingLong(T), thingShort(T). +% all possible cabinets cabinetDomain(C) :- legacyConfig(cabinet(C)). cabinetDomain(C) :- cabinetDomainNew(C). +% all possible rooms roomDomain(R) :- legacyConfig(room(R)). roomDomain(R) :- roomDomainNew(R). -%%%%%%%%%%%%%%%%%%% -% mappings +person(P) :- legacyConfig(person(P)). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% solution mappings +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % each thing is owned by a single person personTOthing(P, T) :- legacyConfig(personTOthing(P, T)). % each thing is stored in a single cabinet 1 { cabinetTOthing(C, T) : cabinetDomain(C) } 1 :- thing(T). -cabinetTOthing(C, T) :- legacyConfig(cabinetTOthing(C, T)). % each cabinet is located in a single room 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 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) :- roomTOcabinet(R, C). - -% cabinet subtypes +% subtypes 1 { cabinetSmall(C); cabinetHigh(C) } 1 :- cabinet(C). % long things can only be stored in high cabinets -% :- thingLong(T), cabinetTOthing(C, T), cabinetSmall(C). cabinetHigh(C) :- thingLong(T), cabinetTOthing(C, T). +% room room(R) :- roomTOcabinet(R, C). room(R) :- personTOroom(P, R). @@ -58,8 +62,9 @@ room(R) :- personTOroom(P, R). cabinet(C1) :- cabinetDomainNew(C1), cabinetDomainNew(C2), cabinet(C2), C1 < C2. room(R1) :- roomDomainNew(R1), roomDomainNew(R2), room(R2), R1 < R2. -%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % constraints +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % no shared ownership of things or rooms :- 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). :- #sum { S, C : roomSlotUsage(R, C, S) } > 4, room(R). -%%%%%%%%%%%%%%%%%%% -% reconfiguration +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% reconfiguration +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% legacy -> solution === reuse or delete elements -1 { reuse(room(R)); delete(room(R)) } 1 :- legacyConfig(room(R)). -1 { reuse(cabinetHigh(C)); reuse(cabinetSmall(C)); delete(cabinet(C)) } 1 :- legacyConfig(cabinet(C)). -1 { reuse(personTOroom(P, R)); delete(personTOroom(P, R)) } 1 :- legacyConfig(personTOroom(P, R)). -1 { reuse(roomTOcabinet(R, C)); delete(roomTOcabinet(R, C)) } 1 :- legacyConfig(roomTOcabinet(R, C)). -1 { reuse(cabinetTOthing(C, T)); delete(cabinetTOthing(C, T)) } 1 :- legacyConfig(cabinetTOthing(C, T)). +% legacy -> solution === either reuse or remove elements +1 { reuse(room(R)); remove(room(R)) } 1 :- legacyConfig(room(R)). +1 { reuse(cabinet(C)); remove(cabinet(C)) } 1 :- legacyConfig(cabinet(C)). +1 { reuse(personTOroom(P, R)); remove(personTOroom(P, R)) } 1 :- legacyConfig(personTOroom(P, R)). +1 { reuse(roomTOcabinet(R, C)); remove(roomTOcabinet(R, C)) } 1 :- legacyConfig(roomTOcabinet(R, C)). +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)). -cabinetHigh(C) :- reuse(cabinetHigh(C)). -cabinetSmall(C) :- reuse(cabinetSmall(C)). +cabinet(C) :- reuse(cabinet(C)). personTOroom(P, R) :- reuse(personTOroom(P, R)). roomTOcabinet(R, C) :- reuse(roomTOcabinet(R, C)). cabinetTOthing(C, T) :- reuse(cabinetTOthing(C, T)). -% deleted elements are not a part of the solution -:- room(R), delete(room(R)). -:- cabinet(C), delete(cabinet(C)). -:- personTOroom(P, R), delete(personTOroom(P, R)). -:- roomTOcabinet(R, C), delete(roomTOcabinet(R, C)). -:- cabinetTOthing(C, T), delete(cabinetTOthing(C, T)). +% deleted elements CANNOT be a part of the solution +:- room(R), remove(room(R)). +:- cabinet(C), remove(cabinet(C)). +:- personTOroom(P, R), remove(personTOroom(P, R)). +:- roomTOcabinet(R, C), remove(roomTOcabinet(R, C)). +:- cabinetTOthing(C, T), remove(cabinetTOthing(C, T)). -%%%%%%%%%%%%%%%%%%% -% costs +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% cost defaults +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% creating cabinets and rooms -cost(create(room(R)), W) :- room(R), roomCost(W), roomDomainNew(R). -cost(create(cabinetHigh(C)), W) :- cabinetHigh(C), cabinetHighCost(W), cabinetDomainNew(C). -cost(create(cabinetSmall(C)), W) :- cabinetSmall(C), cabinetSmallCost(W), cabinetDomainNew(C). +cabinetTOthingCost(0). +finalcabinetTOthingCost(W) :- cabinetTOthingCost(0), #max {X, 1 : cabinetTOthingCost(X)} = W. +roomTOcabinetCost(0). +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 -cost(reuse(room(R)), W) :- reuse(room(R)), reuseRoomCost(W). -cost(reuse(cabinetHigh(C)), W) :- reuse(cabinetHigh(C)), reuseCabinetAsHighCost(W). -cost(reuse(cabinetSmall(C)), W) :- reuse(cabinetSmall(C)), reuseCabinetAsSmallCost(W). -cost(reuse(personTOroom(P, R)), W) :- reuse(personTOroom(P, R)), reusePersonTOroomCost(W). -cost(reuse(roomTOcabinet(R, C)), W) :- reuse(roomTOcabinet(R, C)), reuseRoomTOcabinetCost(W). -cost(reuse(cabinetTOthing(C, T)), W) :- reuse(cabinetTOthing(C, T)), reuseCabinetTOthingCost(W). +cost(reuse(cabinetTOthing(C, T)), W) :- cabinetTOthing(C, T), legacyConfig(cabinetTOthing(C, T)), finalreuseCabinetTOthingCost(W). +cost(reuse(roomTOcabinet(R, C)), W) :- roomTOcabinet(R, C), legacyConfig(roomTOcabinet(R, C)), finalreuseRoomTOcabinetCost(W). +cost(reuse(personTOroom(P, R)), W) :- personTOroom(P, R), legacyConfig(personTOroom(P, R)), finalreusePersonTOroomCost(W). +cost(reuse(cabinetHigh(C)), W) :- cabinetHigh(C), legacyConfig(cabinet(C)), finalreuseCabinetAsHighCost(W). +cost(reuse(cabinetSmall(C)), W) :- cabinetSmall(C), legacyConfig(cabinet(C)), finalreuseCabinetAsSmallCost(W). +cost(reuse(room(R)), W) :- room(R), legacyConfig(room(R)), finalreuseRoomCost(W). -% deleting -cost(delete(room(R)), W) :- delete(room(R)), removeRoomCost(W). -cost(delete(cabinet(C)), W) :- delete(cabinet(C)), removeCabinetCost(W). -cost(delete(personTOroom(P, R)), W) :- delete(personTOroom(P, R)), removePersonTOroomCost(W). -cost(delete(roomTOcabinet(R, C)), W) :- delete(roomTOcabinet(R, C)), removeRoomTOcabinetCost(W). -cost(delete(cabinetTOthing(C, T)), W) :- delete(cabinetTOthing(C, T)), removeCabinetTOthingCost(W). +% removing +cost(remove(cabinetTOthing(C, T)), W) :- not cabinetTOthing(C, T), legacyConfig(cabinetTOthing(C, T)), finalremoveCabinetTOthingCost(W). +cost(remove(roomTOcabinet(R, C)), W) :- not roomTOcabinet(R, C), legacyConfig(roomTOcabinet(R, C)), finalremoveRoomTOcabinetCost(W). +cost(remove(personTOroom(P, R)), W) :- not personTOroom(P, R), legacyConfig(personTOroom(P, R)), finalremovePersonTOroomCost(W). +cost(remove(cabinet(C)), W) :- not cabinet(C), legacyConfig(cabinet(C)), finalremoveCabinetCost(W). +cost(remove(room(R)), W) :- not room(R), legacyConfig(room(R)), finalremoveRoomCost(W). -% OPTIMIZATION -#minimize { W, X: cost(X, 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). -% OUTPUT PREDICATES +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% optimization +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +#minimize { W, X : cost(X, W) }. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% output +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +#show cabinet/1. #show cabinetHigh/1. #show cabinetSmall/1. #show room/1. #show cabinetTOthing/2. #show roomTOcabinet/2. -#show personTOroom/2. \ No newline at end of file +#show personTOroom/2. +% #show cost/2. diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..64a92c1 --- /dev/null +++ b/test.sh @@ -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