From 1b81737ce7d41476ebab5a737d3e8f739b443ad2 Mon Sep 17 00:00:00 2001 From: sp Date: Fri, 5 Jan 2024 19:38:07 +0100 Subject: [PATCH 01/38] add yaml-cpp as dependency --- CMakeLists.txt | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a7c5c1..0392819 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,24 +1,26 @@ -include(util/CMakeLists.txt) - -set(CMAKE_CXX_STANDARD 20) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'") -add_definitions(-DLOG_DEBUG) - cmake_minimum_required(VERSION 3.0...3.22) - -set(CMAKE_BUILD_TYPE Debug) - project( Minigrid2PRISM VERSION 0.1 LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 20) -find_package(yaml-cpp) - -add_executable(main - ${SRCS} - main.cpp - ) - -target_link_libraries(main pthread yaml-cpp) +include(util/CMakeLists.txt) +include(FetchContent) + +FetchContent_Declare( + yaml-cpp + GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git + GIT_TAG master + OVERRIDE_FIND_PACKAGE +) +FetchContent_GetProperties(yaml-cpp) +if(NOT yaml-cpp_POPULATED) + message(STATUS "Fetching yaml-cpp...") + FetchContent_Populate(yaml-cpp) + add_subdirectory(${yaml-cpp_SOURCE_DIR} ${yaml-cpp_BINARY_DIR}) +endif() +FetchContent_MakeAvailable(yaml-cpp) + +add_executable(main ${SRCS} main.cpp) +target_link_libraries(main pthread yaml-cpp::yaml-cpp) From 9f03a14347d8322d736ef4f350f39da9e4058ced Mon Sep 17 00:00:00 2001 From: sp Date: Fri, 5 Jan 2024 21:12:49 +0100 Subject: [PATCH 02/38] init PrismFormulaPrinter first attempt to enforce DRY for formulas in PrismModulesPrinter --- util/PrismFormulaPrinter.cpp | 46 ++++++++++++++++++++++++++++++++++++ util/PrismFormulaPrinter.h | 21 ++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 util/PrismFormulaPrinter.cpp create mode 100644 util/PrismFormulaPrinter.h diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp new file mode 100644 index 0000000..a54233d --- /dev/null +++ b/util/PrismFormulaPrinter.cpp @@ -0,0 +1,46 @@ +#include "PrismFormulaPrinter.h" + +#include +#include + + +std::string cellToConjunction(const AgentName &agentName, const cell &c) { + return "x" + agentName + "=" + std::to_string(c.column) + "&y" + agentName + "=" + std::to_string(c.row); +} + +namespace prism { + PrismFormulaPrinter::PrismFormulaPrinter() {} + + std::ostream& PrismFormulaPrinter::printRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells) { + os << buildFormula(agentName + "CannotMove" + direction, buildDisjunction(agentName, grid_cells)); + return os; + } + std::ostream& PrismFormulaPrinter::printRestrictionFormulaWithCondition(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells, const std::vector conditions) { + os << buildFormula(agentName + "Something", buildDisjunction(agentName, grid_cells, conditions)); + return os; + } + + std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula) { + return "formula " + formulaName + " = " + formula + ";\n"; + } + + std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions) { + bool first = true; + std::string disjunction = ""; + if(!conditions.empty()) { + for(uint index = 0; index < cells.size(); index++) { + if(first) first = false; + else disjunction += " | "; + disjunction += "(" + cellToConjunction(agentName, cells.at(index)) + "&" + conditions.at(index) + ")"; + } + + } else { + for(auto const cell : cells) { + if(first) first = false; + else disjunction += " | "; + disjunction += "(" + cellToConjunction(agentName, cell) + ")"; + } + } + return disjunction; + } +} diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h new file mode 100644 index 0000000..a9b5df4 --- /dev/null +++ b/util/PrismFormulaPrinter.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include "MinigridGrammar.h" +#include "PrismPrinter.h" +#include "ConfigYaml.h" + +namespace prism { + class PrismFormulaPrinter { + public: + PrismFormulaPrinter(); + + std::ostream& printRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells); + std::ostream& printRestrictionFormulaWithCondition(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells, const std::vector conditions); + private: + std::string buildFormula(const std::string &formulaName, const std::string &formula); + std::string buildLabel(const std::string &labelName, const std::string &label); + std::string buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions = {}); + }; +} From 7f8e195412ee988a2b2109f52ed1197b0e91e9f8 Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 10:55:34 +0100 Subject: [PATCH 03/38] added methods for formulas with conditions --- util/PrismFormulaPrinter.cpp | 70 ++++++++++++++++++++++++++++++++---- util/PrismFormulaPrinter.h | 24 +++++++++++-- util/PrismPrinter.h | 2 ++ 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index a54233d..0ae965b 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -2,28 +2,84 @@ #include #include +#include +std::string capitalize(std::string string) { + string[0] = std::toupper(string[0]); + return string; +} std::string cellToConjunction(const AgentName &agentName, const cell &c) { return "x" + agentName + "=" + std::to_string(c.column) + "&y" + agentName + "=" + std::to_string(c.row); } +std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection) { + return "x" + agentName + "=" + std::to_string(c.first) + "&y" + agentName + "=" + std::to_string(c.second) + "&view" + agentName + "=" + std::to_string(viewDirection); +} + +std::map getSurroundingCells(const cell &c) { + return {{1, c.getNorth()}, {2, c.getEast()}, {3, c.getSouth()}, {0, c.getWest()}}; +} + namespace prism { - PrismFormulaPrinter::PrismFormulaPrinter() {} + PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys) + : os(os), agentName(agentName), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys) + { } + + void PrismFormulaPrinter::printFormulas() { + for(const auto& [direction, cells] : restrictions) { + printRestrictionFormula(direction, cells); + } + + for(const auto& ball : balls) { + std::string color = capitalize(ball.getColor()); + printRestrictionFormulaWithCondition(color + "Ball", getSurroundingCells(ball), "!" + color + "BallPickedUp"); + } + + for(const auto& box : boxes) { + std::string color = capitalize(box.getColor()); + printRestrictionFormulaWithCondition(color + "Box", getSurroundingCells(box), "!" + color + "BoxPickedUp"); + } + + for(const auto& key : keys) { + std::string color = capitalize(key.getColor()); + printRestrictionFormulaWithCondition(color + "Key", getSurroundingCells(key), "!" + color + "KeyPickedUp"); + } - std::ostream& PrismFormulaPrinter::printRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells) { - os << buildFormula(agentName + "CannotMove" + direction, buildDisjunction(agentName, grid_cells)); - return os; + for(const auto& door : unlockedDoors) { + std::string color = capitalize(door.getColor()); + printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened"); + } + + for(const auto& door : lockedDoors) { + std::string color = capitalize(door.getColor()); + printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened"); + } } - std::ostream& PrismFormulaPrinter::printRestrictionFormulaWithCondition(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells, const std::vector conditions) { - os << buildFormula(agentName + "Something", buildDisjunction(agentName, grid_cells, conditions)); - return os; + + void PrismFormulaPrinter::printRestrictionFormula(const std::string &direction, const cells &grid_cells) { + os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells)); + } + + void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const std::string &reason, const std::map &coordinates, const std::string &condition) { + os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition); } std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula) { return "formula " + formulaName + " = " + formula + ";\n"; } + std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map &cells) { + bool first = true; + std::string disjunction = ""; + for(const auto [viewDirection, coordinates] : cells) { + if(first) first = false; + else disjunction += " | "; + disjunction += "(" + coordinatesToConjunction(agentName, coordinates, viewDirection) + ")"; + } + return disjunction; + } + std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions) { bool first = true; std::string disjunction = ""; diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index a9b5df4..73ecc19 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -6,16 +6,34 @@ #include "PrismPrinter.h" #include "ConfigYaml.h" + +std::string cellToConjunction(const AgentName &agentName, const cell &c); +std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection); +std::map getSurroundingCells(const cell &c); + namespace prism { class PrismFormulaPrinter { public: - PrismFormulaPrinter(); + PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys); + + void printFormulas(); - std::ostream& printRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells); - std::ostream& printRestrictionFormulaWithCondition(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells, const std::vector conditions); + void printRestrictionFormula(const std::string &direction, const cells &grid_cells); + void printRestrictionFormulaWithCondition(const std::string &reason, const std::map &coordinates, const std::string &condition); private: std::string buildFormula(const std::string &formulaName, const std::string &formula); std::string buildLabel(const std::string &labelName, const std::string &label); + std::string buildDisjunction(const AgentName &agentName, const std::map &cells); std::string buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions = {}); + + std::ostream &os; + AgentName agentName; + std::map restrictions; + cells boxes; + cells balls; + cells lockedDoors; + cells unlockedDoors; + cells keys; + }; } diff --git a/util/PrismPrinter.h b/util/PrismPrinter.h index 55f4ada..a120803 100644 --- a/util/PrismPrinter.h +++ b/util/PrismPrinter.h @@ -5,10 +5,12 @@ #include "cell.h" typedef std::string AgentName; +typedef size_t ViewDirection; typedef std::pair AgentNameAndPosition; typedef AgentNameAndPosition KeyNameAndPosition; typedef std::map AgentNameAndPositionMap; typedef std::map KeyNameAndPositionMap; +typedef std::pair CellAndCondition; namespace prism { enum class ModelType { From 9ea5c0194d9195a377d370c08e5a158659c51d72 Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 12:02:02 +0100 Subject: [PATCH 04/38] add slip and lava formulas this also adds a disjunction over all conditional checks --- util/PrismFormulaPrinter.cpp | 31 +++++++++++++++++++++++++++++-- util/PrismFormulaPrinter.h | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index 0ae965b..74b7ff3 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -9,6 +9,17 @@ std::string capitalize(std::string string) { return string; } +std::string vectorToDisjunction(const std::vector &formulae) { + bool first = true; + std::string disjunction = ""; + for(const auto &formula : formulae) { + if(first) first = false; + else disjunction += " | "; + disjunction += formula; + } + return disjunction; +} + std::string cellToConjunction(const AgentName &agentName, const cell &c) { return "x" + agentName + "=" + std::to_string(c.column) + "&y" + agentName + "=" + std::to_string(c.row); } @@ -22,8 +33,8 @@ std::map getSurroundingCells(const cell &c) { } namespace prism { - PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys) - : os(os), agentName(agentName), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys) + PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava) + : os(os), agentName(agentName), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava) { } void PrismFormulaPrinter::printFormulas() { @@ -31,6 +42,11 @@ namespace prism { printRestrictionFormula(direction, cells); } + for(const auto& [direction, cells] : slipperyTiles) { + printIsOnFormula("Slippery", cells, direction); + } + printIsOnFormula("Lava", lava); + for(const auto& ball : balls) { std::string color = capitalize(ball.getColor()); printRestrictionFormulaWithCondition(color + "Ball", getSurroundingCells(ball), "!" + color + "BallPickedUp"); @@ -55,14 +71,23 @@ namespace prism { std::string color = capitalize(door.getColor()); printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened"); } + + if(conditionalMovementRestrictions.size() > 0) { + os << buildFormula("CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions)); + } } void PrismFormulaPrinter::printRestrictionFormula(const std::string &direction, const cells &grid_cells) { os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells)); } + void PrismFormulaPrinter::printIsOnFormula(const std::string &type, const cells &grid_cells, const std::string &direction) { + os << buildFormula(agentName + "IsOn" + type + direction, buildDisjunction(agentName, grid_cells)); + } + void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const std::string &reason, const std::map &coordinates, const std::string &condition) { os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition); + conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason); } std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula) { @@ -70,6 +95,7 @@ namespace prism { } std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map &cells) { + if(cells.size() == 0) return "false"; bool first = true; std::string disjunction = ""; for(const auto [viewDirection, coordinates] : cells) { @@ -81,6 +107,7 @@ namespace prism { } std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions) { + if(cells.size() == 0) return "false"; bool first = true; std::string disjunction = ""; if(!conditions.empty()) { diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index 73ecc19..ac14cb4 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -7,6 +7,8 @@ #include "ConfigYaml.h" +std::string capitalize(std::string string); +std::string vectorToDisjunction(const std::vector &formulae); std::string cellToConjunction(const AgentName &agentName, const cell &c); std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection); std::map getSurroundingCells(const cell &c); From b0b9dd61dc750919b681894eccfe2a6c8c1c9439 Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 12:11:01 +0100 Subject: [PATCH 05/38] add agent and adv to floor cells --- util/Grid.cpp | 60 ++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 42 deletions(-) diff --git a/util/Grid.cpp b/util/Grid.cpp index d14a3a0..cdd07eb 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -17,48 +17,23 @@ Grid::Grid(cells gridCells, cells background, const GridOptions &gridOptions, co { cell max = allGridCells.at(allGridCells.size() - 1); maxBoundaries = std::make_pair(max.row - 1, max.column - 1); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(walls), [](cell c) { - return c.type == Type::Wall; - }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lava), [](cell c) { - return c.type == Type::Lava; - }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(floor), [](cell c) { - return c.type == Type::Floor; - }); - std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorth), [](cell c) { - return c.type == Type::SlipperyNorth; - }); - std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyEast), [](cell c) { - return c.type == Type::SlipperyEast; - }); - std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouth), [](cell c) { - return c.type == Type::SlipperySouth; - }); - std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyWest), [](cell c) { - return c.type == Type::SlipperyWest; - }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lockedDoors), [](cell c) { - return c.type == Type::LockedDoor; - }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(unlockedDoors), [](cell c) { - return c.type == Type::Door; - }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(goals), [](cell c) { - return c.type == Type::Goal; - }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(keys), [this](cell c) { - return c.type == Type::Key; - }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(boxes), [](cell c) { - return c.type == Type::Box; - }); - agent = *std::find_if(gridCells.begin(), gridCells.end(), [](cell c) { - return c.type == Type::Agent; - }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(adversaries), [](cell c) { - return c.type == Type::Adversary; - }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(walls), [](cell c) { return c.type == Type::Wall; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lava), [](cell c) { return c.type == Type::Lava; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(floor), [](cell c) { return c.type == Type::Floor; }); // TODO Add agent cells to floor + std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorth), [](cell c) { return c.type == Type::SlipperyNorth; }); + std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyEast), [](cell c) { return c.type == Type::SlipperyEast; }); + std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouth), [](cell c) { return c.type == Type::SlipperySouth; }); + std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyWest), [](cell c) { return c.type == Type::SlipperyWest; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lockedDoors), [](cell c) { return c.type == Type::LockedDoor; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(unlockedDoors), [](cell c) { return c.type == Type::Door; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(goals), [](cell c) { return c.type == Type::Goal; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(keys), [](cell c) { return c.type == Type::Key; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(boxes), [](cell c) { return c.type == Type::Box; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(balls), [](cell c) { return c.type == Type::Ball; }); + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(adversaries), [](cell c) { return c.type == Type::Adversary; }); + agent = *std::find_if(gridCells.begin(), gridCells.end(), [](cell c) { return c.type == Type::Agent; }); + floor.push_back(agent); + agentNameAndPositionMap.insert({ "Agent", agent.getCoordinates() }); for(auto const& adversary : adversaries) { std::string color = adversary.getColor(); @@ -66,6 +41,7 @@ Grid::Grid(cells gridCells, cells background, const GridOptions &gridOptions, co try { if(gridOptions.agentsToBeConsidered.size() != 0 && std::find(gridOptions.agentsToBeConsidered.begin(), gridOptions.agentsToBeConsidered.end(), color) == gridOptions.agentsToBeConsidered.end()) continue; auto success = agentNameAndPositionMap.insert({ color, adversary.getCoordinates() }); + floor.push_back(adversary); if(!success.second) { throw std::logic_error("Agent with " + color + " already present\n"); } From d07807de6125691dac01bc7f061beff7322ad374 Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 12:11:22 +0100 Subject: [PATCH 06/38] added PrismFormulaPrinter to CMakeLists --- util/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 877fde4..5d8bf56 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -3,6 +3,7 @@ list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/MinigridGrammar.h ${CMAKE_CURRENT_LIST_DIR}/Grid.cpp ${CMAKE_CURRENT_LIST_DIR}/PrismModulesPrinter.cpp + ${CMAKE_CURRENT_LIST_DIR}/PrismFormulaPrinter.cpp ${CMAKE_CURRENT_LIST_DIR}/popl.hpp ${CMAKE_CURRENT_LIST_DIR}/OptionParser.cpp ${CMAKE_CURRENT_LIST_DIR}/ConfigYaml.cpp From 1729ba7c566f530fb41abe66356aed7dbc9356ea Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 12:13:00 +0100 Subject: [PATCH 07/38] summary commit added cells for balls more formulas in PrismFormulaPrinter --- util/Grid.h | 2 ++ util/PrismFormulaPrinter.h | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/util/Grid.h b/util/Grid.h index 29e9e20..80f5055 100644 --- a/util/Grid.h +++ b/util/Grid.h @@ -7,6 +7,7 @@ #include "MinigridGrammar.h" #include "PrismModulesPrinter.h" +#include "PrismFormulaPrinter.h" #include "ConfigYaml.h" struct GridOptions { @@ -59,6 +60,7 @@ class Grid { cells lockedDoors; cells unlockedDoors; cells boxes; + cells balls; cells lava; cells goals; diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index ac14cb4..058849d 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -16,11 +16,12 @@ std::map getSurroundingCells(const cell &c); namespace prism { class PrismFormulaPrinter { public: - PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys); + PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava); void printFormulas(); void printRestrictionFormula(const std::string &direction, const cells &grid_cells); + void printIsOnFormula(const std::string &type, const cells &grid_cells, const std::string &direction = ""); void printRestrictionFormulaWithCondition(const std::string &reason, const std::map &coordinates, const std::string &condition); private: std::string buildFormula(const std::string &formulaName, const std::string &formula); @@ -36,6 +37,9 @@ namespace prism { cells lockedDoors; cells unlockedDoors; cells keys; + std::map slipperyTiles; + cells lava; + std::vector conditionalMovementRestrictions; }; } From 87661100682d0e51807ed86db9ff7a129f3dce0e Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 18:34:16 +0100 Subject: [PATCH 08/38] getType method for cell --- util/cell.cpp | 21 +++++++++++++++++++++ util/cell.h | 1 + 2 files changed, 22 insertions(+) diff --git a/util/cell.cpp b/util/cell.cpp index 07d25f7..df20997 100644 --- a/util/cell.cpp +++ b/util/cell.cpp @@ -69,6 +69,27 @@ std::string cell::getColor() const { } } +std::string cell::getType() const { + switch(type) { + case Type::Wall: return "Wall"; + case Type::Floor: return "Floor"; + case Type::Door: return "Door"; + case Type::LockedDoor: return "LockedDoor"; + case Type::Key: return "Key"; + case Type::Ball: return "Ball"; + case Type::Box: return "Box"; + case Type::Goal: return "Goal"; + case Type::Lava: return "Lava"; + case Type::Agent: return "Agent"; + case Type::Adversary: return "Adversary"; + case Type::SlipperyNorth: return "SlipperyNorth"; + case Type::SlipperySouth: return "SlipperySouth"; + case Type::SlipperyEast: return "SlipperyEast"; + case Type::SlipperyWest: return "SlipperyWest"; + default: return ""; + } +} + std::string getColor(Color color) { switch(color) { case Color::Red: return "red"; diff --git a/util/cell.h b/util/cell.h index c369052..d1d1169 100644 --- a/util/cell.h +++ b/util/cell.h @@ -54,6 +54,7 @@ class cell { coordinates getCoordinates() const; std::string getColor() const; + std::string getType() const; int row; int column; From 6f541c1bc2bf10dc112e16a39e402acda80f1224 Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 18:34:28 +0100 Subject: [PATCH 09/38] moved capitalize util to PrismPrinter --- util/CMakeLists.txt | 1 + util/PrismFormulaPrinter.cpp | 5 ----- util/PrismFormulaPrinter.h | 1 - util/PrismPrinter.cpp | 8 ++++++++ util/PrismPrinter.h | 3 +++ 5 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 util/PrismPrinter.cpp diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 5d8bf56..2325d92 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -2,6 +2,7 @@ list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/cell.cpp ${CMAKE_CURRENT_LIST_DIR}/MinigridGrammar.h ${CMAKE_CURRENT_LIST_DIR}/Grid.cpp + ${CMAKE_CURRENT_LIST_DIR}/PrismPrinter.cpp ${CMAKE_CURRENT_LIST_DIR}/PrismModulesPrinter.cpp ${CMAKE_CURRENT_LIST_DIR}/PrismFormulaPrinter.cpp ${CMAKE_CURRENT_LIST_DIR}/popl.hpp diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index 74b7ff3..0bcdd69 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -4,11 +4,6 @@ #include #include -std::string capitalize(std::string string) { - string[0] = std::toupper(string[0]); - return string; -} - std::string vectorToDisjunction(const std::vector &formulae) { bool first = true; std::string disjunction = ""; diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index 058849d..97f77f8 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -7,7 +7,6 @@ #include "ConfigYaml.h" -std::string capitalize(std::string string); std::string vectorToDisjunction(const std::vector &formulae); std::string cellToConjunction(const AgentName &agentName, const cell &c); std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection); diff --git a/util/PrismPrinter.cpp b/util/PrismPrinter.cpp new file mode 100644 index 0000000..4c172f1 --- /dev/null +++ b/util/PrismPrinter.cpp @@ -0,0 +1,8 @@ +#include "PrismPrinter.h" + +#include + +std::string capitalize(std::string string) { + string[0] = std::toupper(string[0]); + return string; +} diff --git a/util/PrismPrinter.h b/util/PrismPrinter.h index a120803..0d28af0 100644 --- a/util/PrismPrinter.h +++ b/util/PrismPrinter.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "cell.h" @@ -12,6 +13,8 @@ typedef std::map KeyNameAndPositionMap; typedef std::pair CellAndCondition; +std::string capitalize(std::string string); + namespace prism { enum class ModelType { MDP, SMG From 5955bb97914f39e16ad55f695f7519304f4b4e2f Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 18:35:41 +0100 Subject: [PATCH 10/38] isNextTo formulas for doors --- util/PrismFormulaPrinter.cpp | 18 ++++++++++++------ util/PrismFormulaPrinter.h | 5 +++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index 0bcdd69..8f333f2 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -32,7 +32,7 @@ namespace prism { : os(os), agentName(agentName), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava) { } - void PrismFormulaPrinter::printFormulas() { + void PrismFormulaPrinter::print() { for(const auto& [direction, cells] : restrictions) { printRestrictionFormula(direction, cells); } @@ -58,17 +58,19 @@ namespace prism { } for(const auto& door : unlockedDoors) { - std::string color = capitalize(door.getColor()); - printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened"); + std::string identifier = capitalize(door.getColor()) + door.getType(); + printRestrictionFormulaWithCondition(identifier, getSurroundingCells(door), "!" + identifier + "Open"); + printIsNextToFormula(identifier, getSurroundingCells(door)); } for(const auto& door : lockedDoors) { - std::string color = capitalize(door.getColor()); - printRestrictionFormulaWithCondition(color + "Door", getSurroundingCells(door), "!" + color + "DoorOpened"); + std::string identifier = capitalize(door.getColor()) + door.getType(); + printRestrictionFormulaWithCondition(identifier, getSurroundingCells(door), "!" + identifier + "Open"); + printIsNextToFormula(identifier, getSurroundingCells(door)); } if(conditionalMovementRestrictions.size() > 0) { - os << buildFormula("CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions)); + os << buildFormula(agentName + "CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions)); } } @@ -80,6 +82,10 @@ namespace prism { os << buildFormula(agentName + "IsOn" + type + direction, buildDisjunction(agentName, grid_cells)); } + void PrismFormulaPrinter::printIsNextToFormula(const std::string &type, const std::map &coordinates) { + os << buildFormula(agentName + "IsNextTo" + type, buildDisjunction(agentName, coordinates)); + } + void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const std::string &reason, const std::map &coordinates, const std::string &condition) { os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition); conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason); diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index 97f77f8..4795f3e 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -17,10 +17,11 @@ namespace prism { public: PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava); - void printFormulas(); + void print(); void printRestrictionFormula(const std::string &direction, const cells &grid_cells); void printIsOnFormula(const std::string &type, const cells &grid_cells, const std::string &direction = ""); + void printIsNextToFormula(const std::string &type, const std::map &coordinates); void printRestrictionFormulaWithCondition(const std::string &reason, const std::map &coordinates, const std::string &condition); private: std::string buildFormula(const std::string &formulaName, const std::string &formula); @@ -29,7 +30,7 @@ namespace prism { std::string buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions = {}); std::ostream &os; - AgentName agentName; + AgentName agentName; // move this to functions std::map restrictions; cells boxes; cells balls; From ca70595b58fdb3cbd8e5ccc98d9c9687d8d67c09 Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 18:35:59 +0100 Subject: [PATCH 11/38] WIP major rework of PrismModulesPrinter will probably get squashed, otw summary in later commit --- util/PrismModulesPrinter.cpp | 576 ++++++++--------------------------- util/PrismModulesPrinter.h | 72 ++--- 2 files changed, 161 insertions(+), 487 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 9b20dc0..1f26d89 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -5,11 +5,12 @@ namespace prism { - PrismModulesPrinter::PrismModulesPrinter(const ModelType &modelType, const size_t &numberOfPlayer, std::vector config, const bool enforceOneWays) - : modelType(modelType), numberOfPlayer(numberOfPlayer), enforceOneWays(enforceOneWays), configuration(config), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) { + PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const bool enforceOneWays) + : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), agentNameAndPositionMap(agentNameAndPositionMap), enforceOneWays(enforceOneWays), configuration(config), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) { + numberOfPlayer = agentNameAndPositionMap.size(); } - std::ostream& PrismModulesPrinter::printModel(std::ostream &os, const ModelType &modelType) { + std::ostream& PrismModulesPrinter::printModelType(const ModelType &modelType) { switch(modelType) { case(ModelType::MDP): os << "mdp"; @@ -22,394 +23,52 @@ namespace prism { return os; } - std::ostream& PrismModulesPrinter::printBackgroundLabels(std::ostream &os, const AgentName &agentName, const std::pair &backgroundTiles) { - if(backgroundTiles.second.size() == 0) return os; - - bool first = true; - std::string color = getColor(backgroundTiles.first); - color.at(0) = std::toupper(color.at(0)); - os << "formula " << agentName << "On" << color << " = "; - for(auto const& cell : backgroundTiles.second) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - os << "label \"" << agentName << "On" << color << "\" = " << agentName << "On" << color << ";\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells, const cells &keys, const cells &doors) { - bool first = true; - os << "formula " << agentName << "CannotMove" << direction << " = " ; - for(auto const& cell : grid_cells) { - if(first) first = false; - else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - - if (!keys.empty()) { - os << " | " << agentName << "CannotMove" << direction << "BecauseOfKey"; - } - if (!doors.empty()) { - os << " | " << agentName << "CannotMove" << direction << "BecauseOfDoor"; - } - - os << ";\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printKeyRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &keys) { - bool first = true; - os << "formula " << agentName << "CannotMove" << direction << "BecauseOfKey" << " = "; - for (auto const& key : keys) { - if(first) first = false; - else os << " | "; - std::string keyColor = key.getColor(); - std::string xKey = "xKey" + keyColor; - std::string yKey = "yKey" + keyColor; - coordinates coords; - - os << "(!" << agentName << "_has_" << keyColor << "_key & "; - - if (direction == "North") { - os << " x" << agentName << " = " << xKey << "&y" << agentName << "-1 = " << yKey << ")"; - } else if (direction == "South") { - os << " x" << agentName << " = " << xKey << "&y" << agentName << "+1 = " << yKey << ")"; - } else if (direction == "East") { - os << " x" << agentName << "+1 = " << xKey << "&y" << agentName << " = " << yKey << ")"; - } else if (direction == "West") { - os << " x" << agentName << "-1 = " << xKey << "&y" << agentName << " = " << yKey << ")"; - } else { - os << "Invalid Direction! in Key Restriction"; - } - } - - os << ";\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printDoorRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &doors) { - bool first = true; - - os << "formula " << agentName << "CannotMove" << direction << "BecauseOfDoor" << " = "; - - for (auto const& door : doors) { - if (first) first = false; - else os << " | "; - - std::string doorColor = door.getColor(); - size_t y = door.getCoordinates().first; - size_t x = door.getCoordinates().second; - - os << "(!Door" << doorColor << "open & "; - - if (direction == "North") { - os << " x" << agentName << " = " << x << "&y" << agentName << "-1 = " << y << ")"; - } else if (direction == "South") { - os << " x" << agentName << " = " << x << "&y" << agentName << "+1 = " << y << ")"; - } else if (direction == "East") { - os << " x" << agentName << "+1 = " << x << "&y" << agentName << " = " << y << ")"; - } else if (direction == "West") { - os << " x" << agentName << "-1 = " << y << "&y" << agentName << " = " << y << ")"; - } else { - os << "Invalid Direction! in Key Restriction"; - } - - } - - os << ";\n"; - return os; - } - - - std::ostream& PrismModulesPrinter::printIsOnSlipperyFormula(std::ostream& os, const AgentName &agentName, const std::vector> &slipperyCollection, const cells &slipperyNorth, const cells &slipperyEast, const cells &slipperySouth, const cells &slipperyWest) { - if(std::find_if(slipperyCollection.cbegin(), slipperyCollection.cend(), [=](const std::reference_wrapper& c) { return !c.get().empty(); }) == slipperyCollection.cend()) { - os << "formula " << agentName << "IsOnSlippery = false;\n"; - return os; - } - - bool first = true; - if (!slipperyNorth.empty()) { - os << "formula " << agentName << "IsOnSlipperyNorth = "; - for (const auto& cell : slipperyNorth) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - } - - if (!slipperyEast.empty()) { - first = true; - os << "formula " << agentName << "IsOnSlipperyEast = "; - for (const auto& cell : slipperyEast) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - + std::ostream& PrismModulesPrinter::print() { + for(const auto &key : keys) { + printPortableObjectModule(key); } - - if (!slipperySouth.empty()) { - first = true; - os << "formula " << agentName << "IsOnSlipperySouth = "; - for (const auto& cell : slipperySouth) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - } - - if (!slipperyWest.empty()) { - first = true; - os << "formula " << agentName << "IsOnSlipperyWest = "; - for (const auto& cell : slipperyWest) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - } - - first = true; - os << "formula " << agentName << "IsOnSlippery = "; - - for (const auto& slippery: slipperyCollection) { - for(const auto& cell : slippery.get()) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - } - os << ";\n"; - if(enforceOneWays) { - first = true; - os << "formula " << agentName << "IsOnSlipperyNorth = "; - - for (const auto& cell: slipperyNorth) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - first = true; - os << "formula " << agentName << "IsOnSlipperyEast = "; - - for (const auto& cell: slipperyEast) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - first = true; - os << "formula " << agentName << "IsOnSlipperySouth = "; - - for (const auto& cell: slipperySouth) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - first = true; - os << "formula " << agentName << "IsOnSlipperyWest = "; - for (const auto& cell: slipperyWest) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - } - return os; - } - - std::ostream& PrismModulesPrinter::printIsInLavaFormula(std::ostream& os, const AgentName &agentName, const cells &lava) { - if(lava.size() == 0) { - os << "formula " << agentName << "IsInLava = false;\n"; - return os; - } - bool first = true; - os << "formula " << agentName << "IsInLava = "; - for(auto const& cell : lava) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << ";\n"; - os << "formula " << agentName << "IsInLavaAndNotDone = " << agentName << "IsInLava & !" << agentName << "Done;\n"; - os << "label \"" << agentName << "IsInLavaAndNotDone\" = " << agentName << "IsInLava & !" << agentName << "Done;\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printTurningNotAllowedFormulas(std::ostream& os, const AgentName &agentName, const cells &noTurnFloor) { - if( (!enforceOneWays or noTurnFloor.size() == 0) or (noTurnFloor.size() == 0) ) { - os << "formula " << agentName << "CannotTurn = false;\n"; - return os; - } - bool first = true; - os << "formula " << agentName << "CannotTurn = "; - for(auto const& cell : noTurnFloor) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; - } - os << " | " << agentName << "IsOnSlippery;\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printIsFixedFormulas(std::ostream& os, const AgentName &agentName) { - os << "formula " << agentName << "IsFixed = false;\n"; - os << "formula " << agentName << "SlipperyTurnLeftAllowed = true;\n"; - os << "formula " << agentName << "SlipperyTurnRightAllowed = true;\n"; - os << "formula " << agentName << "SlipperyMoveForwardAllowed = true;\n"; - os << "label \"FixedStates" << agentName << "\" = " << agentName << "IsFixed | !" << agentName << "SlipperyTurnRightAllowed | !" << agentName << "SlipperyTurnLeftAllowed | !" << agentName << "SlipperyMoveForwardAllowed | " << agentName << "IsInGoal | " << agentName << "IsInLava"; - if(enforceOneWays) { - os << " | " << agentName << "CannotTurn"; - } - os << ";\n"; - //os << "label \"FixedStates\" = " << agentName << "IsFixed | " << agentName << "IsOnSlippery | " << agentName << "IsInGoal | " << agentName << "IsInLava;\n"; - - return os; - } - - - std::ostream& PrismModulesPrinter::printWallFormula(std::ostream& os, const AgentName &agentName, const cells &walls) { - os << "formula " << agentName << "IsOnWall = "; - bool first = true; - for(auto const& cell : walls) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; + for(const auto &ball : balls) { + printPortableObjectModule(ball); } - os << ";\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printFormulas(std::ostream& os, - const AgentName &agentName, - const cells &restrictionNorth, - const cells &restrictionEast, - const cells &restrictionSouth, - const cells &restrictionWest, - const std::vector> &slipperyCollection, - const cells &lava, - const cells &walls, - const cells &noTurnFloor, - const cells &slipperyNorth, - const cells &slipperyEast, - const cells &slipperySouth, - const cells &slipperyWest, - const cells &keys, - const cells &doors) { - printRestrictionFormula(os, agentName, "North", restrictionNorth, keys, doors); - printRestrictionFormula(os, agentName, "East", restrictionEast, keys, doors); - printRestrictionFormula(os, agentName, "South", restrictionSouth, keys, doors); - printRestrictionFormula(os, agentName, "West", restrictionWest, keys, doors); - - if (!keys.empty()) { - printKeyRestrictionFormula(os, agentName, "North", keys); - printKeyRestrictionFormula(os, agentName, "East", keys); - printKeyRestrictionFormula(os, agentName, "South", keys); - printKeyRestrictionFormula(os, agentName, "West", keys); + for(const auto &box : boxes) { + printPortableObjectModule(box); } - - if (!doors.empty()) { - printDoorRestrictionFormula(os, agentName, "North", doors); - printDoorRestrictionFormula(os, agentName, "East", doors); - printDoorRestrictionFormula(os, agentName, "South", doors); - printDoorRestrictionFormula(os, agentName, "West", doors); + for(const auto &door : unlockedDoors) { + printDoorModule(door, true); } - - printIsOnSlipperyFormula(os, agentName, slipperyCollection, slipperyNorth, slipperyEast, slipperySouth, slipperyWest); - printIsInLavaFormula(os, agentName, lava); - printWallFormula(os, agentName, walls); - printTurningNotAllowedFormulas(os, agentName, noTurnFloor); - printIsFixedFormulas(os, agentName); - os << "\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printGoalLabel(std::ostream& os, const AgentName &agentName, const cells &goals) { - if(goals.size() == 0) { - os << "formula " << agentName << "IsInGoal = false;\n"; - return os; + for(const auto &door : lockedDoors) { + printDoorModule(door, false); } - bool first = true; - os << "formula " << agentName << "IsInGoal = "; - for(auto const& cell : goals) { - if(first) first = false; else os << " | "; - os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")"; + for(const auto [agentName, initialPosition] : agentNameAndPositionMap) { + printRobotModule(agentName, initialPosition); } - os << ";\n"; - os << "formula " << agentName << "IsInGoalAndNotDone = " << agentName << "IsInGoal & !" << agentName << "Done;\n"; - os << "label \"" << agentName << "IsInGoalAndNotDone\" = " << agentName << "IsInGoal & !" << agentName << "Done;\n"; return os; } - std::ostream& PrismModulesPrinter::printCrashLabel(std::ostream &os, const std::vector agentNames) { - os << "label crash = "; - bool first = true; - for(auto const& agentName : agentNames) { - if(agentName == "Agent") continue; - if(first) first = false; else os << " | "; - os << "(xAgent=x" << agentName << ")&(yAgent=y" << agentName << ")"; - } - os << ";\n\n"; - return os; - } std::ostream& PrismModulesPrinter::printConfiguration(std::ostream& os, const std::vector& configurations) { - for (auto& configuration : configurations) { if (configuration.overwrite_ || configuration.type_ == ConfigType::Module) { continue; } - os << configuration.expression_ << std::endl; } - return os; } std::ostream& PrismModulesPrinter::printConstants(std::ostream &os, const std::vector &constants) { - for (auto& constant : constants) { os << constant << std::endl; } - - return os; - } - - - std::ostream& PrismModulesPrinter::printAvoidanceLabel(std::ostream &os, const std::vector agentNames, const int &distance) { - os << "label avoidance = "; - bool first = true; - for(auto const& agentName : agentNames) { - if(agentName == "Agent") continue; - if(first) first = false; else os << " | "; - os << "max(xAgent-x" << agentName << ",x" << agentName << "-xAgent)+"; - os << "max(yAgent-y" << agentName << ",y" << agentName << "-yAgent) "; - } - os << ";\n\n"; return os; } - // TODO this does not account for multiple agents yet, i.e. key can be picked up multiple times - std::ostream& PrismModulesPrinter::printKeysLabels(std::ostream& os, const AgentName &agentName, const cells &keys) { - if(keys.size() == 0) return os; - - for(auto const& key : keys) { - std::string keyColor = key.getColor(); - std::string xKey = "xKey" + keyColor; - std::string yKey = "yKey" + keyColor; - os << "label \"" << agentName << "PickedUp" << keyColor << "Key\" = " << agentName << "_has_" << keyColor << "_key = true;\n"; - os << "formula " << agentName << "CanPickUp" << keyColor << "Key = "; - os << "((x" << agentName << "-1 = " << xKey << "&y" << agentName << " = " << yKey << "&view" << agentName << " = 2) |"; - os << " (x" << agentName << "+1 = " << xKey << "&y" << agentName << " = " << yKey << "&view" << agentName << " = 0) |"; - os << " (x" << agentName << " = " << xKey << "&y" << agentName << "-1 = " << yKey << "&view" << agentName << " = 3) |"; - os << " (x" << agentName << " = " << xKey << "&y" << agentName << "+1 = " << yKey << "&view" << agentName << " = 1) ) &"; - os << "!" << agentName << "_has_" << keyColor << "_key;"; - } - os << "\n"; - return os; - } std::ostream& PrismModulesPrinter::printBooleansForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) { for(auto const& key : keys) { os << "\t" << agentName << "_has_"<< key.getColor() << "_key : bool;\n";//init false;\n"; } - - os << "\n"; return os; } @@ -432,7 +91,7 @@ namespace prism { os << "\t[pickup_" << keyColor << "_key]\t" << pickupGuard(agentName, keyColor) << "-> "; os << "(" << agentName << "_has_" << keyColor << "_key'=true) & (" << agentName << "_is_carrying_object'=true);\n"; os << "\n"; - + os << "\t[drop_" << keyColor << "_key_north]\t" << dropGuard(agentName, keyColor, 3) << "-> "; os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n"; @@ -472,7 +131,7 @@ namespace prism { std::ostream& PrismModulesPrinter::printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType) { os << "init\n"; os << "\t"; - + bool first = true; for (auto const& agent : agents) { if (first) first = false; @@ -483,14 +142,14 @@ namespace prism { if(enforceOneWays) { os << " & ( !AgentCannotTurn ) "; } else { - // os << " & ( !AgentIsOnSlippery ) "; + // os << " & ( !AgentIsOnSlippery ) "; } for (auto const& key : keys) { os << " & ( !" << agent.first << "_has_" << key.first << "_key )"; } } - + for (auto const& key : keys) { os << " & ( xKey" << key.first << "="<< key.second.second<< ")"; os << " & ( yKey" << key.first << "=" << key.second.first << ")"; @@ -505,13 +164,13 @@ namespace prism { } if (modelType == ModelType::SMG) { - os << " & move=0"; + os << " & move=0"; } os << "\nendinit\n\n"; - + return os; } @@ -529,9 +188,9 @@ namespace prism { os << "\n"; if (faultyProbability != 0.0) { os << "\t[" << agentName << "_turn_right] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " + 1, 4))" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " + 2, 4))" << ";\n"; - os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << ">1 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " - 1, 4))" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " - 2, 4))" << ";\n"; - os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=0 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=3)" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=2)" << ";\n"; - os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=1 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=0)" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=3)" << ";\n"; + os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << ">1 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " - 1, 4))" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " - 2, 4))" << ";\n"; + os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=0 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=3)" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=2)" << ";\n"; + os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=1 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=0)" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=3)" << ";\n"; } else { os << "\t[" << agentName << "_turn_right] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery -> (view" << agentName << "'=mod(view" << agentName << " + 1, 4)) " << moveUpdate(agentIndex) << ";\n"; os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << ">0 -> (view" << agentName << "'=view" << agentName << " - 1) " << moveUpdate(agentIndex) << ";\n"; @@ -562,93 +221,118 @@ namespace prism { return os; } - std::ostream& PrismModulesPrinter::printKeyModule(std::ostream &os, const cell &key, const coordinates &boundaries, AgentName agentName) { - std::string keyIdentifier = "Key" + key.getColor(); - - os << "module " << keyIdentifier << "\n"; - - os << "\tx" << keyIdentifier << " : [-1.." << boundaries.second << "];\n"; - os << "\ty" << keyIdentifier << " : [-1.." << boundaries.first << "];\n"; + void PrismModulesPrinter::printPortableObjectModule(const cell &object) { + std::string identifier = capitalize(object.getColor()) + object.getType(); + os << "\nmodule " << identifier << std::endl; + os << "\tx" << identifier << " : [-1.." << maxBoundaries.second << "] init " << object.column << ";\n"; + os << "\ty" << identifier << " : [-1.." << maxBoundaries.first << "] init " << object.row << ";\n"; + os << "\t" << identifier << "PickedUp : bool;\n"; os << "\n"; - printKeyActions(os, key ,keyIdentifier, agentName); - os << "\n"; - return os; + for(const auto [name, position] : agentNameAndPositionMap) { + printPortableObjectActions(name, identifier); + } + os << "endmodule\n\n"; } - - std::ostream& PrismModulesPrinter::printKeyActions(std::ostream &os, const cell &key ,const std::string &keyIdentifier, AgentName agentName) { - std::string keyColor = key.getColor(); - os << "\t[pickup_" << keyColor << "key]\t" << pickupGuard(agentName, keyColor) << "-> "; - os << "(xKey" << keyColor << "'=-1) & (yKey" << keyColor << "'=-1)" << ";\n"; + void PrismModulesPrinter::printPortableObjectActions(const std::string &agentName, const std::string &identifier) { + os << "\t[" << agentName << "_pickup_" << identifier << "]\ttrue -> (x" << identifier << "'=-1) & (y" << identifier << "'=-1) & (" << identifier << "PickedUp'=true);\n"; + os << "\t[" << agentName << "_drop_" << identifier << "_north]\ttrue -> (x" << identifier << "'=x" << agentName << ") & (y" << identifier << "'=y" << agentName << "-1) & (" << identifier << "PickedUp'=false);\n"; + os << "\t[" << agentName << "_drop_" << identifier << "_west]\ttrue -> (x" << identifier << "'=x" << agentName << "-1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n"; + os << "\t[" << agentName << "_drop_" << identifier << "_south]\ttrue -> (x" << identifier << "'=x" << agentName << ") & (y" << identifier << "'=y" << agentName << "+1) & (" << identifier << "PickedUp'=false);\n"; + os << "\t[" << agentName << "_drop_" << identifier << "_east]\ttrue -> (x" << identifier << "'=x" << agentName << "+1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n"; + } - os << "\t[drop_" << keyColor << "_key_north]\t" << dropGuard(agentName, keyColor, 3) << "-> "; - os << "(xKey" << keyColor << "'=x" << agentName << ") & (yKey" << keyColor << "'=y" < "; - os << "(xKey" << keyColor << "'=x" << agentName << "-1) & (yKey" << keyColor << "'=y" < "; - os << "(xKey" << keyColor << "'=x" << agentName << ") & (yKey" << keyColor << "'=y" < "; - os << "(xKey" << keyColor << "'=x" << agentName << "+1) & (yKey" << keyColor << "'=y" < (" << identifier << "Open'=true);\n"; + os << "\t[" << agentName << "_close_" << identifier << "] " << identifier << "Open -> (" << identifier << "Open'=false);\n"; + } - return os; + void PrismModulesPrinter::printUnlockedDoorActions(const std::string &agentName, const std::string &identifier) { + os << "\t[" << agentName << "_open_" << identifier << "] !" << identifier << "Open -> (" << identifier << "Open'=true);\n"; + os << "\t[" << agentName << "_close_" << identifier << "] " << identifier << "Open -> (" << identifier << "Open'=false);\n"; } - std::ostream& PrismModulesPrinter::printDoorModule(std::ostream &os, const cell &door, const coordinates &boundaries, AgentName agent) { - std::string doorIdentifier = "Door" + door.getColor(); - os << "module " << doorIdentifier << "\n"; - - os << "\t" << doorIdentifier << "locked : bool;\n"; - os << "\t" << doorIdentifier << "open : bool;\n"; + void PrismModulesPrinter::printRobotModule(const AgentName &agentName, const coordinates &initialPosition) { + os << "\nmodule " << agentName << std::endl; + os << "\tx" << agentName << " : [0.." << maxBoundaries.second << "] init " << initialPosition.second << ";\n"; + os << "\ty" << agentName << " : [0.." << maxBoundaries.first << "] init " << initialPosition.first << ";\n"; + os << "\tview" << agentName << " : [0..3] init 1;\n"; - printDoorActions(os, door, doorIdentifier, agent); - - return os; - } + for(const auto &door : unlockedDoors) { + std::string identifier = capitalize(door.getColor()) + door.getType(); + printUnlockedDoorActionsForRobot(agentName, identifier); + } - std::ostream& PrismModulesPrinter::printDoorActions(std::ostream &os, const cell &door ,const std::string &doorIdentifier, AgentName agentName) { - os << "\t[" << "unlock_" << doorIdentifier << "]\t" << unlockGuard(agentName, door) << " -> "; - os << "(" << doorIdentifier << "locked'=false) & (" << doorIdentifier << "open'=true) ;\n"; + for(const auto &door : lockedDoors) { + std::string identifier = capitalize(door.getColor()) + door.getType(); + std::string key = capitalize(door.getColor()) + "Key"; + printLockedDoorActionsForRobot(agentName, identifier, key); + } - os << "\t[" << "toggle_" << doorIdentifier << "]\t" << toggleGuard(agentName, door) << " -> "; - os << "(" << doorIdentifier << "open'=!" << doorIdentifier << "open) ;\n"; + for(const auto &key : keys) { + std::string identifier = capitalize(key.getColor()) + key.getType(); + os << "\tCarrying" << identifier << " : bool init false;\n"; + printPortableObjectActionsForRobot(agentName, identifier); + } - return os; + os << "\n" << actionStream.str(); + os << "endmodule\n\n"; } - std::string PrismModulesPrinter::unlockGuard(const AgentName &agentName, const cell& door) { - std::string doorColor = door.getColor(); - std::string ret; - ret += agentName + "_has_" + doorColor + "_key & "; - ret += "((" + viewVariable(agentName, 0, true) + "x" + agentName + "+ 1 = " + std::to_string(door.column) + " & y" + agentName + "= " + std::to_string(door.row) + ")"; - ret += " | (" + viewVariable(agentName, 1, true) + "x" + agentName + " = " + std::to_string(door.column) + " & y" + agentName + " + 1 = " + std::to_string(door.row) + ")"; - ret += " | (" + viewVariable(agentName, 2, true) + "x" + agentName + "- 1 = " + std::to_string(door.column) + " & y" + agentName + "= " + std::to_string(door.row) + ")"; - ret += " | (" + viewVariable(agentName, 3, true) + "x" + agentName + " = " + std::to_string(door.column) + " & y" + agentName + " - 1 = " + std::to_string(door.row) + "))"; - - - return ret; + void PrismModulesPrinter::printPortableObjectActionsForRobot(const std::string &a, const std::string &i) { + actionStream << "\t[" << a << "_pickup_" << i << "] " << a << "CannotMove" << i << " -> (Carrying" << i << "'=true);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_north]\tCarrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_west] \tCarrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_south]\tCarrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_east] \tCarrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (Carrying" << i << "'=false);\n"; + actionStream << "\n"; } + void PrismModulesPrinter::printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier) { + actionStream << "\t[" << agentName << "_open_" << identifier << "] " << agentName << "CannotMove" << identifier << " -> true;\n"; + actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " -> true;\n"; + actionStream << "\n"; + } - std::string PrismModulesPrinter::toggleGuard(const AgentName &agentName, const cell& door) { - std::string doorColor = door.getColor(); - std::string ret; - ret += "!Door" + doorColor + "locked & ("; - ret += "(" + viewVariable(agentName, 0, true) + "x" + agentName + "+ 1 = " + std::to_string(door.column) + " & y" + agentName + "= " + std::to_string(door.row) + ")"; - ret += " | (" + viewVariable(agentName, 1, true) + "x" + agentName + " = " + std::to_string(door.column) + " & y" + agentName + " + 1 = " + std::to_string(door.row) + ")"; - ret += " | (" + viewVariable(agentName, 2, true) + "x" + agentName + "- 1 = " + std::to_string(door.column) + " & y" + agentName + "= " + std::to_string(door.row) + ")"; - ret += " | (" + viewVariable(agentName, 3, true) + "x" + agentName + " = " + std::to_string(door.column) + " & y" + agentName + " - 1 = " + std::to_string(door.row) + "))"; - - - - return ret; + void PrismModulesPrinter::printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key) { + actionStream << "\t[" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & Carrying" << key << " -> true;\n"; + actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & Carrying" << key << " -> true;\n"; + actionStream << "\n"; } + + + + + + + + + + + std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) { for (auto& config : configuration) { if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) { @@ -661,7 +345,7 @@ namespace prism { return os; } - std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability, const double &stickyProbability) { + std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability, const double &stickyProbability) { if(stickyProbability != 0.0) { os << "\t[" << agentName << "_move_north]" << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveNorth -> " << (100 - stickyProbability) << "/100:" << "(y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(y" << agentName << "'=max(y" << agentName << "-2, 1 ))" << moveUpdate(agentIndex) << ";\n"; os << "\t[" << agentName << "_move_east] " << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveEast -> " << (100 - stickyProbability) << "/100:" << "(x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(x" << agentName << "'=min(x" << agentName << "+2, width))" << moveUpdate(agentIndex) << ";\n"; @@ -835,7 +519,7 @@ namespace prism { std::array prob_piece_dir_constants_agent_east; std::array prob_piece_dir_constants_agent_south; std::array prob_piece_dir_constants_agent_west; - + switch (orientation) { case SlipperyType::North: @@ -849,12 +533,12 @@ namespace prism { prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 2, 0 /* <- R */, 0 }; prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero" }; - + prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_zero" }; prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement", "prop_zero", "prop_zero" } ; - + straightPosIndex = 4; straightPosIndex_east = 2; @@ -868,25 +552,25 @@ namespace prism { case SlipperyType::South: actionName = "\t[" + agentName + "move_on_slip_south]"; positionGuard = "\t" + agentName + "IsOnSlipperySouth"; - + prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 }; // { n, ne, e, se, s, sw, w, nw } prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; - + prob_piece_dir_constants = { "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement" }; - + prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement * 1/2" }; prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement" } ; - + straightPosIndex = 0; // always north straightPosIndex_east = 2; - straightPosIndex_south = 4; + straightPosIndex_south = 4; straightPosIndex_west = 6; straightPosIndex_north = 0; specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)"); @@ -911,10 +595,10 @@ namespace prism { prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" } ; prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2" } ; - + straightPosIndex = 6; straightPosIndex_east = 2; - straightPosIndex_south = 4; + straightPosIndex_south = 4; straightPosIndex_west = 6; straightPosIndex_north = 0; @@ -934,7 +618,7 @@ namespace prism { prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; prob_piece_dir_constants = {"prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero","prop_zero", "prop_zero" }; - + prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; @@ -943,7 +627,7 @@ namespace prism { straightPosIndex = 2; straightPosIndex_east = 2; - straightPosIndex_south = 4; + straightPosIndex_south = 4; straightPosIndex_west = 6; straightPosIndex_north = 0; specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)"); @@ -988,14 +672,14 @@ namespace prism { // generic output (for every view and every possible view direction) size_t current_dir = 0; // East os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - + for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_east.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); } current_dir = 1; // South os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - + for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_south.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); } @@ -1009,7 +693,7 @@ namespace prism { current_dir = 3; // North - + os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_north.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 9e5936c..460aa67 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -9,43 +9,25 @@ namespace prism { class PrismModulesPrinter { public: - PrismModulesPrinter(const ModelType &modelType, const size_t &numberOfPlayer, std::vector config ,const bool enforceOneWays = false); - - std::ostream& printRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &grid_cells, const cells& keys, const cells& doors); - std::ostream& printKeyRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &keys); - std::ostream& printDoorRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &doors); - std::ostream& printIsOnSlipperyFormula(std::ostream& os, const AgentName &agentName, const std::vector> &slipperyCollection, const cells &slipperyNorth, const cells &slipperyEast, const cells &slipperySouth, const cells &slipperyWest); - std::ostream& printGoalLabel(std::ostream& os, const AgentName&agentName, const cells &goals); - std::ostream& printCrashLabel(std::ostream &os, const std::vector agentNames); - std::ostream& printAvoidanceLabel(std::ostream &os, const std::vector agentNames, const int &distance); - std::ostream& printKeysLabels(std::ostream& os, const AgentName&agentName, const cells &keys); - std::ostream& printBackgroundLabels(std::ostream &os, const AgentName &agentName, const std::pair &backgroundTiles); - std::ostream& printIsInLavaFormula(std::ostream& os, const AgentName &agentName, const cells &lava); - std::ostream& printIsFixedFormulas(std::ostream& os, const AgentName &agentName); - std::ostream& printTurningNotAllowedFormulas(std::ostream& os, const AgentName &agentName, const cells &floor); - std::ostream& printWallFormula(std::ostream& os, const AgentName &agentName, const cells &walls); - std::ostream& printFormulas(std::ostream& os, - const AgentName&agentName, - const cells &restrictionNorth, - const cells &restrictionEast, - const cells &restrictionSouth, - const cells &restrictionWest, - const std::vector> &slipperyCollection, - const cells &lava, - const cells &walls, - const cells &noTurnFloor, - const cells &slipperyNorth, - const cells &slipperyEast, - const cells &slipperySouth, - const cells &slipperyWest, - const cells &keys, - const cells &doors); - - std::ostream& printKeyModule(std::ostream &os, const cell &key, const coordinates &boundaries, AgentName agentName); - std::ostream& printKeyActions(std::ostream &os, const cell& key ,const std::string &keyIdentifier, AgentName agentName); - - std::ostream& printDoorModule(std::ostream &os, const cell &door, const coordinates &boundaries, AgentName agentName); - std::ostream& printDoorActions(std::ostream &os, const cell &door ,const std::string &doorIdentifier, AgentName agentName); + PrismModulesPrinter(std::ostream &os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const bool enforceOneWays = false); + + std::ostream& print(); + + std::ostream& printModelType(const ModelType &modelType); + + void printPortableObjectModule(const cell &object); + void printPortableObjectActions(const std::string &agentName, const std::string &identifier); + + void printDoorModule(const cell &object, const bool &opened); + void printLockedDoorActions(const std::string &agentName, const std::string &identifier); + void printUnlockedDoorActions(const std::string &agentName, const std::string &identifier); + + void printRobotModule(const AgentName &agentName, const coordinates &initialPosition); + void printPortableObjectActionsForRobot(const std::string &agentName, const std::string &identifier); + + void printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier); + void printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key); + std::ostream& printConstants(std::ostream &os, const std::vector &constants); /* * Representation for Slippery Tile. @@ -72,7 +54,6 @@ namespace prism { */ std::ostream& printSlipperyTurn(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation); - std::ostream& printModel(std::ostream &os, const ModelType &modelType); std::ostream& printBooleansForKeys(std::ostream &os, const AgentName &agentName, const cells &keys); std::ostream& printActionsForKeys(std::ostream &os, const AgentName &agentName, const cells &keys); std::ostream& printBooleansForBackground(std::ostream &os, const AgentName &agentName, const std::map &backgroundTiles); @@ -102,16 +83,25 @@ namespace prism { std::string pickupGuard(const AgentName &agentName, const std::string keyColor); std::string dropGuard(const AgentName &agentName, const std::string keyColor, size_t view); std::string moveUpdate(const size_t &agentIndex); - std::string unlockGuard(const AgentName &agentName, const cell& door); - std::string toggleGuard(const AgentName &agentName, const cell& door); std::string viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView); bool isGame() const; private: + std::ostream &os; + std::stringstream actionStream; ModelType const& modelType; - const size_t numberOfPlayer; + coordinates const& maxBoundaries; + AgentName agentName; + cells boxes; + cells balls; + cells lockedDoors; + cells unlockedDoors; + cells keys; + + AgentNameAndPositionMap agentNameAndPositionMap; + size_t numberOfPlayer; bool enforceOneWays; std::vector configuration; std::map viewDirectionMapping; From 26a7808eeb44b5c3d862eac180d401fd90004d2c Mon Sep 17 00:00:00 2001 From: sp Date: Sat, 6 Jan 2024 18:45:24 +0100 Subject: [PATCH 12/38] fix carrying variable --- util/PrismModulesPrinter.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 1f26d89..e97adb0 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -291,7 +291,7 @@ namespace prism { for(const auto &key : keys) { std::string identifier = capitalize(key.getColor()) + key.getType(); - os << "\tCarrying" << identifier << " : bool init false;\n"; + os << "\t" << agentName << "Carrying" << identifier << " : bool init false;\n"; printPortableObjectActionsForRobot(agentName, identifier); } @@ -300,11 +300,11 @@ namespace prism { } void PrismModulesPrinter::printPortableObjectActionsForRobot(const std::string &a, const std::string &i) { - actionStream << "\t[" << a << "_pickup_" << i << "] " << a << "CannotMove" << i << " -> (Carrying" << i << "'=true);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_north]\tCarrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_west] \tCarrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_south]\tCarrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_east] \tCarrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_pickup_" << i << "] " << a << "CannotMove" << i << " -> (" << a << "Carrying" << i << "'=true);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_north]\t" << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_west] \t" << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_south]\t" << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_east] \t" << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n"; actionStream << "\n"; } @@ -315,8 +315,8 @@ namespace prism { } void PrismModulesPrinter::printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key) { - actionStream << "\t[" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & Carrying" << key << " -> true;\n"; - actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & Carrying" << key << " -> true;\n"; + actionStream << "\t[" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; + actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; actionStream << "\n"; } From 4d548f950295cba7203f70eac1e30097bb379b9b Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 10:06:54 +0100 Subject: [PATCH 13/38] first steps towards robots modules This includes: - PrismFormulaPrinter.print takes agentName as argument - added formulas for goals, isnextto, portableobjects - added movement helper for deterministic movement - removed enforceOneWays (for now) --- util/PrismFormulaPrinter.cpp | 47 +-- util/PrismFormulaPrinter.h | 15 +- util/PrismModulesPrinter.cpp | 658 ++++++++++++++++++----------------- util/PrismModulesPrinter.h | 24 +- 4 files changed, 384 insertions(+), 360 deletions(-) diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index 8f333f2..99ba5eb 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -28,65 +28,72 @@ std::map getSurroundingCells(const cell &c) { } namespace prism { - PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava) - : os(os), agentName(agentName), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava) + PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava, const cells &goals) + : os(os), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava), goals(goals) { } - void PrismFormulaPrinter::print() { + void PrismFormulaPrinter::print(const AgentName &agentName) { for(const auto& [direction, cells] : restrictions) { - printRestrictionFormula(direction, cells); + printRestrictionFormula(agentName, direction, cells); } for(const auto& [direction, cells] : slipperyTiles) { - printIsOnFormula("Slippery", cells, direction); + printIsOnFormula(agentName, "Slippery", cells, direction); } - printIsOnFormula("Lava", lava); + std::vector allSlipperyDirections = {agentName + "IsOnSlipperyNorth", agentName + "IsOnSlipperyEast", agentName + "IsOnSlipperySouth", agentName + "IsOnSlipperyWest"}; + os << buildFormula(agentName + "IsOnSlippery", vectorToDisjunction(allSlipperyDirections)); + printIsOnFormula(agentName, "Lava", lava); + printIsOnFormula(agentName, "Goal", goals); for(const auto& ball : balls) { - std::string color = capitalize(ball.getColor()); - printRestrictionFormulaWithCondition(color + "Ball", getSurroundingCells(ball), "!" + color + "BallPickedUp"); + std::string identifier = capitalize(ball.getColor()) + ball.getType(); + printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(ball), "!" + identifier + "PickedUp"); + portableObjects.push_back(agentName + "Carrying" + identifier); } for(const auto& box : boxes) { - std::string color = capitalize(box.getColor()); - printRestrictionFormulaWithCondition(color + "Box", getSurroundingCells(box), "!" + color + "BoxPickedUp"); + std::string identifier = capitalize(box.getColor()) + box.getType(); + printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(box), "!" + identifier + "PickedUp"); + portableObjects.push_back(agentName + "Carrying" + identifier); } for(const auto& key : keys) { - std::string color = capitalize(key.getColor()); - printRestrictionFormulaWithCondition(color + "Key", getSurroundingCells(key), "!" + color + "KeyPickedUp"); + std::string identifier = capitalize(key.getColor()) + key.getType(); + printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(key), "!" + identifier + "PickedUp"); + portableObjects.push_back(agentName + "Carrying" + identifier); } for(const auto& door : unlockedDoors) { std::string identifier = capitalize(door.getColor()) + door.getType(); - printRestrictionFormulaWithCondition(identifier, getSurroundingCells(door), "!" + identifier + "Open"); - printIsNextToFormula(identifier, getSurroundingCells(door)); + printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(door), "!" + identifier + "Open"); + printIsNextToFormula(agentName, identifier, getSurroundingCells(door)); } for(const auto& door : lockedDoors) { std::string identifier = capitalize(door.getColor()) + door.getType(); - printRestrictionFormulaWithCondition(identifier, getSurroundingCells(door), "!" + identifier + "Open"); - printIsNextToFormula(identifier, getSurroundingCells(door)); + printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(door), "!" + identifier + "Open"); + printIsNextToFormula(agentName, identifier, getSurroundingCells(door)); } if(conditionalMovementRestrictions.size() > 0) { os << buildFormula(agentName + "CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions)); + os << buildFormula(agentName + "IsCarrying", vectorToDisjunction(portableObjects)); } } - void PrismFormulaPrinter::printRestrictionFormula(const std::string &direction, const cells &grid_cells) { + void PrismFormulaPrinter::printRestrictionFormula(const AgentName &agentName, const std::string &direction, const cells &grid_cells) { os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells)); } - void PrismFormulaPrinter::printIsOnFormula(const std::string &type, const cells &grid_cells, const std::string &direction) { + void PrismFormulaPrinter::printIsOnFormula(const AgentName &agentName, const std::string &type, const cells &grid_cells, const std::string &direction) { os << buildFormula(agentName + "IsOn" + type + direction, buildDisjunction(agentName, grid_cells)); } - void PrismFormulaPrinter::printIsNextToFormula(const std::string &type, const std::map &coordinates) { + void PrismFormulaPrinter::printIsNextToFormula(const AgentName &agentName, const std::string &type, const std::map &coordinates) { os << buildFormula(agentName + "IsNextTo" + type, buildDisjunction(agentName, coordinates)); } - void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const std::string &reason, const std::map &coordinates, const std::string &condition) { + void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::map &coordinates, const std::string &condition) { os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition); conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason); } diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index 4795f3e..d1ead81 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -15,14 +15,14 @@ std::map getSurroundingCells(const cell &c); namespace prism { class PrismFormulaPrinter { public: - PrismFormulaPrinter(std::ostream &os, const AgentName &agentName, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava); + PrismFormulaPrinter(std::ostream &os, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava, const cells &goals); - void print(); + void print(const AgentName &agentName); - void printRestrictionFormula(const std::string &direction, const cells &grid_cells); - void printIsOnFormula(const std::string &type, const cells &grid_cells, const std::string &direction = ""); - void printIsNextToFormula(const std::string &type, const std::map &coordinates); - void printRestrictionFormulaWithCondition(const std::string &reason, const std::map &coordinates, const std::string &condition); + void printRestrictionFormula(const AgentName &agentName, const std::string &direction, const cells &grid_cells); + void printIsOnFormula(const AgentName &agentName, const std::string &type, const cells &grid_cells, const std::string &direction = ""); + void printIsNextToFormula(const AgentName &agentName, const std::string &type, const std::map &coordinates); + void printRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::map &coordinates, const std::string &condition); private: std::string buildFormula(const std::string &formulaName, const std::string &formula); std::string buildLabel(const std::string &labelName, const std::string &label); @@ -30,7 +30,6 @@ namespace prism { std::string buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions = {}); std::ostream &os; - AgentName agentName; // move this to functions std::map restrictions; cells boxes; cells balls; @@ -39,7 +38,9 @@ namespace prism { cells keys; std::map slipperyTiles; cells lava; + cells goals; std::vector conditionalMovementRestrictions; + std::vector portableObjects; }; } diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index e97adb0..7886c89 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -5,9 +5,13 @@ namespace prism { - PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const bool enforceOneWays) - : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), agentNameAndPositionMap(agentNameAndPositionMap), enforceOneWays(enforceOneWays), configuration(config), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) { + PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float &faultyProbability) + : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), agentNameAndPositionMap(agentNameAndPositionMap), configuration(config), faultyProbability(faultyProbability), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) { numberOfPlayer = agentNameAndPositionMap.size(); + size_t index = 0; + for(auto begin = agentNameAndPositionMap.begin(); begin != agentNameAndPositionMap.end(); begin++, index++) { + agentIndexMap[begin->first] = index; + } } std::ostream& PrismModulesPrinter::printModelType(const ModelType &modelType) { @@ -129,6 +133,7 @@ namespace prism { } std::ostream& PrismModulesPrinter::printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType) { + /* os << "init\n"; os << "\t"; @@ -139,9 +144,6 @@ namespace prism { os << "(!" << agent.first << "IsInGoal & !" << agent.first << "IsInLava & !" << agent.first << "Done & !" << agent.first << "IsOnWall & "; os << "x" << agent.first << "=" << agent.second.second << " & y" << agent.first << "=" << agent.second.first << ")"; os << " & !" << agent.first << "_is_carrying_object"; - if(enforceOneWays) { - os << " & ( !AgentCannotTurn ) "; - } else { // os << " & ( !AgentIsOnSlippery ) "; } @@ -170,11 +172,12 @@ namespace prism { os << "\nendinit\n\n"; - + */ return os; } std::ostream& PrismModulesPrinter::printModule(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &boundaries, const coordinates& initialPosition, const cells &keys, const std::map &backgroundTiles, const bool agentWithView, const std::vector &probabilities, const double faultyProbability) { + /* os << "module " << agentName << "\n"; os << "\tx" << agentName << " : [1.." << boundaries.second << "];\n"; os << "\ty" << agentName << " : [1.." << boundaries.first << "];\n"; @@ -196,12 +199,6 @@ namespace prism { os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << ">0 -> (view" << agentName << "'=view" << agentName << " - 1) " << moveUpdate(agentIndex) << ";\n"; os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=0 -> (view" << agentName << "'=3) " << moveUpdate(agentIndex) << ";\n"; } - if(enforceOneWays) { - os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 0 & " << agentName << "CannotMoveEast -> true;\n"; - os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 1 & " << agentName << "CannotMoveSouth -> true;\n"; - os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 2 & " << agentName << "CannotMoveWest -> true;\n"; - os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 3 & " << agentName << "CannotMoveNorth -> true;\n"; - } } else { os << "\t[" << agentName << "_turns] " << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << moveGuard(agentIndex) << " true -> (x" << agentName << "'=x" << agentName << ")" << moveUpdate(agentIndex) << ";\n"; } @@ -218,6 +215,7 @@ namespace prism { printConfiguredActions(os, agentName); os << "\n"; + */ return os; } @@ -278,6 +276,8 @@ namespace prism { os << "\ty" << agentName << " : [0.." << maxBoundaries.first << "] init " << initialPosition.first << ";\n"; os << "\tview" << agentName << " : [0..3] init 1;\n"; + printMovementActionsForRobot(agentName); + for(const auto &door : unlockedDoors) { std::string identifier = capitalize(door.getColor()) + door.getType(); printUnlockedDoorActionsForRobot(agentName, identifier); @@ -296,6 +296,7 @@ namespace prism { } os << "\n" << actionStream.str(); + actionStream.str(std::string()); os << "endmodule\n\n"; } @@ -320,18 +321,26 @@ namespace prism { actionStream << "\n"; } + void PrismModulesPrinter::printTurningActionsForRobot(const AgentName &a) { + } + void PrismModulesPrinter::printMovementActionsForRobot(const AgentName &a) { + if(faultyProbability <= 0.0) { + actionStream << printMovementGuard(a, "North", 3) << printMovementUpdate(a, "y" + a + "'=y" + a + "-1"); + actionStream << printMovementGuard(a, "East", 0) << printMovementUpdate(a, "x" + a + "'=x" + a + "+1"); + actionStream << printMovementGuard(a, "South", 1) << printMovementUpdate(a, "y" + a + "'=y" + a + "+1"); + actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, "x" + a + "'=x" + a + "-1"); + } + } + std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) { + return "\t[" + a + "_move_" + direction + "]" + moveGuard(a) + viewVariable(a, 3) + " !" + a + "IsOnSlippery & !" + a + "IsOnLava & !" + a + "IsOnGoal & !" + a + "CannotMove" + direction + "Wall -> "; + } - - - - - - - - + std::string PrismModulesPrinter::printMovementUpdate(const AgentName &a, const std::string &update) { + return "(" + update + ") & " + moveUpdate(a) + ";\n"; + } std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) { for (auto& config : configuration) { @@ -346,6 +355,7 @@ namespace prism { } std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability, const double &stickyProbability) { + /* if(stickyProbability != 0.0) { os << "\t[" << agentName << "_move_north]" << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveNorth -> " << (100 - stickyProbability) << "/100:" << "(y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(y" << agentName << "'=max(y" << agentName << "-2, 1 ))" << moveUpdate(agentIndex) << ";\n"; os << "\t[" << agentName << "_move_east] " << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveEast -> " << (100 - stickyProbability) << "/100:" << "(x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(x" << agentName << "'=min(x" << agentName << "+2, width))" << moveUpdate(agentIndex) << ";\n"; @@ -381,323 +391,320 @@ namespace prism { os << probabilityString << ": (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << " + "; os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n"; } + */ return os; } - std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex) { - os << "\t[" << agentName << "_done]" << moveGuard(agentIndex) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n"; + std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName) { + os << "\t[" << agentName << "_done]" << moveGuard(agentName) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n"; return os; } std::ostream& PrismModulesPrinter::printSlipperyTurn(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation) { - constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 9; - - std::array positionTransition = { - /* north */ "(y" + agentName + "'=y" + agentName + "-1)", - /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)", - /* east */ "(x" + agentName + "'=x" + agentName + "+1)", - /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)", - /* south */ "(y" + agentName + "'=y" + agentName + "+1)", - /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)", - /* west */ "(x" + agentName + "'=x" + agentName + "-1)", - /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)", - /* own position */ "(x" + agentName + "'=x" + agentName + ") & (y" + agentName + "'=y" + agentName + ")" - }; - - // view transition appdx in form (guard, update part) - // IMPORTANT: No mod() usage for turn left due to bug in mod() function for decrement - - - std::array, 3> viewTransition = { - std::make_tuple(" & " + agentName + "SlipperyTurnRightAllowed ", " & (view" + agentName + "'=mod(view" + agentName + " + 1, 4))", "_right]"), - std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + ">0", " & (view" + agentName + "'=view" + agentName + " - 1)", "_left]"), - std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + "=0", " & (view" + agentName + "'=3)", "_left]") - }; - - // direction specifics - - std::string actionName; - std::string positionGuard; - std::size_t remainPosIndex = 8; - std::array prob_piece_dir; // { n, ne, e, se, s, sw, w, nw, CURRENT POS } - std::array prob_piece_dir_constants; - - switch (orientation) - { - case SlipperyType::North: - actionName = "\t[" + agentName + "turn_at_slip_north"; - positionGuard = "\t" + agentName + "IsOnSlipperyNorth"; - prob_piece_dir = { 0, 0, 0, 0, 1, 0, 0, 0, 0 /* <- R */ }; - prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_turn_displacement" /* <- R */, "prop_zero", "prop_zero", "prop_zero","prop_zero" }; - break; - - case SlipperyType::South: - actionName = "\t[" + agentName + "turn_at_slip_south"; - positionGuard = "\t" + agentName + "IsOnSlipperySouth"; - prob_piece_dir = { 1, 0, 0, 0, 0, 0, 0, 0, 0 /* <- R */ }; - prob_piece_dir_constants = { "prop_turn_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" }; - break; - - case SlipperyType::East: - actionName = "\t[" + agentName + "turn_at_slip_east"; - positionGuard = "\t" + agentName + "IsOnSlipperyEast"; - prob_piece_dir = { 0, 0, 0, 0, 0, 0, 1, 0, 0 /* <- R */ }; - prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_turn_displacement", "prop_zero", "prop_zero" }; - break; - - case SlipperyType::West: - actionName = "\t[" + agentName + "turn_at_slip_west"; - positionGuard = "\t" + agentName + "IsOnSlipperyWest"; - prob_piece_dir = { 0, 0, 1, 0, 0, 0, 0, 0, 0 /* <- R */ }; - prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_turn_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" }; - break; - } - - slipperyActions.insert(actionName + "_left]"); - slipperyActions.insert(actionName + "_right]"); - - // override probability to 0 if corresp. direction is blocked - - for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS - 1; i++) { - if (!neighborhood.at(i)) - prob_piece_dir.at(i) = 0; - } - - // determine residual probability (R) by replacing 0 with (1 - overall sum) - - prob_piece_dir.at(remainPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0); - prob_piece_dir_constants.at(remainPosIndex) = "prop_turn_intended"; - // - { - assert(prob_piece_dir.at(remainPosIndex) <= 9 && prob_piece_dir.at(remainPosIndex) >= 6 && "Value not in Range!"); - assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!"); - } - // - - // generic output (for every view transition) - for (std::size_t v = 0; v < viewTransition.size(); v++) { - os << actionName << std::get<2>(viewTransition.at(v)) << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << std::get<0>(viewTransition.at(v)); - for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - if (i == remainPosIndex) { - os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants.at(i) << " : " << positionTransition.at(i) << std::get<1>(viewTransition.at(v)) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - } else { - os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - } - } - } + // constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 9; + + // std::array positionTransition = { + // /* north */ "(y" + agentName + "'=y" + agentName + "-1)", + // /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)", + // /* east */ "(x" + agentName + "'=x" + agentName + "+1)", + // /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)", + // /* south */ "(y" + agentName + "'=y" + agentName + "+1)", + // /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)", + // /* west */ "(x" + agentName + "'=x" + agentName + "-1)", + // /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)", + // /* own position */ "(x" + agentName + "'=x" + agentName + ") & (y" + agentName + "'=y" + agentName + ")" + // }; + + // // view transition appdx in form (guard, update part) + // // IMPORTANT: No mod() usage for turn left due to bug in mod() function for decrement + + + // std::array, 3> viewTransition = { + // std::make_tuple(" & " + agentName + "SlipperyTurnRightAllowed ", " & (view" + agentName + "'=mod(view" + agentName + " + 1, 4))", "_right]"), + // std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + ">0", " & (view" + agentName + "'=view" + agentName + " - 1)", "_left]"), + // std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + "=0", " & (view" + agentName + "'=3)", "_left]") + // }; + + // // direction specifics + + // std::string actionName; + // std::string positionGuard; + // std::size_t remainPosIndex = 8; + // std::array prob_piece_dir; // { n, ne, e, se, s, sw, w, nw, CURRENT POS } + // std::array prob_piece_dir_constants; + + // switch (orientation) + // { + // case SlipperyType::North: + // actionName = "\t[" + agentName + "turn_at_slip_north"; + // positionGuard = "\t" + agentName + "IsOnSlipperyNorth"; + // prob_piece_dir = { 0, 0, 0, 0, 1, 0, 0, 0, 0 /* <- R */ }; + // prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_turn_displacement" /* <- R */, "prop_zero", "prop_zero", "prop_zero","prop_zero" }; + // break; + + // case SlipperyType::South: + // actionName = "\t[" + agentName + "turn_at_slip_south"; + // positionGuard = "\t" + agentName + "IsOnSlipperySouth"; + // prob_piece_dir = { 1, 0, 0, 0, 0, 0, 0, 0, 0 /* <- R */ }; + // prob_piece_dir_constants = { "prop_turn_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" }; + // break; + + // case SlipperyType::East: + // actionName = "\t[" + agentName + "turn_at_slip_east"; + // positionGuard = "\t" + agentName + "IsOnSlipperyEast"; + // prob_piece_dir = { 0, 0, 0, 0, 0, 0, 1, 0, 0 /* <- R */ }; + // prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_turn_displacement", "prop_zero", "prop_zero" }; + // break; + + // case SlipperyType::West: + // actionName = "\t[" + agentName + "turn_at_slip_west"; + // positionGuard = "\t" + agentName + "IsOnSlipperyWest"; + // prob_piece_dir = { 0, 0, 1, 0, 0, 0, 0, 0, 0 /* <- R */ }; + // prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_turn_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" }; + // break; + // } + + // slipperyActions.insert(actionName + "_left]"); + // slipperyActions.insert(actionName + "_right]"); + + // // override probability to 0 if corresp. direction is blocked + + // for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS - 1; i++) { + // if (!neighborhood.at(i)) + // prob_piece_dir.at(i) = 0; + // } + + // // determine residual probability (R) by replacing 0 with (1 - overall sum) + + // prob_piece_dir.at(remainPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0); + // prob_piece_dir_constants.at(remainPosIndex) = "prop_turn_intended"; + // // + // { + // assert(prob_piece_dir.at(remainPosIndex) <= 9 && prob_piece_dir.at(remainPosIndex) >= 6 && "Value not in Range!"); + // assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!"); + // } + // // + + // // generic output (for every view transition) + // for (std::size_t v = 0; v < viewTransition.size(); v++) { + // os << actionName << std::get<2>(viewTransition.at(v)) << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << std::get<0>(viewTransition.at(v)); + // for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { + // if (i == remainPosIndex) { + // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants.at(i) << " : " << positionTransition.at(i) << std::get<1>(viewTransition.at(v)) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); + // } else { + // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); + // } + // } + // } return os; } std::ostream& PrismModulesPrinter::printSlipperyMove(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation) { - constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 8; - - std::array positionTransition = { - /* north */ "(y" + agentName + "'=y" + agentName + "-1)", - /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)", - /* east */ "(x" + agentName + "'=x" + agentName + "+1)", - /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)", - /* south */ "(y" + agentName + "'=y" + agentName + "+1)", - /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)", - /* west */ "(x" + agentName + "'=x" + agentName + "-1)", - /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)" - }; - - // direction specifics - - std::size_t straightPosIndex, straightPosIndex_east, straightPosIndex_south, straightPosIndex_west, straightPosIndex_north; - std::string actionName, specialTransition; // if straight ahead is blocked - std::string positionGuard; - std::array prob_piece_dir; // { n, ne, e, se, s, sw, w, nw } - std::array prob_piece_dir_agent_north; // { n, ne, e, se, s, sw, w, nw } - std::array prob_piece_dir_agent_east; // { n, ne, e, se, s, sw, w, nw } - std::array prob_piece_dir_agent_south; // { n, ne, e, se, s, sw, w, nw } - std::array prob_piece_dir_agent_west; // { n, ne, e, se, s, sw, w, nw } - - std::array prob_piece_dir_constants; - std::array prob_piece_dir_constants_agent_north; - std::array prob_piece_dir_constants_agent_east; - std::array prob_piece_dir_constants_agent_south; - std::array prob_piece_dir_constants_agent_west; - - switch (orientation) - { - case SlipperyType::North: - actionName = "\t[" + agentName + "move_on_slip_north]"; - positionGuard = "\t" + agentName + "IsOnSlipperyNorth"; - prob_piece_dir = { 0, 0, 1, 2, 0 /* <- R */, 2, 1, 0 }; - - prob_piece_dir_agent_south = { 0 , 0, 0, 1, 0 /*s <- R */, 1, 0, 0}; - prob_piece_dir_agent_east = { 0, 0, 0 /*e <- R */, 2, 0, 0, 0, 0 }; - prob_piece_dir_agent_north = { 0 /*n <- R */, 0, 0, 0, 2 , 0, 0, 0 }; - prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 2, 0 /* <- R */, 0 }; - - prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero" }; - - prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_zero" }; - prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; - prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; - prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement", "prop_zero", "prop_zero" } ; - - - straightPosIndex = 4; - straightPosIndex_east = 2; - straightPosIndex_south = 4; - straightPosIndex_west = 6; - straightPosIndex_north = 0; - - specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)"); - break; + //constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 8; + + //std::array positionTransition = { + // /* north */ "(y" + agentName + "'=y" + agentName + "-1)", + // /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)", + // /* east */ "(x" + agentName + "'=x" + agentName + "+1)", + // /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)", + // /* south */ "(y" + agentName + "'=y" + agentName + "+1)", + // /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)", + // /* west */ "(x" + agentName + "'=x" + agentName + "-1)", + // /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)" + //}; + + //// direction specifics + + //std::size_t straightPosIndex, straightPosIndex_east, straightPosIndex_south, straightPosIndex_west, straightPosIndex_north; + //std::string actionName, specialTransition; // if straight ahead is blocked + //std::string positionGuard; + //std::array prob_piece_dir; // { n, ne, e, se, s, sw, w, nw } + //std::array prob_piece_dir_agent_north; // { n, ne, e, se, s, sw, w, nw } + //std::array prob_piece_dir_agent_east; // { n, ne, e, se, s, sw, w, nw } + //std::array prob_piece_dir_agent_south; // { n, ne, e, se, s, sw, w, nw } + //std::array prob_piece_dir_agent_west; // { n, ne, e, se, s, sw, w, nw } + + //std::array prob_piece_dir_constants; + //std::array prob_piece_dir_constants_agent_north; + //std::array prob_piece_dir_constants_agent_east; + //std::array prob_piece_dir_constants_agent_south; + //std::array prob_piece_dir_constants_agent_west; + + //switch (orientation) + //{ + // case SlipperyType::North: + // actionName = "\t[" + agentName + "move_on_slip_north]"; + // positionGuard = "\t" + agentName + "IsOnSlipperyNorth"; + // prob_piece_dir = { 0, 0, 1, 2, 0 /* <- R */, 2, 1, 0 }; + + // prob_piece_dir_agent_south = { 0 , 0, 0, 1, 0 /*s <- R */, 1, 0, 0}; + // prob_piece_dir_agent_east = { 0, 0, 0 /*e <- R */, 2, 0, 0, 0, 0 }; + // prob_piece_dir_agent_north = { 0 /*n <- R */, 0, 0, 0, 2 , 0, 0, 0 }; + // prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 2, 0 /* <- R */, 0 }; + + // prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero" }; + + // prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_zero" }; + // prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; + // prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; + // prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement", "prop_zero", "prop_zero" } ; + + + // straightPosIndex = 4; + // straightPosIndex_east = 2; + // straightPosIndex_south = 4; + // straightPosIndex_west = 6; + // straightPosIndex_north = 0; + + // specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)"); + // break; + + // case SlipperyType::South: + // actionName = "\t[" + agentName + "move_on_slip_south]"; + // positionGuard = "\t" + agentName + "IsOnSlipperySouth"; + + // prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 }; + // // { n, ne, e, se, s, sw, w, nw } + // prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; + // prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; + // prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; + // prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; + + // prob_piece_dir_constants = { "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement" }; + + // prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement * 1/2" }; + // prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; + // prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; + // prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement" } ; + + + // straightPosIndex = 0; // always north + // straightPosIndex_east = 2; + // straightPosIndex_south = 4; + // straightPosIndex_west = 6; + // straightPosIndex_north = 0; + // specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)"); + // break; + + // case SlipperyType::East: + // actionName = "\t[" + agentName + "move_on_slip_east]"; + // positionGuard = "\t" + agentName + "IsOnSlipperyEast"; + // // { n, ne, e, se, s, sw, w, nw } + + // prob_piece_dir = { 1, 0, 0, 0, 1, 2, 0 /* <- R */, 2 }; + // // TODO + // prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; + // prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; + // prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; + // prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; + + // prob_piece_dir_constants = { "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement" }; + + // prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2" }; + // prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_displacement", "prop_zero" }; + // prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" } ; + // prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2" } ; + + + // straightPosIndex = 6; + // straightPosIndex_east = 2; + // straightPosIndex_south = 4; + // straightPosIndex_west = 6; + // straightPosIndex_north = 0; + + // specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)"); + // break; + + // case SlipperyType::West: + // actionName = "\t[" + agentName + "move_on_slip_west]"; + // positionGuard = "\t" + agentName + "IsOnSlipperyWest"; + // prob_piece_dir = { 1, 2, 0 /* <- R */, 2, 1, 0, 0, 0 }; + // // TODO + // // { n, ne, e, se, s, sw, w, nw } + + // prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; + // prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; + // prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; + // prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; + + // prob_piece_dir_constants = {"prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero","prop_zero", "prop_zero" }; + + // prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; + // prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; + // prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; + // prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_displacement", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; + + + // straightPosIndex = 2; + // straightPosIndex_east = 2; + // straightPosIndex_south = 4; + // straightPosIndex_west = 6; + // straightPosIndex_north = 0; + // specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)"); + // break; + //} + + //slipperyActions.insert(actionName); + + //// override probability to 0 if corresp. direction is blocked + + //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { + // if (!neighborhood.at(i)) + // prob_piece_dir.at(i) = 0; + //} + + //// determine residual probability (R) by replacing 0 with (1 - overall sum) + //prob_piece_dir.at(straightPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0); + //prob_piece_dir_constants.at(straightPosIndex) = "prop_intended"; + //prob_piece_dir_constants_agent_east.at(straightPosIndex_east) = "prop_intended"; + //prob_piece_dir_constants_agent_south.at(straightPosIndex_south) = "prop_intended"; + //prob_piece_dir_constants_agent_west.at(straightPosIndex_west) = "prop_intended"; + //prob_piece_dir_constants_agent_north.at(straightPosIndex_north) = "prop_intended"; + //// + //{ + // assert(prob_piece_dir.at(straightPosIndex) <= 9 && prob_piece_dir.at(straightPosIndex) >= 3 && "Value not in Range!"); + // assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!"); + // assert(orientation != SlipperyType::North || (prob_piece_dir.at(7) == 0 && prob_piece_dir.at(0) == 0 && prob_piece_dir.at(1) == 0 && "Slippery up should be impossible!")); + // assert(orientation != SlipperyType::South || (prob_piece_dir.at(3) == 0 && prob_piece_dir.at(4) == 0 && prob_piece_dir.at(5) == 0 && "Slippery down should be impossible!")); + // assert(orientation != SlipperyType::East || (prob_piece_dir.at(1) == 0 && prob_piece_dir.at(2) == 0 && prob_piece_dir.at(3) == 0 && "Slippery right should be impossible!")); + // assert(orientation != SlipperyType::West || (prob_piece_dir.at(5) == 0 && prob_piece_dir.at(6) == 0 && prob_piece_dir.at(7) == 0 && "Slippery left should be impossible!")); + //} + //// + + //// special case: straight forward is blocked (then remain in same position) + + //positionTransition.at(straightPosIndex) = specialTransition; + + //// generic output (for every view and every possible view direction) + //size_t current_dir = 0; // East + //os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; + + //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { + // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_east.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); + //} + + //current_dir = 1; // South + //os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; + + //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { + // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_south.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); + //} + + //current_dir = 2; // West + + //os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; + //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { + // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_west.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); + //} - case SlipperyType::South: - actionName = "\t[" + agentName + "move_on_slip_south]"; - positionGuard = "\t" + agentName + "IsOnSlipperySouth"; - prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 }; - // { n, ne, e, se, s, sw, w, nw } - prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; - prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; - prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; - prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; + //current_dir = 3; // North - prob_piece_dir_constants = { "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement" }; - - prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement * 1/2" }; - prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; - prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; - prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement" } ; - - - straightPosIndex = 0; // always north - straightPosIndex_east = 2; - straightPosIndex_south = 4; - straightPosIndex_west = 6; - straightPosIndex_north = 0; - specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)"); - break; - - case SlipperyType::East: - actionName = "\t[" + agentName + "move_on_slip_east]"; - positionGuard = "\t" + agentName + "IsOnSlipperyEast"; - // { n, ne, e, se, s, sw, w, nw } - - prob_piece_dir = { 1, 0, 0, 0, 1, 2, 0 /* <- R */, 2 }; - // TODO - prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; - prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; - prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; - prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; - - prob_piece_dir_constants = { "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement" }; - - prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2" }; - prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_displacement", "prop_zero" }; - prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" } ; - prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2" } ; - - - straightPosIndex = 6; - straightPosIndex_east = 2; - straightPosIndex_south = 4; - straightPosIndex_west = 6; - straightPosIndex_north = 0; - - specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)"); - break; - - case SlipperyType::West: - actionName = "\t[" + agentName + "move_on_slip_west]"; - positionGuard = "\t" + agentName + "IsOnSlipperyWest"; - prob_piece_dir = { 1, 2, 0 /* <- R */, 2, 1, 0, 0, 0 }; - // TODO - // { n, ne, e, se, s, sw, w, nw } - - prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; - prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; - prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; - prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; - - prob_piece_dir_constants = {"prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero","prop_zero", "prop_zero" }; - - prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; - prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; - prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; - prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_displacement", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; - - - straightPosIndex = 2; - straightPosIndex_east = 2; - straightPosIndex_south = 4; - straightPosIndex_west = 6; - straightPosIndex_north = 0; - specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)"); - break; - } - - slipperyActions.insert(actionName); - - // override probability to 0 if corresp. direction is blocked - - for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - if (!neighborhood.at(i)) - prob_piece_dir.at(i) = 0; - } - - // determine residual probability (R) by replacing 0 with (1 - overall sum) - if(enforceOneWays) { - prob_piece_dir = {0,0,0,0,0,0,0,0}; - prob_piece_dir_constants = {"zero","zero","zero","zero","zero","zero","zero","zero"}; - } - prob_piece_dir.at(straightPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0); - prob_piece_dir_constants.at(straightPosIndex) = "prop_intended"; - prob_piece_dir_constants_agent_east.at(straightPosIndex_east) = "prop_intended"; - prob_piece_dir_constants_agent_south.at(straightPosIndex_south) = "prop_intended"; - prob_piece_dir_constants_agent_west.at(straightPosIndex_west) = "prop_intended"; - prob_piece_dir_constants_agent_north.at(straightPosIndex_north) = "prop_intended"; - // - { - assert(prob_piece_dir.at(straightPosIndex) <= 9 && prob_piece_dir.at(straightPosIndex) >= 3 && "Value not in Range!"); - assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!"); - assert(orientation != SlipperyType::North || (prob_piece_dir.at(7) == 0 && prob_piece_dir.at(0) == 0 && prob_piece_dir.at(1) == 0 && "Slippery up should be impossible!")); - assert(orientation != SlipperyType::South || (prob_piece_dir.at(3) == 0 && prob_piece_dir.at(4) == 0 && prob_piece_dir.at(5) == 0 && "Slippery down should be impossible!")); - assert(orientation != SlipperyType::East || (prob_piece_dir.at(1) == 0 && prob_piece_dir.at(2) == 0 && prob_piece_dir.at(3) == 0 && "Slippery right should be impossible!")); - assert(orientation != SlipperyType::West || (prob_piece_dir.at(5) == 0 && prob_piece_dir.at(6) == 0 && prob_piece_dir.at(7) == 0 && "Slippery left should be impossible!")); - } - // - - // special case: straight forward is blocked (then remain in same position) - - positionTransition.at(straightPosIndex) = specialTransition; - - // generic output (for every view and every possible view direction) - size_t current_dir = 0; // East - os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - - for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_east.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - } - - current_dir = 1; // South - os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - - for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_south.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - } - - current_dir = 2; // West - - os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_west.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - } - - - current_dir = 3; // North - - os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_north.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - } + //os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; + //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { + // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_north.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); + //} return os; } @@ -810,11 +817,12 @@ namespace prism { return os; } - std::string PrismModulesPrinter::moveGuard(const size_t &agentIndex) { - return isGame() ? " move=" + std::to_string(agentIndex) + " & " : " "; + std::string PrismModulesPrinter::moveGuard(const AgentName &agentName) { + return isGame() ? " move=" + std::to_string(agentIndexMap[agentName]) + " & " : " "; } - std::string PrismModulesPrinter::moveUpdate(const size_t &agentIndex) { + std::string PrismModulesPrinter::moveUpdate(const AgentName &agentName) { + size_t agentIndex = agentIndexMap[agentName]; return isGame() ? (agentIndex == numberOfPlayer - 1) ? " & (move'=0) " : diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 460aa67..626d06c 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -9,7 +9,7 @@ namespace prism { class PrismModulesPrinter { public: - PrismModulesPrinter(std::ostream &os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const bool enforceOneWays = false); + PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float &faultyProbability); std::ostream& print(); @@ -28,6 +28,9 @@ namespace prism { void printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier); void printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key); + void printMovementActionsForRobot(const std::string &a); + void printTurningActionsForRobot(const std::string &a); + std::ostream& printConstants(std::ostream &os, const std::vector &constants); /* * Representation for Slippery Tile. @@ -70,7 +73,7 @@ namespace prism { const std::vector &probabilities = {}, const double faultyProbability = 0); std::ostream& printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability = 1.0, const double &stickyProbability = 0.0); - std::ostream& printDoneActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex); + std::ostream& printDoneActions(std::ostream &os, const AgentName &agentName); std::ostream& printEndmodule(std::ostream &os); std::ostream& printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector &probabilities = {}, const std::set &slipperyActions = {}); std::ostream& printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer); @@ -79,20 +82,24 @@ namespace prism { std::ostream& printConfiguration(std::ostream &os, const std::vector& configurations); std::ostream& printConfiguredActions(std::ostream &os, const AgentName &agentName); - std::string moveGuard(const size_t &agentIndex); + std::string moveGuard(const AgentName &agentName); std::string pickupGuard(const AgentName &agentName, const std::string keyColor); std::string dropGuard(const AgentName &agentName, const std::string keyColor, size_t view); - std::string moveUpdate(const size_t &agentIndex); + std::string moveUpdate(const AgentName &agentName); - std::string viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView); + std::string viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView = true); bool isGame() const; private: + std::string printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection); + std::string printMovementUpdate(const AgentName &a, const std::string &update); + + std::ostream &os; std::stringstream actionStream; - ModelType const& modelType; - coordinates const& maxBoundaries; + ModelType const &modelType; + coordinates const &maxBoundaries; AgentName agentName; cells boxes; cells balls; @@ -101,8 +108,9 @@ namespace prism { cells keys; AgentNameAndPositionMap agentNameAndPositionMap; + std::map agentIndexMap; size_t numberOfPlayer; - bool enforceOneWays; + float const &faultyProbability; std::vector configuration; std::map viewDirectionMapping; }; From 7faa8d1efb461153d051d86748bbfdde56cd9293 Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 12:22:30 +0100 Subject: [PATCH 14/38] added actionId and update typedefs --- util/PrismPrinter.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/util/PrismPrinter.h b/util/PrismPrinter.h index 0d28af0..397cfa9 100644 --- a/util/PrismPrinter.h +++ b/util/PrismPrinter.h @@ -12,6 +12,9 @@ typedef AgentNameAndPosition KeyNameAndPosition; typedef std::map AgentNameAndPositionMap; typedef std::map KeyNameAndPositionMap; typedef std::pair CellAndCondition; +typedef std::pair update; +typedef std::map updates; +typedef int8_t ActionId; std::string capitalize(std::string string); From 3999e778fe3d071b629ed14caa16fce5f0b00586 Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 12:22:49 +0100 Subject: [PATCH 15/38] added turn actions for robots also removed some old methods --- util/PrismModulesPrinter.cpp | 182 +++++++++++++++++------------------ util/PrismModulesPrinter.h | 36 +++---- 2 files changed, 109 insertions(+), 109 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 7886c89..968d46c 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -3,9 +3,14 @@ #include #include +#define NOFAULT -1 +#define LEFT 0 +#define RIGHT 1 +#define FORWARD 2 + namespace prism { - PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float &faultyProbability) + PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float faultyProbability) : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), agentNameAndPositionMap(agentNameAndPositionMap), configuration(config), faultyProbability(faultyProbability), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) { numberOfPlayer = agentNameAndPositionMap.size(); size_t index = 0; @@ -68,70 +73,6 @@ namespace prism { return os; } - - std::ostream& PrismModulesPrinter::printBooleansForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) { - for(auto const& key : keys) { - os << "\t" << agentName << "_has_"<< key.getColor() << "_key : bool;\n";//init false;\n"; - } - os << "\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printBooleansForBackground(std::ostream &os, const AgentName &agentName, const std::map &backgroundTiles) { - for(auto const& [color, cells] : backgroundTiles) { - if(cells.size() == 0) continue; - std::string c = getColor(color); - c.at(0) = std::toupper(c.at(0)); - os << "\t" << agentName << "_picked_up_" << c << " : bool init false;\n"; - } - os << "\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printActionsForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) { - for(auto const& key : keys) { // TODO ADD Drop action and enforce that pickup only possible if pockets empty (nothing picked up already) - os << "\n"; - std::string keyColor = key.getColor(); - os << "\t[pickup_" << keyColor << "_key]\t" << pickupGuard(agentName, keyColor) << "-> "; - os << "(" << agentName << "_has_" << keyColor << "_key'=true) & (" << agentName << "_is_carrying_object'=true);\n"; - os << "\n"; - - os << "\t[drop_" << keyColor << "_key_north]\t" << dropGuard(agentName, keyColor, 3) << "-> "; - os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n"; - - os << "\t[drop_" << keyColor << "_key_west]\t" << dropGuard(agentName, keyColor, 2) << "-> "; - os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n"; - - os << "\t[drop_" << keyColor << "_key_south]\t" << dropGuard(agentName, keyColor, 1) << "-> "; - os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n"; - - os << "\t[drop_" << keyColor << "_key_east]\t" << dropGuard(agentName, keyColor, 0) << "-> "; - os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n"; - } - - return os; - } - - std::string PrismModulesPrinter::pickupGuard(const AgentName &agentName, const std::string keyColor ) { - return "!" + agentName + "_is_carrying_object &\t" + agentName + "CanPickUp" + keyColor + "Key "; - } - - std::string PrismModulesPrinter::dropGuard(const AgentName &agentName, const std::string keyColor, size_t view) { - return viewVariable(agentName, view, true) + "\t!" + agentName + "CannotMove" + viewDirectionMapping.at(view) + "\t&\t" + agentName + "_has_" + keyColor + "_key\t"; - } - - std::ostream& PrismModulesPrinter::printActionsForBackground(std::ostream &os, const AgentName &agentName, const std::map &backgroundTiles) { - for(auto const& [color, cells] : backgroundTiles) { - if(cells.size() == 0) continue; - std::string c = getColor(color); - c.at(0) = std::toupper(c.at(0)); - os << "\t[" << agentName << "_pickup_" << c << "] " << agentName << "On" << c << " & !" << agentName << "_picked_up_" << c << " -> "; - os << "(" << agentName << "_picked_up_" << c << "'=true);\n"; - } - os << "\n"; - return os; - } - std::ostream& PrismModulesPrinter::printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType) { /* os << "init\n"; @@ -176,7 +117,7 @@ namespace prism { return os; } - std::ostream& PrismModulesPrinter::printModule(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &boundaries, const coordinates& initialPosition, const cells &keys, const std::map &backgroundTiles, const bool agentWithView, const std::vector &probabilities, const double faultyProbability) { + std::ostream& PrismModulesPrinter::printModule(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &boundaries, const coordinates& initialPosition, const cells &keys, const std::map &backgroundTiles, const bool agentWithView, const std::vector &probabilities, const float faultyProbability) { /* os << "module " << agentName << "\n"; os << "\tx" << agentName << " : [1.." << boundaries.second << "];\n"; @@ -276,6 +217,9 @@ namespace prism { os << "\ty" << agentName << " : [0.." << maxBoundaries.first << "] init " << initialPosition.first << ";\n"; os << "\tview" << agentName << " : [0..3] init 1;\n"; + if(faultyBehaviour()) os << "\tpreviousAction" << agentName << " : [-1..2] init -1;\n"; + + printTurnActionsForRobot(agentName); printMovementActionsForRobot(agentName); for(const auto &door : unlockedDoors) { @@ -295,51 +239,83 @@ namespace prism { printPortableObjectActionsForRobot(agentName, identifier); } + for(const auto &ball : balls) { + std::string identifier = capitalize(ball.getColor()) + ball.getType(); + os << "\t" << agentName << "Carrying" << identifier << " : bool init false;\n"; + printPortableObjectActionsForRobot(agentName, identifier); + } + + for(const auto &box : boxes) { + std::string identifier = capitalize(box.getColor()) + box.getType(); + os << "\t" << agentName << "Carrying" << identifier << " : bool init false;\n"; + printPortableObjectActionsForRobot(agentName, identifier); + } + os << "\n" << actionStream.str(); actionStream.str(std::string()); os << "endmodule\n\n"; } void PrismModulesPrinter::printPortableObjectActionsForRobot(const std::string &a, const std::string &i) { - actionStream << "\t[" << a << "_pickup_" << i << "] " << a << "CannotMove" << i << " -> (" << a << "Carrying" << i << "'=true);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_north]\t" << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_west] \t" << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_south]\t" << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_east] \t" << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_pickup_" << i << "] " << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << " !" << a << "IsCarrying & " << a << "CannotMove" << i << " -> (" << a << "Carrying" << i << "'=true);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_north]\t" << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_west] \t" << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_south]\t" << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_east] \t" << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n"; actionStream << "\n"; } void PrismModulesPrinter::printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier) { - actionStream << "\t[" << agentName << "_open_" << identifier << "] " << agentName << "CannotMove" << identifier << " -> true;\n"; - actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " -> true;\n"; + actionStream << "\t[" << agentName << "_open_" << identifier << "] " << faultyBehaviourGuard(agentName, NOFAULT) << moveGuard(agentName) << agentName << "CannotMove" << identifier << " -> true;\n"; + actionStream << "\t[" << agentName << "_close_" << identifier << "] " << faultyBehaviourGuard(agentName, NOFAULT) << moveGuard(agentName) << agentName << "IsNextTo" << identifier << " -> true;\n"; actionStream << "\n"; } void PrismModulesPrinter::printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key) { - actionStream << "\t[" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; - actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; + actionStream << "\t[" << agentName << "_unlock_" << identifier << "] " << faultyBehaviourGuard(agentName, NOFAULT) << moveGuard(agentName) << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; + actionStream << "\t[" << agentName << "_close_" << identifier << "] " << faultyBehaviourGuard(agentName, NOFAULT) << moveGuard(agentName) << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; actionStream << "\n"; } - void PrismModulesPrinter::printTurningActionsForRobot(const AgentName &a) { - + void PrismModulesPrinter::printTurnActionsForRobot(const AgentName &a) { + actionStream << printTurnGuard(a, "right", RIGHT, "true") << printTurnUpdate(a, {1.0, "(view"+a+"'=mod(view"+a+"+1,4))"}, RIGHT); + actionStream << printTurnGuard(a, "left", LEFT, "view"+a+">0") << printTurnUpdate(a, {1.0, "(view"+a+"'=view"+a+"-1)"}, LEFT); + actionStream << printTurnGuard(a, "left", LEFT, "view"+a+"=0") << printTurnUpdate(a, {1.0, "(view"+a+"'=3)"}, LEFT); } void PrismModulesPrinter::printMovementActionsForRobot(const AgentName &a) { - if(faultyProbability <= 0.0) { - actionStream << printMovementGuard(a, "North", 3) << printMovementUpdate(a, "y" + a + "'=y" + a + "-1"); - actionStream << printMovementGuard(a, "East", 0) << printMovementUpdate(a, "x" + a + "'=x" + a + "+1"); - actionStream << printMovementGuard(a, "South", 1) << printMovementUpdate(a, "y" + a + "'=y" + a + "+1"); - actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, "x" + a + "'=x" + a + "-1"); + actionStream << printMovementGuard(a, "North", 3) << printMovementUpdate(a, {1.0, "(y"+a+"'=y"+a+"-1)"}); + actionStream << printMovementGuard(a, "East", 0) << printMovementUpdate(a, {1.0, "(x"+a+"'=x"+a+"+1)"}); + actionStream << printMovementGuard(a, "South", 1) << printMovementUpdate(a, {1.0, "(y"+a+"'=y"+a+"+1)"}); + actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, {1.0, "(x"+a+"'=x"+a+"-1)"}); + } + + std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) const { + return "\t[" + a + "_move_" + direction + "]" + moveGuard(a) + viewVariable(a, 3) + faultyBehaviourGuard(a, FORWARD) + " !" + a + "IsOnSlippery & !" + a + "IsOnLava & !" + a + "IsOnGoal & !" + a + "CannotMove" + direction + "Wall -> "; + } + + std::string PrismModulesPrinter::printMovementUpdate(const AgentName &a, const update &u) const { + if(!faultyBehaviour()) { + return updateToString(u) + ";\n"; + } else { + update nonFaultyUpdate = {u.first - faultyProbability, u.second + " & " + faultyBehaviourUpdate(a, NOFAULT)}; + update faultyUpdate = {faultyProbability, u.second + " & " + faultyBehaviourUpdate(a, FORWARD)}; + return updateToString(nonFaultyUpdate) + " + " + updateToString(faultyUpdate) + ";\n"; } } - std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) { - return "\t[" + a + "_move_" + direction + "]" + moveGuard(a) + viewVariable(a, 3) + " !" + a + "IsOnSlippery & !" + a + "IsOnLava & !" + a + "IsOnGoal & !" + a + "CannotMove" + direction + "Wall -> "; + std::string PrismModulesPrinter::printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond) const { + return "\t[" + a + "_turn_" + direction + "]" + moveGuard(a) + faultyBehaviourGuard(a, actionId) + cond + " -> "; } - std::string PrismModulesPrinter::printMovementUpdate(const AgentName &a, const std::string &update) { - return "(" + update + ") & " + moveUpdate(a) + ";\n"; + std::string PrismModulesPrinter::printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const { + if(!faultyBehaviour()) { + return updateToString(u) + ";\n"; + } else { + update nonFaultyUpdate = {u.first - faultyProbability, u.second + " & " + faultyBehaviourUpdate(a, NOFAULT)}; + update faultyUpdate = {faultyProbability, u.second + " & " + faultyBehaviourUpdate(a, actionId)}; + return updateToString(nonFaultyUpdate) + " + " + updateToString(faultyUpdate) + ";\n"; + } } std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) { @@ -817,12 +793,24 @@ namespace prism { return os; } - std::string PrismModulesPrinter::moveGuard(const AgentName &agentName) { - return isGame() ? " move=" + std::to_string(agentIndexMap[agentName]) + " & " : " "; + std::string PrismModulesPrinter::moveGuard(const AgentName &agentName) const { + return isGame() ? " move=" + std::to_string(agentIndexMap.at(agentName)) + " & " : " "; + } + + std::string PrismModulesPrinter::faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const { + if(actionId == NOFAULT) { + return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + ") & "; + } else { + return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + " | previousAction" + agentName + "=" + std::to_string(actionId) + ") & "; + } } - std::string PrismModulesPrinter::moveUpdate(const AgentName &agentName) { - size_t agentIndex = agentIndexMap[agentName]; + std::string PrismModulesPrinter::faultyBehaviourUpdate(const AgentName &agentName, const ActionId &actionId) const { + return "(previousAction" + agentName + "'=" + std::to_string(actionId) + ")"; + } + + std::string PrismModulesPrinter::moveUpdate(const AgentName &agentName) const { + size_t agentIndex = agentIndexMap.at(agentName); return isGame() ? (agentIndex == numberOfPlayer - 1) ? " & (move'=0) " : @@ -830,10 +818,22 @@ namespace prism { ""; } - std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView) { + std::string PrismModulesPrinter::updateToString(const update &u) const { + return std::to_string(u.first) + ": " + u.second; + } + + std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView) const { return agentWithView ? " view" + agentName + "=" + std::to_string(agentDirection) + " & " : " "; } + bool PrismModulesPrinter::anyPortableObject() const { + return !keys.empty() || !boxes.empty() || !balls.empty(); + } + + bool PrismModulesPrinter::faultyBehaviour() const { + return faultyProbability > 0.0f; + } + bool PrismModulesPrinter::isGame() const { return modelType == ModelType::SMG; } diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 626d06c..425283e 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -9,7 +9,7 @@ namespace prism { class PrismModulesPrinter { public: - PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float &faultyProbability); + PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float faultyProbability); std::ostream& print(); @@ -24,12 +24,10 @@ namespace prism { void printRobotModule(const AgentName &agentName, const coordinates &initialPosition); void printPortableObjectActionsForRobot(const std::string &agentName, const std::string &identifier); - void printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier); void printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key); - void printMovementActionsForRobot(const std::string &a); - void printTurningActionsForRobot(const std::string &a); + void printTurnActionsForRobot(const std::string &a); std::ostream& printConstants(std::ostream &os, const std::vector &constants); /* @@ -57,10 +55,6 @@ namespace prism { */ std::ostream& printSlipperyTurn(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation); - std::ostream& printBooleansForKeys(std::ostream &os, const AgentName &agentName, const cells &keys); - std::ostream& printActionsForKeys(std::ostream &os, const AgentName &agentName, const cells &keys); - std::ostream& printBooleansForBackground(std::ostream &os, const AgentName &agentName, const std::map &backgroundTiles); - std::ostream& printActionsForBackground(std::ostream &os, const AgentName &agentName, const std::map &backgroundTiles); std::ostream& printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType); std::ostream& printModule(std::ostream &os, const AgentName &agentName, @@ -71,7 +65,7 @@ namespace prism { const std::map &backgroundTiles, const bool agentWithView, const std::vector &probabilities = {}, - const double faultyProbability = 0); + const float faultyProbability = 0); std::ostream& printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability = 1.0, const double &stickyProbability = 0.0); std::ostream& printDoneActions(std::ostream &os, const AgentName &agentName); std::ostream& printEndmodule(std::ostream &os); @@ -82,17 +76,23 @@ namespace prism { std::ostream& printConfiguration(std::ostream &os, const std::vector& configurations); std::ostream& printConfiguredActions(std::ostream &os, const AgentName &agentName); - std::string moveGuard(const AgentName &agentName); - std::string pickupGuard(const AgentName &agentName, const std::string keyColor); - std::string dropGuard(const AgentName &agentName, const std::string keyColor, size_t view); - std::string moveUpdate(const AgentName &agentName); - - std::string viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView = true); bool isGame() const; private: - std::string printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection); - std::string printMovementUpdate(const AgentName &a, const std::string &update); + std::string printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) const; + std::string printMovementUpdate(const AgentName &a, const update &update) const; + std::string printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond = "") const; + std::string printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const; + + bool anyPortableObject() const; + bool faultyBehaviour() const; + std::string moveGuard(const AgentName &agentName) const; + std::string faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const; + std::string faultyBehaviourUpdate(const AgentName &agentName, const ActionId &actionId) const; + std::string moveUpdate(const AgentName &agentName) const; + std::string updateToString(const update &u) const; + + std::string viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView = true) const; std::ostream &os; @@ -110,7 +110,7 @@ namespace prism { AgentNameAndPositionMap agentNameAndPositionMap; std::map agentIndexMap; size_t numberOfPlayer; - float const &faultyProbability; + float const faultyProbability; std::vector configuration; std::map viewDirectionMapping; }; From 13974739d9692937b775c06eb6ec9bf1de4f7564 Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 16:58:14 +0100 Subject: [PATCH 16/38] commit to (col, row) --- util/Grid.cpp | 31 ++----------------------------- util/Grid.h | 8 ++------ util/cell.cpp | 10 +++++----- util/cell.h | 8 ++++---- 4 files changed, 13 insertions(+), 44 deletions(-) diff --git a/util/Grid.cpp b/util/Grid.cpp index cdd07eb..7a4cbfb 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -89,43 +89,16 @@ cells Grid::getGridCells() { } bool Grid::isBlocked(coordinates p) { - return isWall(p); //|| isLockedDoor(p) || isKey(p); + return isWall(p); } bool Grid::isWall(coordinates p) { return std::find_if(walls.begin(), walls.end(), [p](cell cell) { - return cell.row == p.first && cell.column == p.second; + return cell.row == p.second && cell.column == p.first; }) != walls.end(); } -bool Grid::isLockedDoor(coordinates p) { - return std::find_if(lockedDoors.begin(), lockedDoors.end(), - [p](cell cell) { - return cell.row == p.first && cell.column == p.second; - }) != lockedDoors.end(); -} - -bool Grid::isUnlockedDoor(coordinates p) { - return std::find_if(unlockedDoors.begin(), unlockedDoors.end(), - [p](cell cell) { - return cell.row == p.first && cell.column == p.second; - }) != unlockedDoors.end(); -} - -bool Grid::isKey(coordinates p) { - return std::find_if(keys.begin(), keys.end(), - [p](cell cell) { - return cell.row == p.first && cell.column == p.second; - }) != keys.end(); -} - -bool Grid::isBox(coordinates p) { - return std::find_if(boxes.begin(), boxes.end(), - [p](cell cell) { - return cell.row == p.first && cell.column == p.second; - }) != boxes.end(); -} void Grid::applyOverwrites(std::string& str, std::vector& configuration) { for (auto& config : configuration) { diff --git a/util/Grid.h b/util/Grid.h index 80f5055..e33d34e 100644 --- a/util/Grid.h +++ b/util/Grid.h @@ -22,16 +22,12 @@ struct GridOptions { class Grid { public: - Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map &stateRewards = {}, const double faultyProbability = 0); + Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map &stateRewards = {}, const float faultyProbability = 0); cells getGridCells(); bool isBlocked(coordinates p); bool isWall(coordinates p); - bool isLockedDoor(coordinates p); - bool isUnlockedDoor(coordinates p); - bool isKey(coordinates p); - bool isBox(coordinates p); void printToPrism(std::ostream &os, std::vector& configuration, const prism::ModelType& modelType); void applyOverwrites(std::string& str, std::vector& configuration); @@ -69,5 +65,5 @@ class Grid { std::map backgroundTiles; std::map stateRewards; - double faultyProbability; + float faultyProbability; }; diff --git a/util/cell.cpp b/util/cell.cpp index df20997..c6a72a1 100644 --- a/util/cell.cpp +++ b/util/cell.cpp @@ -4,10 +4,14 @@ std::ostream &operator<<(std::ostream &os, const cell &c) { os << static_cast(c.type) << static_cast(c.color); - os << " at (" << c.row << "," << c.column << ")"; + os << " at (" << c.column << "," << c.row << ")"; return os; } +coordinates cell::getCoordinates() const { + return std::make_pair(column, row); +} + cell cell::getNorth(const std::vector &grid) const { auto north = std::find_if(grid.begin(), grid.end(), [this](const cell &c) { return this->row - 1 == c.row && this->column == c.column; @@ -52,10 +56,6 @@ cell cell::getWest(const std::vector &grid) const { return *west; } -coordinates cell::getCoordinates() const { - return std::make_pair(row, column); -} - std::string cell::getColor() const { switch(color) { case Color::Red: return "red"; diff --git a/util/cell.h b/util/cell.h index d1d1169..ca71559 100644 --- a/util/cell.h +++ b/util/cell.h @@ -40,10 +40,10 @@ std::string getColor(Color color); class cell { public: - coordinates getNorth() const { return std::make_pair(row - 1, column); } - coordinates getSouth() const { return std::make_pair(row + 1, column); } - coordinates getEast() const { return std::make_pair(row, column + 1); } - coordinates getWest() const { return std::make_pair(row, column - 1); } + coordinates getNorth() const { return std::make_pair(column, row - 1); } + coordinates getSouth() const { return std::make_pair(column, row + 1); } + coordinates getEast() const { return std::make_pair(column + 1, row); } + coordinates getWest() const { return std::make_pair(column - 1, row); } cell getNorth(const std::vector &grid) const; cell getEast(const std::vector &grid) const; From 14e6375877e32d7489ee2b13bb2a026884cbe080 Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 16:58:42 +0100 Subject: [PATCH 17/38] faultyProbability is float --- util/Grid.cpp | 209 +++++++++++++++++++------------------------------- util/Grid.h | 2 +- 2 files changed, 81 insertions(+), 130 deletions(-) diff --git a/util/Grid.cpp b/util/Grid.cpp index 7a4cbfb..936f715 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -3,23 +3,22 @@ #include -prism::ModelType GridOptions::getModelType() const +prism::ModelType GridOptions::getModelType() const { if (agentsWithView.size() > 1) { return prism::ModelType::SMG; - } - + } return prism::ModelType::MDP; } -Grid::Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map &stateRewards, const double faultyProbability) +Grid::Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map &stateRewards, const float faultyProbability) : allGridCells(gridCells), background(background), gridOptions(gridOptions), stateRewards(stateRewards), faultyProbability(faultyProbability) { cell max = allGridCells.at(allGridCells.size() - 1); maxBoundaries = std::make_pair(max.row - 1, max.column - 1); std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(walls), [](cell c) { return c.type == Type::Wall; }); std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lava), [](cell c) { return c.type == Type::Lava; }); - std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(floor), [](cell c) { return c.type == Type::Floor; }); // TODO Add agent cells to floor + std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(floor), [](cell c) { return c.type == Type::Floor; }); // TODO CHECK IF ALL AGENTS ARE ADDED TO FLOOR std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorth), [](cell c) { return c.type == Type::SlipperyNorth; }); std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyEast), [](cell c) { return c.type == Type::SlipperyEast; }); std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouth), [](cell c) { return c.type == Type::SlipperySouth; }); @@ -106,7 +105,7 @@ void Grid::applyOverwrites(std::string& str, std::vector& configu continue; } size_t start_pos; - + if (config.type_ == ConfigType::Formula) { start_pos = str.find("formula " + config.identifier_); } else if (config.type_ == ConfigType::Label) { @@ -126,155 +125,107 @@ void Grid::applyOverwrites(std::string& str, std::vector& configu size_t end_pos = str.find(';', start_pos) + 1; std::string expression = config.expression_; - + str.replace(start_pos, end_pos - start_pos , expression); } } - void Grid::printToPrism(std::ostream& os, std::vector& configuration ,const prism::ModelType& modelType) { - cells northRestriction; - cells eastRestriction; - cells southRestriction; - cells westRestriction; - cells walkable = floor; - cells conditionallyWalkable; + cells northRestriction, eastRestriction, southRestriction, westRestriction; + cells walkable = floor; walkable.insert(walkable.end(), goals.begin(), goals.end()); walkable.insert(walkable.end(), boxes.begin(), boxes.end()); - walkable.push_back(agent); walkable.insert(walkable.end(), adversaries.begin(), adversaries.end()); walkable.insert(walkable.end(), lava.begin(), lava.end()); - - conditionallyWalkable.insert(conditionallyWalkable.end(), keys.begin(), keys.end()); - conditionallyWalkable.insert(conditionallyWalkable.end(), lockedDoors.begin(), lockedDoors.end()); - conditionallyWalkable.insert(conditionallyWalkable.end(), unlockedDoors.begin(), unlockedDoors.end()); + walkable.insert(walkable.end(), keys.begin(), keys.end()); + walkable.insert(walkable.end(), balls.begin(), balls.end()); for(auto const& c : walkable) { - if(isBlocked(c.getNorth())) northRestriction.push_back(c); - if(isBlocked(c.getEast())) eastRestriction.push_back(c); - if(isBlocked(c.getSouth())) southRestriction.push_back(c); - if(isBlocked(c.getWest())) westRestriction.push_back(c); - } - // TODO Add doors here (list of doors keys etc) - // walkable.insert(walkable.end(), lockedDoors.begin(), lockedDoors.end()); - // walkable.insert(walkable.end(), unlockedDoors.begin(), unlockedDoors.end()); - for(auto const& c : conditionallyWalkable) { - if(isBlocked(c.getNorth())) northRestriction.push_back(c); - if(isBlocked(c.getEast())) eastRestriction.push_back(c); - if(isBlocked(c.getSouth())) southRestriction.push_back(c); - if(isBlocked(c.getWest())) westRestriction.push_back(c); + std::cout << "checking: " << c << " south of c: " << c.getSouth().first << " " << c.getSouth().second << std::endl; + if(isWall(c.getNorth())) northRestriction.push_back(c); + if(isWall(c.getEast())) eastRestriction.push_back(c); + if(isWall(c.getSouth())) southRestriction.push_back(c); + if(isWall(c.getWest())) westRestriction.push_back(c); } - prism::PrismModulesPrinter printer(modelType, agentNameAndPositionMap.size(), configuration, gridOptions.enforceOneWays); - printer.printModel(os, modelType); - if(modelType == prism::ModelType::SMG) { - printer.printGlobalMoveVariable(os, agentNameAndPositionMap.size()); - } - for(auto const &backgroundTilesOfColor : backgroundTiles) { - for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition) { - printer.printBackgroundLabels(os, agentNameAndPosition->first, backgroundTilesOfColor); - } - } - cells noTurnFloor; - if(gridOptions.enforceOneWays) { - for(auto const& c : floor) { - cell east = c.getEast(allGridCells); - cell south = c.getSouth(allGridCells); - cell west = c.getWest(allGridCells); - cell north = c.getNorth(allGridCells); - if( (east.type == Type::Wall && west.type == Type::Wall) or - (north.type == Type::Wall && south.type == Type::Wall) ) { - noTurnFloor.push_back(c); - } - } - } - cells doors; - doors.insert(doors.end(), lockedDoors.begin(), lockedDoors.end()); - doors.insert(doors.end(), unlockedDoors.begin(), unlockedDoors.end()); - for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition) { - printer.printFormulas(os, agentNameAndPosition->first, northRestriction, eastRestriction, southRestriction, westRestriction, { slipperyNorth, slipperyEast, slipperySouth, slipperyWest }, lava, walls, noTurnFloor, slipperyNorth, slipperyEast, slipperySouth, slipperyWest, keys, doors); - printer.printGoalLabel(os, agentNameAndPosition->first, goals); - printer.printKeysLabels(os, agentNameAndPosition->first, keys); - } - std::vector constants {"const double prop_zero = 0/9;", - "const double prop_intended = 6/9;", - "const double prop_turn_intended = 6/9;", - "const double prop_displacement = 3/9;", - "const double prop_turn_displacement = 3/9;", - "const int width = " + std::to_string(maxBoundaries.first) + ";", - "const int height = " + std::to_string(maxBoundaries.second) + ";" - }; - printer.printConstants(os, constants); + std::map wallRestrictions = {{"North", northRestriction}, {"East", eastRestriction}, {"South", southRestriction}, {"West", westRestriction}}; + std::map slipperyTiles = {{"North", slipperyNorth}, {"East", slipperyEast}, {"South", slipperySouth}, {"West", slipperyWest}}; std::vector agentNames; std::transform(agentNameAndPositionMap.begin(), agentNameAndPositionMap.end(), std::back_inserter(agentNames), [](const std::map::value_type &pair){return pair.first;}); - if(modelType == prism::ModelType::SMG) { - printer.printCrashLabel(os, agentNames); - } - size_t agentIndex = 0; - - printer.printInitStruct(os, agentNameAndPositionMap, keyNameAndPositionMap, lockedDoors, unlockedDoors, modelType); - - - for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition, agentIndex++) { - AgentName agentName = agentNameAndPosition->first; - //std::cout << "Agent Name: " << agentName << std::endl; - bool agentWithView = std::find(gridOptions.agentsWithView.begin(), gridOptions.agentsWithView.end(), agentName) != gridOptions.agentsWithView.end(); - bool agentWithProbabilisticBehaviour = std::find(gridOptions.agentsWithProbabilisticBehaviour.begin(), gridOptions.agentsWithProbabilisticBehaviour.end(), agentName) != gridOptions.agentsWithProbabilisticBehaviour.end(); - std::set slipperyActions; // TODO AGENT POSITION INITIALIZATIN - if(agentWithProbabilisticBehaviour) printer.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, gridOptions.probabilitiesForActions); - else printer.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, {} ,faultyProbability); - for(auto const& c : slipperyNorth) { - printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North); - if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North); - - } - for(auto const& c : slipperyEast) { - printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East); - if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East); - } - for(auto const& c : slipperySouth) { - printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South); - if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South); - } - for(auto const& c : slipperyWest) { - printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West); - if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West); - } - - printer.printEndmodule(os); + std::string agentName = agentNames.at(0); - if(modelType == prism::ModelType::SMG) { - if(agentWithProbabilisticBehaviour) printer.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, gridOptions.probabilitiesForActions, slipperyActions); - else printer.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, {}, slipperyActions); - } - //if(!stateRewards.empty()) { - printer.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles); - //} + prism::PrismFormulaPrinter formulas(os, wallRestrictions, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, lava, goals); + prism::PrismModulesPrinter modules(os, modelType, maxBoundaries, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, agentNameAndPositionMap, configuration, faultyProbability); + modules.printModelType(modelType); + for(const auto &agentName : agentNames) { + formulas.print(agentName); } + if(modelType == prism::ModelType::SMG) modules.printGlobalMoveVariable(os, agentNameAndPositionMap.size()); + std::vector constants {"const double prop_zero = 0/9;", + "const double prop_intended = 6/9;", + "const double prop_turn_intended = 6/9;", + "const double prop_displacement = 3/9;", + "const double prop_turn_displacement = 3/9;", + "const int width = " + std::to_string(maxBoundaries.first) + ";", + "const int height = " + std::to_string(maxBoundaries.second) + ";" + }; + modules.printConstants(os, constants); + modules.print(); + + + + + //modules.printInitStruct(os, agentNameAndPositionMap, keyNameAndPositionMap, lockedDoors, unlockedDoors, modelType); + + + //size_t agentIndex = 0; + //for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition, agentIndex++) { + // AgentName agentName = agentNameAndPosition->first; + // //std::cout << "Agent Name: " << agentName << std::endl; + // bool agentWithView = std::find(gridOptions.agentsWithView.begin(), gridOptions.agentsWithView.end(), agentName) != gridOptions.agentsWithView.end(); + // bool agentWithProbabilisticBehaviour = std::find(gridOptions.agentsWithProbabilisticBehaviour.begin(), gridOptions.agentsWithProbabilisticBehaviour.end(), agentName) != gridOptions.agentsWithProbabilisticBehaviour.end(); + // std::set slipperyActions; // TODO AGENT POSITION INITIALIZATIN + // if(agentWithProbabilisticBehaviour) modules.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, gridOptions.probabilitiesForActions); + // else modules.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, {} ,faultyProbability); + // for(auto const& c : slipperyNorth) { + // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North); + // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North); + + // } + // for(auto const& c : slipperyEast) { + // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East); + // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East); + // } + // for(auto const& c : slipperySouth) { + // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South); + // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South); + // } + // for(auto const& c : slipperyWest) { + // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West); + // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West); + // } + + // modules.printEndmodule(os); + + // if(modelType == prism::ModelType::SMG) { + // if(agentWithProbabilisticBehaviour) modules.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, gridOptions.probabilitiesForActions, slipperyActions); + // else modules.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, {}, slipperyActions); + // } + //if(!stateRewards.empty()) { + // modules.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles); + //} + + //} if (!configuration.empty()) { - printer.printConfiguration(os, configuration); + modules.printConfiguration(os, configuration); } - // TODO CHANGE HANDLING - std::string agentName = agentNames.at(0); - for (auto const & key : keys) { - os << "\n"; - printer.printKeyModule(os, key, maxBoundaries, agentName); - printer.printEndmodule(os); - } - - for (auto const& door : lockedDoors) { - os << "\n"; - printer.printDoorModule(os, door, maxBoundaries, agentName); - printer.printEndmodule(os); - } - } diff --git a/util/Grid.h b/util/Grid.h index e33d34e..1afd30c 100644 --- a/util/Grid.h +++ b/util/Grid.h @@ -65,5 +65,5 @@ class Grid { std::map backgroundTiles; std::map stateRewards; - float faultyProbability; + const float faultyProbability; }; From 5bea12f780bda601b4d0bff3c8e093ae5912938f Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 16:59:06 +0100 Subject: [PATCH 18/38] create formulas with relative offset for portable objects --- util/PrismFormulaPrinter.cpp | 41 +++++++++++++++++++++++++++++++++--- util/PrismFormulaPrinter.h | 5 +++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index 99ba5eb..29e40c4 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -4,6 +4,10 @@ #include #include +std::string oneOffToString(const int &offset) { + return offset != 0 ? ( offset == 1 ? "+1" : "-1" ) : ""; +} + std::string vectorToDisjunction(const std::vector &formulae) { bool first = true; std::string disjunction = ""; @@ -23,10 +27,20 @@ std::string coordinatesToConjunction(const AgentName &agentName, const coordinat return "x" + agentName + "=" + std::to_string(c.first) + "&y" + agentName + "=" + std::to_string(c.second) + "&view" + agentName + "=" + std::to_string(viewDirection); } +std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair &relativePosition, const ViewDirection viewDirection) { + std::string xOffset = oneOffToString(relativePosition.first); + std::string yOffset = oneOffToString(relativePosition.second); + return "x" + agentName + xOffset + "=x" + identifier + "&y" + agentName + yOffset + "=y" + identifier + "&view" + agentName + "=" + std::to_string(viewDirection); +} + std::map getSurroundingCells(const cell &c) { return {{1, c.getNorth()}, {2, c.getEast()}, {3, c.getSouth()}, {0, c.getWest()}}; } +std::map> getRelativeSurroundingCells() { + return { {1, {0,+1}}, {2, {-1,0}}, {3, {0,-1}}, {0, {+1,0}} }; +} + namespace prism { PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava, const cells &goals) : os(os), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava), goals(goals) @@ -47,19 +61,24 @@ namespace prism { for(const auto& ball : balls) { std::string identifier = capitalize(ball.getColor()) + ball.getType(); - printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(ball), "!" + identifier + "PickedUp"); + printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp"); portableObjects.push_back(agentName + "Carrying" + identifier); + for(auto const c : getSurroundingCells(ball)) { + std::cout << ball << std::endl; + std::cout << "dir:" << c.first << " column" << c.second.first << " row" << c.second.second << std::endl; + + } } for(const auto& box : boxes) { std::string identifier = capitalize(box.getColor()) + box.getType(); - printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(box), "!" + identifier + "PickedUp"); + printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp"); portableObjects.push_back(agentName + "Carrying" + identifier); } for(const auto& key : keys) { std::string identifier = capitalize(key.getColor()) + key.getType(); - printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(key), "!" + identifier + "PickedUp"); + printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp"); portableObjects.push_back(agentName + "Carrying" + identifier); } @@ -98,6 +117,11 @@ namespace prism { conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason); } + void PrismFormulaPrinter::printRelativeRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::string &condition) { + os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, reason) + ") & " + condition); + conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason); + } + std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula) { return "formula " + formulaName + " = " + formula + ";\n"; } @@ -134,4 +158,15 @@ namespace prism { } return disjunction; } + + std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::string &reason) { + std::string disjunction = ""; + bool first = true; + for(auto const [viewDirection, relativePosition] : getRelativeSurroundingCells()) { + if(first) first = false; + else disjunction += " | "; + disjunction += "(" + objectPositionToConjunction(agentName, reason, relativePosition, viewDirection) + ")"; + } + return disjunction; + } } diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index d1ead81..e932587 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -7,10 +7,13 @@ #include "ConfigYaml.h" +std::string oneOffToString(const int &offset); std::string vectorToDisjunction(const std::vector &formulae); std::string cellToConjunction(const AgentName &agentName, const cell &c); std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection); +std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair &relativePosition, const ViewDirection viewDirection); std::map getSurroundingCells(const cell &c); +std::map> getRelativeSurroundingCells(); namespace prism { class PrismFormulaPrinter { @@ -23,11 +26,13 @@ namespace prism { void printIsOnFormula(const AgentName &agentName, const std::string &type, const cells &grid_cells, const std::string &direction = ""); void printIsNextToFormula(const AgentName &agentName, const std::string &type, const std::map &coordinates); void printRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::map &coordinates, const std::string &condition); + void printRelativeRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::string &condition); private: std::string buildFormula(const std::string &formulaName, const std::string &formula); std::string buildLabel(const std::string &labelName, const std::string &label); std::string buildDisjunction(const AgentName &agentName, const std::map &cells); std::string buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions = {}); + std::string buildDisjunction(const AgentName &agentName, const std::string &reason); std::ostream &os; std::map restrictions; From 47f14bdc238bb13d351938d187a7c239b75bea60 Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 17:10:30 +0100 Subject: [PATCH 19/38] removed debug output --- util/Grid.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/util/Grid.cpp b/util/Grid.cpp index 936f715..4ccfd00 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -141,7 +141,6 @@ void Grid::printToPrism(std::ostream& os, std::vector& configurat walkable.insert(walkable.end(), balls.begin(), balls.end()); for(auto const& c : walkable) { - std::cout << "checking: " << c << " south of c: " << c.getSouth().first << " " << c.getSouth().second << std::endl; if(isWall(c.getNorth())) northRestriction.push_back(c); if(isWall(c.getEast())) eastRestriction.push_back(c); if(isWall(c.getSouth())) southRestriction.push_back(c); From 48a90f07394b5c7dd7efe9eb16a1fb9562c7f367 Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 17:35:37 +0100 Subject: [PATCH 20/38] added walls to PrismFormulaPrinter also renamed methods for surrounding cells to adjacent --- util/Grid.cpp | 2 +- util/PrismFormulaPrinter.cpp | 21 +++++++++++---------- util/PrismFormulaPrinter.h | 7 ++++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/util/Grid.cpp b/util/Grid.cpp index 4ccfd00..d3e2d49 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -158,7 +158,7 @@ void Grid::printToPrism(std::ostream& os, std::vector& configurat [](const std::map::value_type &pair){return pair.first;}); std::string agentName = agentNames.at(0); - prism::PrismFormulaPrinter formulas(os, wallRestrictions, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, lava, goals); + prism::PrismFormulaPrinter formulas(os, wallRestrictions, walls, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, lava, goals); prism::PrismModulesPrinter modules(os, modelType, maxBoundaries, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, agentNameAndPositionMap, configuration, faultyProbability); modules.printModelType(modelType); diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index 29e40c4..f28950a 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -33,17 +33,18 @@ std::string objectPositionToConjunction(const AgentName &agentName, const std::s return "x" + agentName + xOffset + "=x" + identifier + "&y" + agentName + yOffset + "=y" + identifier + "&view" + agentName + "=" + std::to_string(viewDirection); } -std::map getSurroundingCells(const cell &c) { +std::map getAdjacentCells(const cell &c) { return {{1, c.getNorth()}, {2, c.getEast()}, {3, c.getSouth()}, {0, c.getWest()}}; } -std::map> getRelativeSurroundingCells() { +std::map> getRelativeAdjacentCells() { return { {1, {0,+1}}, {2, {-1,0}}, {3, {0,-1}}, {0, {+1,0}} }; } + namespace prism { - PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava, const cells &goals) - : os(os), restrictions(restrictions), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava), goals(goals) + PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const std::map &restrictions, const cells &walls, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava, const cells &goals) + : os(os), restrictions(restrictions), walls(walls), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava), goals(goals) { } void PrismFormulaPrinter::print(const AgentName &agentName) { @@ -63,7 +64,7 @@ namespace prism { std::string identifier = capitalize(ball.getColor()) + ball.getType(); printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp"); portableObjects.push_back(agentName + "Carrying" + identifier); - for(auto const c : getSurroundingCells(ball)) { + for(auto const c : getAdjacentCells(ball)) { std::cout << ball << std::endl; std::cout << "dir:" << c.first << " column" << c.second.first << " row" << c.second.second << std::endl; @@ -84,14 +85,14 @@ namespace prism { for(const auto& door : unlockedDoors) { std::string identifier = capitalize(door.getColor()) + door.getType(); - printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(door), "!" + identifier + "Open"); - printIsNextToFormula(agentName, identifier, getSurroundingCells(door)); + printRestrictionFormulaWithCondition(agentName, identifier, getAdjacentCells(door), "!" + identifier + "Open"); + printIsNextToFormula(agentName, identifier, getAdjacentCells(door)); } for(const auto& door : lockedDoors) { std::string identifier = capitalize(door.getColor()) + door.getType(); - printRestrictionFormulaWithCondition(agentName, identifier, getSurroundingCells(door), "!" + identifier + "Open"); - printIsNextToFormula(agentName, identifier, getSurroundingCells(door)); + printRestrictionFormulaWithCondition(agentName, identifier, getAdjacentCells(door), "!" + identifier + "Open"); + printIsNextToFormula(agentName, identifier, getAdjacentCells(door)); } if(conditionalMovementRestrictions.size() > 0) { @@ -162,7 +163,7 @@ namespace prism { std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::string &reason) { std::string disjunction = ""; bool first = true; - for(auto const [viewDirection, relativePosition] : getRelativeSurroundingCells()) { + for(auto const [viewDirection, relativePosition] : getRelativeAdjacentCells()) { if(first) first = false; else disjunction += " | "; disjunction += "(" + objectPositionToConjunction(agentName, reason, relativePosition, viewDirection) + ")"; diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index e932587..f93406f 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -12,13 +12,13 @@ std::string vectorToDisjunction(const std::vector &formulae); std::string cellToConjunction(const AgentName &agentName, const cell &c); std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection); std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair &relativePosition, const ViewDirection viewDirection); -std::map getSurroundingCells(const cell &c); -std::map> getRelativeSurroundingCells(); +std::map getAdjacentCells(const cell &c); +std::map> getRelativeAdjacentCells(); namespace prism { class PrismFormulaPrinter { public: - PrismFormulaPrinter(std::ostream &os, const std::map &restrictions, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava, const cells &goals); + PrismFormulaPrinter(std::ostream &os, const std::map &restrictions, const cells &walls, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava, const cells &goals); void print(const AgentName &agentName); @@ -36,6 +36,7 @@ namespace prism { std::ostream &os; std::map restrictions; + cells walls; cells boxes; cells balls; cells lockedDoors; From 389963a7c464421e19b5c74ea5e35ac37264996d Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 7 Jan 2024 19:30:21 +0100 Subject: [PATCH 21/38] added formulas to check where a robot can slip to --- util/PrismFormulaPrinter.cpp | 95 ++++++++++++++++++++++++++---------- util/PrismFormulaPrinter.h | 13 ++++- 2 files changed, 81 insertions(+), 27 deletions(-) diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index f28950a..3b4e0c9 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -23,10 +23,19 @@ std::string cellToConjunction(const AgentName &agentName, const cell &c) { return "x" + agentName + "=" + std::to_string(c.column) + "&y" + agentName + "=" + std::to_string(c.row); } +std::string cellToConjunctionWithOffset(const AgentName &agentName, const cell &c, const std::string &xOffset, const std::string &yOffset){ + return "x" + agentName + xOffset + "=" + std::to_string(c.column) + "&y" + agentName + yOffset + "=" + std::to_string(c.row); +} + std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection) { return "x" + agentName + "=" + std::to_string(c.first) + "&y" + agentName + "=" + std::to_string(c.second) + "&view" + agentName + "=" + std::to_string(viewDirection); } +std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair &relativePosition) { + std::string xOffset = oneOffToString(relativePosition.first); + std::string yOffset = oneOffToString(relativePosition.second); + return "x" + agentName + xOffset + "=x" + identifier + "&y" + agentName + yOffset + "=y" + identifier; +} std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair &relativePosition, const ViewDirection viewDirection) { std::string xOffset = oneOffToString(relativePosition.first); std::string yOffset = oneOffToString(relativePosition.second); @@ -41,6 +50,11 @@ std::map> getRelativeAdjacentCells() { return { {1, {0,+1}}, {2, {-1,0}}, {3, {0,-1}}, {0, {+1,0}} }; } +std::map> getRelativeSurroundingCells() { + return { {"NorthWest", {-1,-1}}, {"North", { 0,-1}}, {"NorthEast", {+1,-1}}, + {"West", {-1, 0}}, {"East", {+1, 0}}, + {"SouthWest", {-1,+1}}, {"South", { 0,+1}}, {"SouthEast", {+1,+1}} }; +} namespace prism { PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const std::map &restrictions, const cells &walls, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const cells &lava, const cells &goals) @@ -52,11 +66,17 @@ namespace prism { printRestrictionFormula(agentName, direction, cells); } - for(const auto& [direction, cells] : slipperyTiles) { - printIsOnFormula(agentName, "Slippery", cells, direction); + if(slipperyBehaviour()) { + for(const auto& [direction, cells] : slipperyTiles) { + printIsOnFormula(agentName, "Slippery", cells, direction); + } + std::vector allSlipperyDirections = {agentName + "IsOnSlipperyNorth", agentName + "IsOnSlipperyEast", agentName + "IsOnSlipperySouth", agentName + "IsOnSlipperyWest"}; + os << buildFormula(agentName + "IsOnSlippery", vectorToDisjunction(allSlipperyDirections)); + + for(const auto& [direction, relativePosition] : getRelativeSurroundingCells()) { + printSlipRestrictionFormula(agentName, direction); + } } - std::vector allSlipperyDirections = {agentName + "IsOnSlipperyNorth", agentName + "IsOnSlipperyEast", agentName + "IsOnSlipperySouth", agentName + "IsOnSlipperyWest"}; - os << buildFormula(agentName + "IsOnSlippery", vectorToDisjunction(allSlipperyDirections)); printIsOnFormula(agentName, "Lava", lava); printIsOnFormula(agentName, "Goal", goals); @@ -64,11 +84,6 @@ namespace prism { std::string identifier = capitalize(ball.getColor()) + ball.getType(); printRelativeRestrictionFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp"); portableObjects.push_back(agentName + "Carrying" + identifier); - for(auto const c : getAdjacentCells(ball)) { - std::cout << ball << std::endl; - std::cout << "dir:" << c.first << " column" << c.second.first << " row" << c.second.second << std::endl; - - } } for(const auto& box : boxes) { @@ -123,8 +138,27 @@ namespace prism { conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason); } - std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula) { - return "formula " + formulaName + " = " + formula + ";\n"; + void PrismFormulaPrinter::printSlipRestrictionFormula(const AgentName &agentName, const std::string &direction) { + std::pair slipCell = getRelativeSurroundingCells().at(direction); + bool semicolon = anyPortableObject() ? false : true; + os << buildFormula(agentName + "CannotSlip" + direction, buildDisjunction(agentName, walls, slipCell), semicolon); + for(auto const key : keys) { + std::string identifier = capitalize(key.getColor()) + key.getType(); + os << " | " << objectPositionToConjunction(agentName, identifier, slipCell); + } + for(auto const ball : balls) { + std::string identifier = capitalize(ball.getColor()) + ball.getType(); + os << " | " << objectPositionToConjunction(agentName, identifier, slipCell); + } + for(auto const box : boxes) { + std::string identifier = capitalize(box.getColor()) + box.getType(); + os << " | " << objectPositionToConjunction(agentName, identifier, slipCell); + } + os << ";\n"; + } + + std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula, const bool semicolon) { + return "formula " + formulaName + " = " + formula + (semicolon ? ";\n": ""); } std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map &cells) { @@ -139,23 +173,14 @@ namespace prism { return disjunction; } - std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions) { + std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells) { if(cells.size() == 0) return "false"; bool first = true; std::string disjunction = ""; - if(!conditions.empty()) { - for(uint index = 0; index < cells.size(); index++) { - if(first) first = false; - else disjunction += " | "; - disjunction += "(" + cellToConjunction(agentName, cells.at(index)) + "&" + conditions.at(index) + ")"; - } - - } else { - for(auto const cell : cells) { - if(first) first = false; - else disjunction += " | "; - disjunction += "(" + cellToConjunction(agentName, cell) + ")"; - } + for(auto const cell : cells) { + if(first) first = false; + else disjunction += " | "; + disjunction += "(" + cellToConjunction(agentName, cell) + ")"; } return disjunction; } @@ -170,4 +195,24 @@ namespace prism { } return disjunction; } + + std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::pair &offset) { + std::string disjunction = ""; + bool first = true; + std::string xOffset = oneOffToString(offset.first); + std::string yOffset = oneOffToString(offset.second); + for(auto const cell : cells) { + if(first) first = false; + else disjunction += " | "; + disjunction += "(" + cellToConjunctionWithOffset(agentName, cell, xOffset, yOffset) + ")"; + } + return disjunction; + } + + bool PrismFormulaPrinter::slipperyBehaviour() const { + return !slipperyTiles.at("North").empty() || !slipperyTiles.at("East").empty() || !slipperyTiles.at("South").empty() || !slipperyTiles.at("West").empty(); + } + bool PrismFormulaPrinter::anyPortableObject() const { + return !keys.empty() || !boxes.empty() || !balls.empty(); + } } diff --git a/util/PrismFormulaPrinter.h b/util/PrismFormulaPrinter.h index f93406f..f1def2d 100644 --- a/util/PrismFormulaPrinter.h +++ b/util/PrismFormulaPrinter.h @@ -10,10 +10,13 @@ std::string oneOffToString(const int &offset); std::string vectorToDisjunction(const std::vector &formulae); std::string cellToConjunction(const AgentName &agentName, const cell &c); +std::string cellToConjunctionWithOffset(const AgentName &agentName, const cell &c, const std::string &xOffset, const std::string &yOffset); std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection); +std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair &relativePosition); std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair &relativePosition, const ViewDirection viewDirection); std::map getAdjacentCells(const cell &c); std::map> getRelativeAdjacentCells(); +std::map> getRelativeSurroundingCells(); namespace prism { class PrismFormulaPrinter { @@ -27,12 +30,18 @@ namespace prism { void printIsNextToFormula(const AgentName &agentName, const std::string &type, const std::map &coordinates); void printRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::map &coordinates, const std::string &condition); void printRelativeRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::string &condition); + void printSlipRestrictionFormula(const AgentName &agentName, const std::string &direction); private: - std::string buildFormula(const std::string &formulaName, const std::string &formula); + std::string buildFormula(const std::string &formulaName, const std::string &formula, const bool semicolon = true); std::string buildLabel(const std::string &labelName, const std::string &label); std::string buildDisjunction(const AgentName &agentName, const std::map &cells); - std::string buildDisjunction(const AgentName &agentName, const cells &cells, const std::vector &conditions = {}); + std::string buildDisjunction(const AgentName &agentName, const cells &cells); std::string buildDisjunction(const AgentName &agentName, const std::string &reason); + std::string buildDisjunction(const AgentName &agentName, const cells &cells, const std::pair &offset); + + bool slipperyBehaviour() const; + bool anyPortableObject() const; + std::ostream &os; std::map restrictions; From bb2bef9b04acc1f5f420ab54777b9c6bf1f97164 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 13:36:51 +0100 Subject: [PATCH 22/38] updates is a vector of update --- util/PrismPrinter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/PrismPrinter.h b/util/PrismPrinter.h index 397cfa9..572cb40 100644 --- a/util/PrismPrinter.h +++ b/util/PrismPrinter.h @@ -13,7 +13,7 @@ typedef std::map KeyNameAndPositionMap; typedef std::pair CellAndCondition; typedef std::pair update; -typedef std::map updates; +typedef std::vector updates; typedef int8_t ActionId; std::string capitalize(std::string string); From 8c6a2bf32731cc76f545e1f2fca44127ecbe66db Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 13:37:18 +0100 Subject: [PATCH 23/38] moved probIntended to Grid members --- util/Grid.cpp | 6 +++--- util/Grid.h | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/util/Grid.cpp b/util/Grid.cpp index d3e2d49..eb4ef38 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -11,8 +11,8 @@ prism::ModelType GridOptions::getModelType() const return prism::ModelType::MDP; } -Grid::Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map &stateRewards, const float faultyProbability) - : allGridCells(gridCells), background(background), gridOptions(gridOptions), stateRewards(stateRewards), faultyProbability(faultyProbability) +Grid::Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map &stateRewards, const float probIntended, const float faultyProbability) + : allGridCells(gridCells), background(background), gridOptions(gridOptions), stateRewards(stateRewards), probIntended(probIntended), faultyProbability(faultyProbability) { cell max = allGridCells.at(allGridCells.size() - 1); maxBoundaries = std::make_pair(max.row - 1, max.column - 1); @@ -159,7 +159,7 @@ void Grid::printToPrism(std::ostream& os, std::vector& configurat std::string agentName = agentNames.at(0); prism::PrismFormulaPrinter formulas(os, wallRestrictions, walls, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, lava, goals); - prism::PrismModulesPrinter modules(os, modelType, maxBoundaries, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, agentNameAndPositionMap, configuration, faultyProbability); + prism::PrismModulesPrinter modules(os, modelType, maxBoundaries, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, agentNameAndPositionMap, configuration, probIntended, faultyProbability); modules.printModelType(modelType); for(const auto &agentName : agentNames) { diff --git a/util/Grid.h b/util/Grid.h index 1afd30c..eb95437 100644 --- a/util/Grid.h +++ b/util/Grid.h @@ -22,7 +22,7 @@ struct GridOptions { class Grid { public: - Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map &stateRewards = {}, const float faultyProbability = 0); + Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map &stateRewards = {}, const float probIntended = 1.0, const float faultyProbability = 0); cells getGridCells(); @@ -65,5 +65,6 @@ class Grid { std::map backgroundTiles; std::map stateRewards; + const float probIntended; const float faultyProbability; }; From ea9d61ddd58c65c6d4d283fbc3103870efd5e8b8 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 13:40:39 +0100 Subject: [PATCH 24/38] added slippery movement updates This also: - adds direction update helpers - adds probIntended to PrismModulesPrinter - removes dead code --- util/PrismModulesPrinter.cpp | 272 ++++++++++++++++++++++------------- util/PrismModulesPrinter.h | 62 ++++---- 2 files changed, 212 insertions(+), 122 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 968d46c..be92c2b 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -8,10 +8,16 @@ #define RIGHT 1 #define FORWARD 2 + +std::string northUpdate(const AgentName &a) { return "(y"+a+"'=y"+a+"-1)"; } +std::string southUpdate(const AgentName &a) { return "(y"+a+"'=y"+a+"+1)"; } +std::string eastUpdate(const AgentName &a) { return "(x"+a+"'=x"+a+"+1)"; } +std::string westUpdate(const AgentName &a) { return "(x"+a+"'=x"+a+"-1)"; } + namespace prism { - PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float faultyProbability) - : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), agentNameAndPositionMap(agentNameAndPositionMap), configuration(config), faultyProbability(faultyProbability), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) { + PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float probIntended, const float faultyProbability) + : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), agentNameAndPositionMap(agentNameAndPositionMap), configuration(config), probIntended(probIntended), faultyProbability(faultyProbability), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) { numberOfPlayer = agentNameAndPositionMap.size(); size_t index = 0; for(auto begin = agentNameAndPositionMap.begin(); begin != agentNameAndPositionMap.end(); begin++, index++) { @@ -117,48 +123,6 @@ namespace prism { return os; } - std::ostream& PrismModulesPrinter::printModule(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &boundaries, const coordinates& initialPosition, const cells &keys, const std::map &backgroundTiles, const bool agentWithView, const std::vector &probabilities, const float faultyProbability) { - /* - os << "module " << agentName << "\n"; - os << "\tx" << agentName << " : [1.." << boundaries.second << "];\n"; - os << "\ty" << agentName << " : [1.." << boundaries.first << "];\n"; - os << "\t" << agentName << "_is_carrying_object : bool;\n"; - - printBooleansForKeys(os, agentName, keys); - printBooleansForBackground(os, agentName, backgroundTiles); - os << "\t" << agentName << "Done : bool;\n"; - if(agentWithView) { - os << "\tview" << agentName << " : [0..3];\n"; - os << "\n"; - if (faultyProbability != 0.0) { - os << "\t[" << agentName << "_turn_right] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " + 1, 4))" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " + 2, 4))" << ";\n"; - os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << ">1 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " - 1, 4))" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " - 2, 4))" << ";\n"; - os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=0 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=3)" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=2)" << ";\n"; - os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=1 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=0)" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=3)" << ";\n"; - } else { - os << "\t[" << agentName << "_turn_right] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery -> (view" << agentName << "'=mod(view" << agentName << " + 1, 4)) " << moveUpdate(agentIndex) << ";\n"; - os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << ">0 -> (view" << agentName << "'=view" << agentName << " - 1) " << moveUpdate(agentIndex) << ";\n"; - os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=0 -> (view" << agentName << "'=3) " << moveUpdate(agentIndex) << ";\n"; - } - } else { - os << "\t[" << agentName << "_turns] " << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << moveGuard(agentIndex) << " true -> (x" << agentName << "'=x" << agentName << ")" << moveUpdate(agentIndex) << ";\n"; - } - printActionsForKeys(os, agentName, keys); - printActionsForBackground(os, agentName, backgroundTiles); - os << "\n"; - - printMovementActions(os, agentName, agentIndex, agentWithView, 1.0, faultyProbability); - for(auto const& probability : probabilities) { - printMovementActions(os, agentName, agentIndex, agentWithView, probability); - } - printDoneActions(os, agentName, agentIndex); - - printConfiguredActions(os, agentName); - - os << "\n"; - */ - return os; - } void PrismModulesPrinter::printPortableObjectModule(const cell &object) { std::string identifier = capitalize(object.getColor()) + object.getType(); @@ -182,7 +146,6 @@ namespace prism { os << "\t[" << agentName << "_drop_" << identifier << "_east]\ttrue -> (x" << identifier << "'=x" << agentName << "+1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n"; } - void PrismModulesPrinter::printDoorModule(const cell &door, const bool &opened) { std::string identifier = capitalize(door.getColor()) + door.getType(); os << "\nmodule " << identifier << std::endl; @@ -221,6 +184,7 @@ namespace prism { printTurnActionsForRobot(agentName); printMovementActionsForRobot(agentName); + if(slipperyBehaviour()) printSlipperyMovementActionsForRobot(agentName); for(const auto &door : unlockedDoors) { std::string identifier = capitalize(door.getColor()) + door.getType(); @@ -291,7 +255,7 @@ namespace prism { } std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) const { - return "\t[" + a + "_move_" + direction + "]" + moveGuard(a) + viewVariable(a, 3) + faultyBehaviourGuard(a, FORWARD) + " !" + a + "IsOnSlippery & !" + a + "IsOnLava & !" + a + "IsOnGoal & !" + a + "CannotMove" + direction + "Wall -> "; + return "\t[" + a + "_move_" + direction + "]" + moveGuard(a) + viewVariable(a, viewDirection) + faultyBehaviourGuard(a, FORWARD) + " !" + a + "IsOnSlippery & !" + a + "IsOnLava & !" + a + "IsOnGoal & !" + a + "CannotMove" + direction + "Wall &!" + a + "CannotMoveConditionally -> "; } std::string PrismModulesPrinter::printMovementUpdate(const AgentName &a, const update &u) const { @@ -318,6 +282,135 @@ namespace prism { } } + void PrismModulesPrinter::printSlipperyMovementActionsForRobot(const AgentName &a) { + if(!slipperyTiles.at("North").empty()) { + printSlipperyMovementActionsForNorth(a); + } + if(!slipperyTiles.at("East").empty()) { + printSlipperyMovementActionsForEast(a) ; + } + if(!slipperyTiles.at("South").empty()) { + printSlipperyMovementActionsForSouth(a); + } + if(!slipperyTiles.at("West").empty()) { + printSlipperyMovementActionsForWest(a) ; + } + } + + void PrismModulesPrinter::printSlipperyMovementActionsForNorth(const AgentName &a) { + actionStream << printSlipperyMovementGuard(a, "North", 3, { "CanSlipNorth", "CanSlipNorthEast", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended) * 1/2, northUpdate(a)+"&"+eastUpdate(a)}, {(1 - probIntended) * 1/2, northUpdate(a)+"&"+westUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 3, {"!CanSlipNorth", "CanSlipNorthEast", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1/2, northUpdate(a)+"&"+eastUpdate(a)}, {1/2, northUpdate(a)+"&"+westUpdate(a)} }); + actionStream << printSlipperyMovementGuard(a, "North", 3, { "CanSlipNorth", "!CanSlipNorthEast", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended), northUpdate(a)+"&"+westUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 3, { "CanSlipNorth", "CanSlipNorthEast", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended), northUpdate(a)+"&"+eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 3, {"!CanSlipNorth", "!CanSlipNorthEast", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)+"&"+westUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 3, { "CanSlipNorth", "!CanSlipNorthEast", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 3, {"!CanSlipNorth", "CanSlipNorthEast", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)+"&"+eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 3, {"!CanSlipNorth", "!CanSlipNorthEast", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "North", 2, { "CanSlipWest", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, westUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 2, {"!CanSlipWest", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, westUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 2, { "CanSlipWest", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, westUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 2, {"!CanSlipWest", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "North", 0, { "CanSlipEast", "CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, eastUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 0, {"!CanSlipEast", "CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {1, eastUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 0, { "CanSlipEast", "!CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {1, eastUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 0, {"!CanSlipEast", "!CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "North", 1, { "CanSlipSouth", "CanSlipNorth"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, southUpdate(a) }, {1 - probIntended, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 1, {"!CanSlipSouth", "CanSlipNorth"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 1, { "CanSlipSouth", "!CanSlipNorth"}) << printSlipperyMovementUpdate(a, "North", { {1, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "North", 1, {"!CanSlipSouth", "!CanSlipNorth"}) << printSlipperyMovementUpdate(a, "North", {}) << "\n"; + } + + void PrismModulesPrinter::printSlipperyMovementActionsForEast(const AgentName &a) { + actionStream << printSlipperyMovementGuard(a, "East", 0, { "CanSlipEast", "CanSlipSouthEast", "CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended) * 1/2, eastUpdate(a)+"&"+southUpdate(a)}, {(1 - probIntended) * 1/2, eastUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 0, {"!CanSlipEast", "CanSlipSouthEast", "CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1/2, eastUpdate(a)+"&"+southUpdate(a)}, {1/2, eastUpdate(a)+"&"+northUpdate(a)} }); + actionStream << printSlipperyMovementGuard(a, "East", 0, { "CanSlipEast", "!CanSlipSouthEast", "CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended), eastUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 0, { "CanSlipEast", "CanSlipSouthEast", "!CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended), eastUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 0, {"!CanSlipEast", "!CanSlipSouthEast", "CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+northUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 0, { "CanSlipEast", "!CanSlipSouthEast", "!CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 0, {"!CanSlipEast", "CanSlipSouthEast", "!CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 0, {"!CanSlipEast", "!CanSlipSouthEast", "!CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "East", 3, { "CanSlipNorth", "CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, northUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 3, {"!CanSlipNorth", "CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 3, { "CanSlipNorth", "!CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, northUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 3, {"!CanSlipNorth", "!CanSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "East", 1, { "CanSlipSouth", "CanSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, southUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 1, {"!CanSlipSouth", "CanSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 1, { "CanSlipSouth", "!CanSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, southUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 1, {"!CanSlipSouth", "!CanSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "East", 2, { "CanSlipWest", "CanSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a) }, {1 - probIntended, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 2, {"!CanSlipWest", "CanSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 2, { "CanSlipWest", "!CanSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {1, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "East", 2, {"!CanSlipWest", "!CanSlipEast"}) << printSlipperyMovementUpdate(a, "East", {}) << "\n"; + } + + void PrismModulesPrinter::printSlipperyMovementActionsForSouth(const AgentName &a) { + actionStream << printSlipperyMovementGuard(a, "South", 1, { "CanSlipSouth", "CanSlipSouthEast", "CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended) * 1/2, southUpdate(a)+"&"+eastUpdate(a)}, {(1 - probIntended) * 1/2, southUpdate(a)+"&"+westUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 1, {"!CanSlipSouth", "CanSlipSouthEast", "CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1/2, southUpdate(a)+"&"+eastUpdate(a)}, {1/2, southUpdate(a)+"&"+westUpdate(a)} }); + actionStream << printSlipperyMovementGuard(a, "South", 1, { "CanSlipSouth", "!CanSlipSouthEast", "CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended), southUpdate(a)+"&"+westUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 1, { "CanSlipSouth", "CanSlipSouthEast", "!CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended), southUpdate(a)+"&"+eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 1, {"!CanSlipSouth", "!CanSlipSouthEast", "CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)+"&"+westUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 1, { "CanSlipSouth", "!CanSlipSouthEast", "!CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 1, {"!CanSlipSouth", "CanSlipSouthEast", "!CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)+"&"+eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 1, {"!CanSlipSouth", "!CanSlipSouthEast", "!CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "South", 2, { "CanSlipWest", "CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, westUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 2, {"!CanSlipWest", "CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 2, { "CanSlipWest", "!CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, westUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 2, {"!CanSlipWest", "!CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "South", 0, { "CanSlipEast", "CanSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, eastUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 0, {"!CanSlipEast", "CanSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 0, { "CanSlipEast", "!CanSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {1, eastUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 0, {"!CanSlipEast", "!CanSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "South", 3, { "CanSlipSouth", "CanSlipNorth"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a) }, {1 - probIntended, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 3, {"!CanSlipSouth", "CanSlipNorth"}) << printSlipperyMovementUpdate(a, "South", { {1, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 3, { "CanSlipSouth", "!CanSlipNorth"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "South", 3, {"!CanSlipSouth", "!CanSlipNorth"}) << printSlipperyMovementUpdate(a, "South", {}) << "\n"; + } + + void PrismModulesPrinter::printSlipperyMovementActionsForWest(const AgentName &a) { + actionStream << printSlipperyMovementGuard(a, "West", 2, { "CanSlipWest", "CanSlipSouthWest", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended) * 1/2, westUpdate(a)+"&"+southUpdate(a)}, {(1 - probIntended) * 1/2, westUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 2, {"!CanSlipWest", "CanSlipSouthWest", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1/2, westUpdate(a)+"&"+southUpdate(a)}, {1/2, westUpdate(a)+"&"+northUpdate(a)} }); + actionStream << printSlipperyMovementGuard(a, "West", 2, { "CanSlipWest", "!CanSlipSouthWest", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended), westUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 2, { "CanSlipWest", "CanSlipSouthWest", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended), westUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 2, {"!CanSlipWest", "!CanSlipSouthWest", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+northUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 2, { "CanSlipWest", "!CanSlipSouthWest", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 2, {"!CanSlipWest", "CanSlipSouthWest", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 2, {"!CanSlipWest", "!CanSlipSouthWest", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "West", 3, { "CanSlipNorth", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, northUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 3, {"!CanSlipNorth", "CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+northUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 3, { "CanSlipNorth", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 3, {"!CanSlipNorth", "!CanSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "West", 1, { "CanSlipSouth", "CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, southUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 1, {"!CanSlipSouth", "CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 1, { "CanSlipSouth", "!CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, southUpdate(a) } }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 1, {"!CanSlipSouth", "!CanSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << "\n"; + + actionStream << printSlipperyMovementGuard(a, "West", 0, { "CanSlipEast", "CanSlipWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a) }, {1 - probIntended, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 0, {"!CanSlipEast", "CanSlipWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 0, { "CanSlipEast", "!CanSlipWest"}) << printSlipperyMovementUpdate(a, "West", { {1, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyMovementGuard(a, "West", 0, {"!CanSlipEast", "!CanSlipWest"}) << printSlipperyMovementUpdate(a, "West", {}) << "\n"; + } + + + std::string PrismModulesPrinter::printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector &guards) const { + return "\t[" + a + "_move_" + direction + "]" + moveGuard(a) + viewVariable(a, viewDirection) + faultyBehaviourGuard(a, FORWARD) + a + "IsOnSlippery" + direction + " & " + buildConjunction(a, guards) + " -> "; + } + + std::string PrismModulesPrinter::printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const { + return updatesToString(u, a, FORWARD); + } + + std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) { for (auto& config : configuration) { if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) { @@ -330,47 +423,6 @@ namespace prism { return os; } - std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability, const double &stickyProbability) { - /* - if(stickyProbability != 0.0) { - os << "\t[" << agentName << "_move_north]" << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveNorth -> " << (100 - stickyProbability) << "/100:" << "(y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(y" << agentName << "'=max(y" << agentName << "-2, 1 ))" << moveUpdate(agentIndex) << ";\n"; - os << "\t[" << agentName << "_move_east] " << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveEast -> " << (100 - stickyProbability) << "/100:" << "(x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(x" << agentName << "'=min(x" << agentName << "+2, width))" << moveUpdate(agentIndex) << ";\n"; - os << "\t[" << agentName << "_move_south]" << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveSouth -> " << (100 - stickyProbability) << "/100:" << "(y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(y" << agentName << "'=min(y" << agentName << "+2, height))" << moveUpdate(agentIndex) << ";\n"; - os << "\t[" << agentName << "_move_west] " << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveWest -> " << (100 - stickyProbability) << "/100:" << "(x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(x" << agentName << "'=max(x" << agentName << "-1, 1))" << moveUpdate(agentIndex) << ";\n"; - } - else if(probability >= 1) { - os << "\t[" << agentName << "_move_north]" << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveNorth -> (y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << ";\n"; - os << "\t[" << agentName << "_move_east] " << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveEast -> (x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << ";\n"; - os << "\t[" << agentName << "_move_south]" << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveSouth -> (y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << ";\n"; - os << "\t[" << agentName << "_move_west] " << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveWest -> (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << ";\n"; - } else { - std::string probabilityString = std::to_string(probability); - std::string percentageString = std::to_string((int)(100 * probability)); - std::string complementProbabilityString = std::to_string(1 - probability); - os << "\t[" << agentName << "_move_north_" << percentageString << "] "; - os << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveNorth -> "; - os << probabilityString << ": (y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << " + "; - os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n"; - - os << "\t[" << agentName << "_move_east_" << percentageString << "] "; - os << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveEast -> "; - os << probabilityString << ": (x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << " + "; - os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n"; - - os << "\t[" << agentName << "_move_south_" << percentageString << "] "; - os << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveSouth -> "; - os << probabilityString << ": (y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << " + "; - os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n"; - - os << "\t[" << agentName << "_move_west_" << percentageString << "] "; - os << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveWest -> "; - os << probabilityString << ": (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << " + "; - os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n"; - } - */ - return os; - } - std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName) { os << "\t[" << agentName << "_done]" << moveGuard(agentName) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n"; return os; @@ -685,12 +737,6 @@ namespace prism { return os; } - std::ostream& PrismModulesPrinter::printEndmodule(std::ostream &os) { - os << "endmodule\n"; - os << "\n"; - return os; - } - std::ostream& PrismModulesPrinter::printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector &probabilities, const std::set &slipperyActions) { os << "player " << agentName << "\n\t"; bool first = true; @@ -798,10 +844,14 @@ namespace prism { } std::string PrismModulesPrinter::faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const { - if(actionId == NOFAULT) { - return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + ") & "; + if(faultyBehaviour()) { + if(actionId == NOFAULT) { + return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + ") & "; + } else { + return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + " | previousAction" + agentName + "=" + std::to_string(actionId) + ") & "; + } } else { - return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + " | previousAction" + agentName + "=" + std::to_string(actionId) + ") & "; + return ""; } } @@ -818,6 +868,19 @@ namespace prism { ""; } + std::string PrismModulesPrinter::updatesToString(const updates &updates, const AgentName &a, const ActionId &actionId) const { + if(updates.empty()) return "true"; + std::string updatesString = ""; + bool first = true; + for(auto const update : updates) { + if(first) first = false; + else updatesString += " + "; + updatesString += updateToString(update); + if(faultyBehaviour()) updatesString += "&" + faultyBehaviourUpdate(a, actionId); + } + return updatesString; + } + std::string PrismModulesPrinter::updateToString(const update &u) const { return std::to_string(u.first) + ": " + u.second; } @@ -834,7 +897,24 @@ namespace prism { return faultyProbability > 0.0f; } + bool PrismModulesPrinter::slipperyBehaviour() const { + return !slipperyTiles.at("North").empty() || !slipperyTiles.at("East").empty() || !slipperyTiles.at("South").empty() || !slipperyTiles.at("West").empty(); + } + bool PrismModulesPrinter::isGame() const { return modelType == ModelType::SMG; } + + std::string PrismModulesPrinter::buildConjunction(const AgentName &a, std::vector formulae) const { + if(formulae.empty()) return "true"; + std::string conjunction = ""; + bool first = true; + for(auto const formula : formulae) { + if(first) first = false; + else conjunction += " & "; + conjunction += formula; + } + return conjunction; + } + } diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 425283e..00f3836 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -6,28 +6,21 @@ #include "PrismPrinter.h" #include "ConfigYaml.h" + +std::string northUpdate(const AgentName &a); +std::string southUpdate(const AgentName &a); +std::string eastUpdate(const AgentName &a); +std::string westUpdate(const AgentName &a); + namespace prism { class PrismModulesPrinter { public: - PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float faultyProbability); + PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float probIntended, const float faultyProbability); std::ostream& print(); std::ostream& printModelType(const ModelType &modelType); - void printPortableObjectModule(const cell &object); - void printPortableObjectActions(const std::string &agentName, const std::string &identifier); - - void printDoorModule(const cell &object, const bool &opened); - void printLockedDoorActions(const std::string &agentName, const std::string &identifier); - void printUnlockedDoorActions(const std::string &agentName, const std::string &identifier); - - void printRobotModule(const AgentName &agentName, const coordinates &initialPosition); - void printPortableObjectActionsForRobot(const std::string &agentName, const std::string &identifier); - void printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier); - void printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key); - void printMovementActionsForRobot(const std::string &a); - void printTurnActionsForRobot(const std::string &a); std::ostream& printConstants(std::ostream &os, const std::vector &constants); /* @@ -56,19 +49,7 @@ namespace prism { std::ostream& printSlipperyTurn(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation); std::ostream& printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType); - std::ostream& printModule(std::ostream &os, - const AgentName &agentName, - const size_t &agentIndex, - const coordinates &boundaries, - const coordinates& initialPosition, - const cells &keys, - const std::map &backgroundTiles, - const bool agentWithView, - const std::vector &probabilities = {}, - const float faultyProbability = 0); - std::ostream& printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability = 1.0, const double &stickyProbability = 0.0); std::ostream& printDoneActions(std::ostream &os, const AgentName &agentName); - std::ostream& printEndmodule(std::ostream &os); std::ostream& printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector &probabilities = {}, const std::set &slipperyActions = {}); std::ostream& printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer); std::ostream& printRewards(std::ostream &os, const AgentName &agentName, const std::map &stateRewards, const cells &lava, const cells &goals, const std::map &backgroundTiles); @@ -79,22 +60,48 @@ namespace prism { bool isGame() const; private: + void printPortableObjectModule(const cell &object); + void printPortableObjectActions(const std::string &agentName, const std::string &identifier); + + void printDoorModule(const cell &object, const bool &opened); + void printLockedDoorActions(const std::string &agentName, const std::string &identifier); + void printUnlockedDoorActions(const std::string &agentName, const std::string &identifier); + + void printRobotModule(const AgentName &agentName, const coordinates &initialPosition); + void printPortableObjectActionsForRobot(const std::string &agentName, const std::string &identifier); + void printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier); + void printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key); + void printMovementActionsForRobot(const std::string &a); + void printTurnActionsForRobot(const std::string &a); + void printSlipperyMovementActionsForRobot(const AgentName &a); + void printSlipperyMovementActionsForNorth(const AgentName &a); + void printSlipperyMovementActionsForEast(const AgentName &a); + void printSlipperyMovementActionsForSouth(const AgentName &a); + void printSlipperyMovementActionsForWest(const AgentName &a); + std::string printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) const; std::string printMovementUpdate(const AgentName &a, const update &update) const; std::string printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond = "") const; std::string printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const; + std::string printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector &guards) const; + std::string printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const; bool anyPortableObject() const; bool faultyBehaviour() const; + bool slipperyBehaviour() const; std::string moveGuard(const AgentName &agentName) const; std::string faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const; std::string faultyBehaviourUpdate(const AgentName &agentName, const ActionId &actionId) const; std::string moveUpdate(const AgentName &agentName) const; + std::string updatesToString(const updates &updates, const AgentName &a, const ActionId &actionId) const; std::string updateToString(const update &u) const; std::string viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView = true) const; + std::string buildConjunction(const AgentName &a, std::vector formulae) const; + + std::ostream &os; std::stringstream actionStream; @@ -106,12 +113,15 @@ namespace prism { cells lockedDoors; cells unlockedDoors; cells keys; + std::map slipperyTiles; AgentNameAndPositionMap agentNameAndPositionMap; std::map agentIndexMap; size_t numberOfPlayer; float const faultyProbability; + float const probIntended; std::vector configuration; std::map viewDirectionMapping; + std::vector viewDirections = {0, 1, 2, 3}; }; } From 8cf91e700cd59c6bfa571e475041799ebf2312ec Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 13:42:16 +0100 Subject: [PATCH 25/38] WIP: changed probabilities in main for testing --- main.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 37b3a36..be33399 100644 --- a/main.cpp +++ b/main.cpp @@ -144,12 +144,13 @@ int main(int argc, char* argv[]) { pos_iterator_t backgroundIter = backgroundFirst; pos_iterator_t backgroundLast(background.end()); MinigridParser backgroundParser(backgroundFirst); - + cells contentCells; cells backgroundCells; std::vector configurations; std::map stateRewards; - double faultyProbability; + float faultyProbability = 0.0; + float probIntended = 0.9; try { bool ok = phrase_parse(contentIter, contentLast, contentParser, qi::space, contentCells); @@ -157,7 +158,7 @@ int main(int argc, char* argv[]) { ok &= phrase_parse(backgroundIter, backgroundLast, backgroundParser, qi::space, backgroundCells); // TODO } if (configFilename->is_set()) { - YamlConfigParser parser(configFilename->value(0)); + YamlConfigParser parser(configFilename->value(0)); configurations = parser.parseConfiguration(); } @@ -181,8 +182,9 @@ int main(int argc, char* argv[]) { } } if(ok) { - Grid grid(contentCells, backgroundCells, gridOptions, stateRewards, faultyProbability); - //grid.printToPrism(std::cout, prism::ModelType::MDP); + Grid grid(contentCells, backgroundCells, gridOptions, stateRewards, probIntended, faultyProbability); + + grid.printToPrism(std::cout, configurations , gridOptions.getModelType()); std::stringstream ss; // grid.printToPrism(file, configurations ,prism::ModelType::MDP); grid.printToPrism(ss, configurations , gridOptions.getModelType()); From c6f7bb27be22a342ede8e8b08caa358c9f81dbde Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 14:06:13 +0100 Subject: [PATCH 26/38] added slippery turn actions --- util/PrismModulesPrinter.cpp | 52 +++++++++++++++++++++++++++++++++++- util/PrismModulesPrinter.h | 6 +++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index be92c2b..da7bb8a 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -285,15 +285,19 @@ namespace prism { void PrismModulesPrinter::printSlipperyMovementActionsForRobot(const AgentName &a) { if(!slipperyTiles.at("North").empty()) { printSlipperyMovementActionsForNorth(a); + printSlipperyTurnActionsForNorth(a); } if(!slipperyTiles.at("East").empty()) { printSlipperyMovementActionsForEast(a) ; + printSlipperyTurnActionsForEast(a); } if(!slipperyTiles.at("South").empty()) { printSlipperyMovementActionsForSouth(a); + printSlipperyTurnActionsForSouth(a); } if(!slipperyTiles.at("West").empty()) { printSlipperyMovementActionsForWest(a) ; + printSlipperyTurnActionsForWest(a); } } @@ -401,15 +405,61 @@ namespace prism { actionStream << printSlipperyMovementGuard(a, "West", 0, {"!CanSlipEast", "!CanSlipWest"}) << printSlipperyMovementUpdate(a, "West", {}) << "\n"; } + void PrismModulesPrinter::printSlipperyTurnActionsForNorth(const AgentName &a) { + actionStream << printSlipperyTurnGuard(a, "right", { "CanSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", {"!CanSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + + actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; + } + + void PrismModulesPrinter::printSlipperyTurnActionsForEast(const AgentName &a) { + actionStream << printSlipperyTurnGuard(a, "right", { "CanSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", {"!CanSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + + actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; + } + + void PrismModulesPrinter::printSlipperyTurnActionsForSouth(const AgentName &a) { + actionStream << printSlipperyTurnGuard(a, "right", { "CanSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", {"!CanSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + + actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; + } + + void PrismModulesPrinter::printSlipperyTurnActionsForWest(const AgentName &a) { + actionStream << printSlipperyTurnGuard(a, "right", { "CanSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", {"!CanSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + + actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; + } std::string PrismModulesPrinter::printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector &guards) const { - return "\t[" + a + "_move_" + direction + "]" + moveGuard(a) + viewVariable(a, viewDirection) + faultyBehaviourGuard(a, FORWARD) + a + "IsOnSlippery" + direction + " & " + buildConjunction(a, guards) + " -> "; + return "\t[" + a + "_move_" + direction + "] " + moveGuard(a) + viewVariable(a, viewDirection) + faultyBehaviourGuard(a, FORWARD) + a + "IsOnSlippery" + direction + " & " + buildConjunction(a, guards) + " -> "; } std::string PrismModulesPrinter::printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const { return updatesToString(u, a, FORWARD); } + std::string PrismModulesPrinter::printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const std::vector &guards, const std::string &cond) const { + return "\t[" + a + "_turn_" + direction + "] " + buildConjunction(a, guards) + " & " + cond + " -> "; + } + + std::string PrismModulesPrinter::printSlipperyTurnUpdate(const AgentName &a, const updates &u) { + return updatesToString(u, a, NOFAULT); + } std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) { for (auto& config : configuration) { diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 00f3836..1e71404 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -78,6 +78,10 @@ namespace prism { void printSlipperyMovementActionsForEast(const AgentName &a); void printSlipperyMovementActionsForSouth(const AgentName &a); void printSlipperyMovementActionsForWest(const AgentName &a); + void printSlipperyTurnActionsForNorth(const AgentName &a); + void printSlipperyTurnActionsForEast(const AgentName &a); + void printSlipperyTurnActionsForSouth(const AgentName &a); + void printSlipperyTurnActionsForWest(const AgentName &a); std::string printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) const; std::string printMovementUpdate(const AgentName &a, const update &update) const; @@ -85,6 +89,8 @@ namespace prism { std::string printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const; std::string printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector &guards) const; std::string printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const; + std::string printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const std::vector &guards, const std::string &cond) const; + std::string printSlipperyTurnUpdate(const AgentName &a, const updates &u); bool anyPortableObject() const; bool faultyBehaviour() const; From 2b7d5bc9a93a6c8137fbfc66d7dd3db8f1c95138 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 14:06:24 +0100 Subject: [PATCH 27/38] removed dead code --- util/PrismModulesPrinter.cpp | 311 +---------------------------------- 1 file changed, 1 insertion(+), 310 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index da7bb8a..7d274c9 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -478,315 +478,6 @@ namespace prism { return os; } - std::ostream& PrismModulesPrinter::printSlipperyTurn(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation) { - // constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 9; - - // std::array positionTransition = { - // /* north */ "(y" + agentName + "'=y" + agentName + "-1)", - // /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)", - // /* east */ "(x" + agentName + "'=x" + agentName + "+1)", - // /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)", - // /* south */ "(y" + agentName + "'=y" + agentName + "+1)", - // /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)", - // /* west */ "(x" + agentName + "'=x" + agentName + "-1)", - // /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)", - // /* own position */ "(x" + agentName + "'=x" + agentName + ") & (y" + agentName + "'=y" + agentName + ")" - // }; - - // // view transition appdx in form (guard, update part) - // // IMPORTANT: No mod() usage for turn left due to bug in mod() function for decrement - - - // std::array, 3> viewTransition = { - // std::make_tuple(" & " + agentName + "SlipperyTurnRightAllowed ", " & (view" + agentName + "'=mod(view" + agentName + " + 1, 4))", "_right]"), - // std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + ">0", " & (view" + agentName + "'=view" + agentName + " - 1)", "_left]"), - // std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + "=0", " & (view" + agentName + "'=3)", "_left]") - // }; - - // // direction specifics - - // std::string actionName; - // std::string positionGuard; - // std::size_t remainPosIndex = 8; - // std::array prob_piece_dir; // { n, ne, e, se, s, sw, w, nw, CURRENT POS } - // std::array prob_piece_dir_constants; - - // switch (orientation) - // { - // case SlipperyType::North: - // actionName = "\t[" + agentName + "turn_at_slip_north"; - // positionGuard = "\t" + agentName + "IsOnSlipperyNorth"; - // prob_piece_dir = { 0, 0, 0, 0, 1, 0, 0, 0, 0 /* <- R */ }; - // prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_turn_displacement" /* <- R */, "prop_zero", "prop_zero", "prop_zero","prop_zero" }; - // break; - - // case SlipperyType::South: - // actionName = "\t[" + agentName + "turn_at_slip_south"; - // positionGuard = "\t" + agentName + "IsOnSlipperySouth"; - // prob_piece_dir = { 1, 0, 0, 0, 0, 0, 0, 0, 0 /* <- R */ }; - // prob_piece_dir_constants = { "prop_turn_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" }; - // break; - - // case SlipperyType::East: - // actionName = "\t[" + agentName + "turn_at_slip_east"; - // positionGuard = "\t" + agentName + "IsOnSlipperyEast"; - // prob_piece_dir = { 0, 0, 0, 0, 0, 0, 1, 0, 0 /* <- R */ }; - // prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_turn_displacement", "prop_zero", "prop_zero" }; - // break; - - // case SlipperyType::West: - // actionName = "\t[" + agentName + "turn_at_slip_west"; - // positionGuard = "\t" + agentName + "IsOnSlipperyWest"; - // prob_piece_dir = { 0, 0, 1, 0, 0, 0, 0, 0, 0 /* <- R */ }; - // prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_turn_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" }; - // break; - // } - - // slipperyActions.insert(actionName + "_left]"); - // slipperyActions.insert(actionName + "_right]"); - - // // override probability to 0 if corresp. direction is blocked - - // for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS - 1; i++) { - // if (!neighborhood.at(i)) - // prob_piece_dir.at(i) = 0; - // } - - // // determine residual probability (R) by replacing 0 with (1 - overall sum) - - // prob_piece_dir.at(remainPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0); - // prob_piece_dir_constants.at(remainPosIndex) = "prop_turn_intended"; - // // - // { - // assert(prob_piece_dir.at(remainPosIndex) <= 9 && prob_piece_dir.at(remainPosIndex) >= 6 && "Value not in Range!"); - // assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!"); - // } - // // - - // // generic output (for every view transition) - // for (std::size_t v = 0; v < viewTransition.size(); v++) { - // os << actionName << std::get<2>(viewTransition.at(v)) << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << std::get<0>(viewTransition.at(v)); - // for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - // if (i == remainPosIndex) { - // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants.at(i) << " : " << positionTransition.at(i) << std::get<1>(viewTransition.at(v)) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - // } else { - // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - // } - // } - // } - - return os; - } - - std::ostream& PrismModulesPrinter::printSlipperyMove(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation) { - //constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 8; - - //std::array positionTransition = { - // /* north */ "(y" + agentName + "'=y" + agentName + "-1)", - // /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)", - // /* east */ "(x" + agentName + "'=x" + agentName + "+1)", - // /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)", - // /* south */ "(y" + agentName + "'=y" + agentName + "+1)", - // /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)", - // /* west */ "(x" + agentName + "'=x" + agentName + "-1)", - // /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)" - //}; - - //// direction specifics - - //std::size_t straightPosIndex, straightPosIndex_east, straightPosIndex_south, straightPosIndex_west, straightPosIndex_north; - //std::string actionName, specialTransition; // if straight ahead is blocked - //std::string positionGuard; - //std::array prob_piece_dir; // { n, ne, e, se, s, sw, w, nw } - //std::array prob_piece_dir_agent_north; // { n, ne, e, se, s, sw, w, nw } - //std::array prob_piece_dir_agent_east; // { n, ne, e, se, s, sw, w, nw } - //std::array prob_piece_dir_agent_south; // { n, ne, e, se, s, sw, w, nw } - //std::array prob_piece_dir_agent_west; // { n, ne, e, se, s, sw, w, nw } - - //std::array prob_piece_dir_constants; - //std::array prob_piece_dir_constants_agent_north; - //std::array prob_piece_dir_constants_agent_east; - //std::array prob_piece_dir_constants_agent_south; - //std::array prob_piece_dir_constants_agent_west; - - //switch (orientation) - //{ - // case SlipperyType::North: - // actionName = "\t[" + agentName + "move_on_slip_north]"; - // positionGuard = "\t" + agentName + "IsOnSlipperyNorth"; - // prob_piece_dir = { 0, 0, 1, 2, 0 /* <- R */, 2, 1, 0 }; - - // prob_piece_dir_agent_south = { 0 , 0, 0, 1, 0 /*s <- R */, 1, 0, 0}; - // prob_piece_dir_agent_east = { 0, 0, 0 /*e <- R */, 2, 0, 0, 0, 0 }; - // prob_piece_dir_agent_north = { 0 /*n <- R */, 0, 0, 0, 2 , 0, 0, 0 }; - // prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 2, 0 /* <- R */, 0 }; - - // prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero" }; - - // prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_zero" }; - // prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; - // prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; - // prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement", "prop_zero", "prop_zero" } ; - - - // straightPosIndex = 4; - // straightPosIndex_east = 2; - // straightPosIndex_south = 4; - // straightPosIndex_west = 6; - // straightPosIndex_north = 0; - - // specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)"); - // break; - - // case SlipperyType::South: - // actionName = "\t[" + agentName + "move_on_slip_south]"; - // positionGuard = "\t" + agentName + "IsOnSlipperySouth"; - - // prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 }; - // // { n, ne, e, se, s, sw, w, nw } - // prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; - // prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; - // prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; - // prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; - - // prob_piece_dir_constants = { "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement" }; - - // prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement * 1/2" }; - // prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; - // prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; - // prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement" } ; - - - // straightPosIndex = 0; // always north - // straightPosIndex_east = 2; - // straightPosIndex_south = 4; - // straightPosIndex_west = 6; - // straightPosIndex_north = 0; - // specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)"); - // break; - - // case SlipperyType::East: - // actionName = "\t[" + agentName + "move_on_slip_east]"; - // positionGuard = "\t" + agentName + "IsOnSlipperyEast"; - // // { n, ne, e, se, s, sw, w, nw } - - // prob_piece_dir = { 1, 0, 0, 0, 1, 2, 0 /* <- R */, 2 }; - // // TODO - // prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; - // prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; - // prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; - // prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; - - // prob_piece_dir_constants = { "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement" }; - - // prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2" }; - // prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_displacement", "prop_zero" }; - // prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" } ; - // prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2" } ; - - - // straightPosIndex = 6; - // straightPosIndex_east = 2; - // straightPosIndex_south = 4; - // straightPosIndex_west = 6; - // straightPosIndex_north = 0; - - // specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)"); - // break; - - // case SlipperyType::West: - // actionName = "\t[" + agentName + "move_on_slip_west]"; - // positionGuard = "\t" + agentName + "IsOnSlipperyWest"; - // prob_piece_dir = { 1, 2, 0 /* <- R */, 2, 1, 0, 0, 0 }; - // // TODO - // // { n, ne, e, se, s, sw, w, nw } - - // prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1}; - // prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 }; - // prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 }; - // prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 }; - - // prob_piece_dir_constants = {"prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero","prop_zero", "prop_zero" }; - - // prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; - // prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" }; - // prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; - // prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_displacement", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ; - - - // straightPosIndex = 2; - // straightPosIndex_east = 2; - // straightPosIndex_south = 4; - // straightPosIndex_west = 6; - // straightPosIndex_north = 0; - // specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)"); - // break; - //} - - //slipperyActions.insert(actionName); - - //// override probability to 0 if corresp. direction is blocked - - //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - // if (!neighborhood.at(i)) - // prob_piece_dir.at(i) = 0; - //} - - //// determine residual probability (R) by replacing 0 with (1 - overall sum) - //prob_piece_dir.at(straightPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0); - //prob_piece_dir_constants.at(straightPosIndex) = "prop_intended"; - //prob_piece_dir_constants_agent_east.at(straightPosIndex_east) = "prop_intended"; - //prob_piece_dir_constants_agent_south.at(straightPosIndex_south) = "prop_intended"; - //prob_piece_dir_constants_agent_west.at(straightPosIndex_west) = "prop_intended"; - //prob_piece_dir_constants_agent_north.at(straightPosIndex_north) = "prop_intended"; - //// - //{ - // assert(prob_piece_dir.at(straightPosIndex) <= 9 && prob_piece_dir.at(straightPosIndex) >= 3 && "Value not in Range!"); - // assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!"); - // assert(orientation != SlipperyType::North || (prob_piece_dir.at(7) == 0 && prob_piece_dir.at(0) == 0 && prob_piece_dir.at(1) == 0 && "Slippery up should be impossible!")); - // assert(orientation != SlipperyType::South || (prob_piece_dir.at(3) == 0 && prob_piece_dir.at(4) == 0 && prob_piece_dir.at(5) == 0 && "Slippery down should be impossible!")); - // assert(orientation != SlipperyType::East || (prob_piece_dir.at(1) == 0 && prob_piece_dir.at(2) == 0 && prob_piece_dir.at(3) == 0 && "Slippery right should be impossible!")); - // assert(orientation != SlipperyType::West || (prob_piece_dir.at(5) == 0 && prob_piece_dir.at(6) == 0 && prob_piece_dir.at(7) == 0 && "Slippery left should be impossible!")); - //} - //// - - //// special case: straight forward is blocked (then remain in same position) - - //positionTransition.at(straightPosIndex) = specialTransition; - - //// generic output (for every view and every possible view direction) - //size_t current_dir = 0; // East - //os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - - //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_east.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - //} - - //current_dir = 1; // South - //os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - - //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_south.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - //} - - //current_dir = 2; // West - - //os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_west.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - //} - - - //current_dir = 3; // North - - //os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir; - //for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) { - // os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_north.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n"); - //} - - return os; - } - std::ostream& PrismModulesPrinter::printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector &probabilities, const std::set &slipperyActions) { os << "player " << agentName << "\n\t"; bool first = true; @@ -926,7 +617,7 @@ namespace prism { if(first) first = false; else updatesString += " + "; updatesString += updateToString(update); - if(faultyBehaviour()) updatesString += "&" + faultyBehaviourUpdate(a, actionId); + //if(faultyBehaviour()) updatesString += "&" + faultyBehaviourUpdate(a, actionId); } return updatesString; } From a8ac3098a7fc4fe6e845a0288f2a4672d3318e29 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 15:18:42 +0100 Subject: [PATCH 28/38] default formulas to false This will be changed to remove the unnecessary guards instead --- util/PrismFormulaPrinter.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/util/PrismFormulaPrinter.cpp b/util/PrismFormulaPrinter.cpp index 3b4e0c9..ef2f5a5 100644 --- a/util/PrismFormulaPrinter.cpp +++ b/util/PrismFormulaPrinter.cpp @@ -76,6 +76,8 @@ namespace prism { for(const auto& [direction, relativePosition] : getRelativeSurroundingCells()) { printSlipRestrictionFormula(agentName, direction); } + } else { + os << buildFormula(agentName + "IsOnSlippery", "false"); } printIsOnFormula(agentName, "Lava", lava); printIsOnFormula(agentName, "Goal", goals); @@ -113,6 +115,8 @@ namespace prism { if(conditionalMovementRestrictions.size() > 0) { os << buildFormula(agentName + "CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions)); os << buildFormula(agentName + "IsCarrying", vectorToDisjunction(portableObjects)); + } else { + os << buildFormula(agentName + "CannotMoveConditionally", "false"); } } From 9be03f2701f2fb1d89c4ad7ba9f0e20c69926ffc Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 15:22:07 +0100 Subject: [PATCH 29/38] summary commit This adds: - slippery turn actions - agent name to set of actions map - modules for faultyBehaviour --- util/PrismModulesPrinter.cpp | 182 +++++++++++++++++++++-------------- util/PrismModulesPrinter.h | 15 ++- 2 files changed, 119 insertions(+), 78 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 7d274c9..988df47 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -39,6 +39,10 @@ namespace prism { } std::ostream& PrismModulesPrinter::print() { + for(const auto [agentName, initialPosition] : agentNameAndPositionMap) { + agentNameActionMap[agentName] = {}; + } + for(const auto &key : keys) { printPortableObjectModule(key); } @@ -58,6 +62,13 @@ namespace prism { for(const auto [agentName, initialPosition] : agentNameAndPositionMap) { printRobotModule(agentName, initialPosition); } + + if(faultyBehaviour()) { + for(const auto [agentName, initialPosition] : agentNameAndPositionMap) { + printFaultyMovementModule(agentName); + } + } + return os; } @@ -139,11 +150,21 @@ namespace prism { } void PrismModulesPrinter::printPortableObjectActions(const std::string &agentName, const std::string &identifier) { - os << "\t[" << agentName << "_pickup_" << identifier << "]\ttrue -> (x" << identifier << "'=-1) & (y" << identifier << "'=-1) & (" << identifier << "PickedUp'=true);\n"; - os << "\t[" << agentName << "_drop_" << identifier << "_north]\ttrue -> (x" << identifier << "'=x" << agentName << ") & (y" << identifier << "'=y" << agentName << "-1) & (" << identifier << "PickedUp'=false);\n"; - os << "\t[" << agentName << "_drop_" << identifier << "_west]\ttrue -> (x" << identifier << "'=x" << agentName << "-1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n"; - os << "\t[" << agentName << "_drop_" << identifier << "_south]\ttrue -> (x" << identifier << "'=x" << agentName << ") & (y" << identifier << "'=y" << agentName << "+1) & (" << identifier << "PickedUp'=false);\n"; - os << "\t[" << agentName << "_drop_" << identifier << "_east]\ttrue -> (x" << identifier << "'=x" << agentName << "+1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n"; + std::string actionName = "[" + agentName + "_pickup_" + identifier + "]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t" << actionName << " true -> (x" << identifier << "'=-1) & (y" << identifier << "'=-1) & (" << identifier << "PickedUp'=true);\n"; + actionName = "[" + agentName + "_drop_" + identifier + "_north]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t" << actionName << " " << " true -> (x" << identifier << "'=x" << agentName << ") & (y" << identifier << "'=y" << agentName << "-1) & (" << identifier << "PickedUp'=false);\n"; + actionName = "[" + agentName + "_drop_" + identifier + "_west]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t" << actionName << " " << " true -> (x" << identifier << "'=x" << agentName << "-1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n"; + actionName = "[" + agentName + "_drop_" + identifier + "_south]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t" << actionName << " " << " true -> (x" << identifier << "'=x" << agentName << ") & (y" << identifier << "'=y" << agentName << "+1) & (" << identifier << "PickedUp'=false);\n"; + actionName = "[" + agentName + "_drop_" + identifier + "_east]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t" << actionName << " " << " ttrue -> (x" << identifier << "'=x" << agentName << "+1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n"; } void PrismModulesPrinter::printDoorModule(const cell &door, const bool &opened) { @@ -165,13 +186,21 @@ namespace prism { } void PrismModulesPrinter::printLockedDoorActions(const std::string &agentName, const std::string &identifier) { - os << "\t[" << agentName << "_unlock_" << identifier << "] !" << identifier << "Open -> (" << identifier << "Open'=true);\n"; - os << "\t[" << agentName << "_close_" << identifier << "] " << identifier << "Open -> (" << identifier << "Open'=false);\n"; + std::string actionName = "[" + agentName + "_unlock_" + identifier + "]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t" << actionName << " !" << identifier << "Open -> (" << identifier << "Open'=true);\n"; + actionName = "[" + agentName + "_close_" + identifier + "]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t" << actionName << " " << identifier << "Open -> (" << identifier << "Open'=false);\n"; } void PrismModulesPrinter::printUnlockedDoorActions(const std::string &agentName, const std::string &identifier) { - os << "\t[" << agentName << "_open_" << identifier << "] !" << identifier << "Open -> (" << identifier << "Open'=true);\n"; - os << "\t[" << agentName << "_close_" << identifier << "] " << identifier << "Open -> (" << identifier << "Open'=false);\n"; + std::string actionName = "[" + agentName + "_open_" + identifier + "]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t !" << identifier << "Open -> (" << identifier << "Open'=true);\n"; + actionName = "[" + agentName + "_close_" + identifier + "]"; + agentNameActionMap.at(agentName).insert({NOFAULT, actionName}); + os << "\t" << agentName << " " << identifier << "Open -> (" << identifier << "Open'=false);\n"; } void PrismModulesPrinter::printRobotModule(const AgentName &agentName, const coordinates &initialPosition) { @@ -180,8 +209,6 @@ namespace prism { os << "\ty" << agentName << " : [0.." << maxBoundaries.first << "] init " << initialPosition.first << ";\n"; os << "\tview" << agentName << " : [0..3] init 1;\n"; - if(faultyBehaviour()) os << "\tpreviousAction" << agentName << " : [-1..2] init -1;\n"; - printTurnActionsForRobot(agentName); printMovementActionsForRobot(agentName); if(slipperyBehaviour()) printSlipperyMovementActionsForRobot(agentName); @@ -221,23 +248,23 @@ namespace prism { } void PrismModulesPrinter::printPortableObjectActionsForRobot(const std::string &a, const std::string &i) { - actionStream << "\t[" << a << "_pickup_" << i << "] " << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << " !" << a << "IsCarrying & " << a << "CannotMove" << i << " -> (" << a << "Carrying" << i << "'=true);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_north]\t" << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_west] \t" << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_south]\t" << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n"; - actionStream << "\t[" << a << "_drop_" << i << "_east] \t" << faultyBehaviourGuard(a, NOFAULT) << moveGuard(a) << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_pickup_" << i << "] " << " !" << a << "IsCarrying & " << a << "CannotMove" << i << " -> (" << a << "Carrying" << i << "'=true);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_north]\t" << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_west] \t" << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_south]\t" << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n"; + actionStream << "\t[" << a << "_drop_" << i << "_east] \t" << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n"; actionStream << "\n"; } void PrismModulesPrinter::printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier) { - actionStream << "\t[" << agentName << "_open_" << identifier << "] " << faultyBehaviourGuard(agentName, NOFAULT) << moveGuard(agentName) << agentName << "CannotMove" << identifier << " -> true;\n"; - actionStream << "\t[" << agentName << "_close_" << identifier << "] " << faultyBehaviourGuard(agentName, NOFAULT) << moveGuard(agentName) << agentName << "IsNextTo" << identifier << " -> true;\n"; + actionStream << "\t[" << agentName << "_open_" << identifier << "] " << agentName << "CannotMove" << identifier << " -> true;\n"; + actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " -> true;\n"; actionStream << "\n"; } void PrismModulesPrinter::printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key) { - actionStream << "\t[" << agentName << "_unlock_" << identifier << "] " << faultyBehaviourGuard(agentName, NOFAULT) << moveGuard(agentName) << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; - actionStream << "\t[" << agentName << "_close_" << identifier << "] " << faultyBehaviourGuard(agentName, NOFAULT) << moveGuard(agentName) << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; + actionStream << "\t[" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; + actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n"; actionStream << "\n"; } @@ -254,32 +281,24 @@ namespace prism { actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, {1.0, "(x"+a+"'=x"+a+"-1)"}); } - std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) const { - return "\t[" + a + "_move_" + direction + "]" + moveGuard(a) + viewVariable(a, viewDirection) + faultyBehaviourGuard(a, FORWARD) + " !" + a + "IsOnSlippery & !" + a + "IsOnLava & !" + a + "IsOnGoal & !" + a + "CannotMove" + direction + "Wall &!" + a + "CannotMoveConditionally -> "; + std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) { + std::string actionName = "[" + a + "_move_" + direction + "]"; + agentNameActionMap.at(a).insert({FORWARD, actionName}); + return "\t" + actionName + " " + viewVariable(a, viewDirection) + " !" + a + "IsOnSlippery & !" + a + "IsOnLava & !" + a + "IsOnGoal & !" + a + "CannotMove" + direction + "Wall &!" + a + "CannotMoveConditionally -> "; } std::string PrismModulesPrinter::printMovementUpdate(const AgentName &a, const update &u) const { - if(!faultyBehaviour()) { - return updateToString(u) + ";\n"; - } else { - update nonFaultyUpdate = {u.first - faultyProbability, u.second + " & " + faultyBehaviourUpdate(a, NOFAULT)}; - update faultyUpdate = {faultyProbability, u.second + " & " + faultyBehaviourUpdate(a, FORWARD)}; - return updateToString(nonFaultyUpdate) + " + " + updateToString(faultyUpdate) + ";\n"; - } + return updateToString(u) + ";\n"; } - std::string PrismModulesPrinter::printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond) const { - return "\t[" + a + "_turn_" + direction + "]" + moveGuard(a) + faultyBehaviourGuard(a, actionId) + cond + " -> "; + std::string PrismModulesPrinter::printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond) { + std::string actionName = "[" + a + "_turn_" + direction + "]"; + agentNameActionMap.at(a).insert({actionId, actionName}); + return "\t" + actionName + " " + cond + " -> "; } std::string PrismModulesPrinter::printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const { - if(!faultyBehaviour()) { - return updateToString(u) + ";\n"; - } else { - update nonFaultyUpdate = {u.first - faultyProbability, u.second + " & " + faultyBehaviourUpdate(a, NOFAULT)}; - update faultyUpdate = {faultyProbability, u.second + " & " + faultyBehaviourUpdate(a, actionId)}; - return updateToString(nonFaultyUpdate) + " + " + updateToString(faultyUpdate) + ";\n"; - } + return updateToString(u) + ";\n"; } void PrismModulesPrinter::printSlipperyMovementActionsForRobot(const AgentName &a) { @@ -406,59 +425,73 @@ namespace prism { } void PrismModulesPrinter::printSlipperyTurnActionsForNorth(const AgentName &a) { - actionStream << printSlipperyTurnGuard(a, "right", { "CanSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, northUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "right", {"!CanSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { "CanSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!CanSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, northUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, northUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, { "CanSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, { "CanSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, northUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!CanSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!CanSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; } void PrismModulesPrinter::printSlipperyTurnActionsForEast(const AgentName &a) { - actionStream << printSlipperyTurnGuard(a, "right", { "CanSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, eastUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "right", {"!CanSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { "CanSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!CanSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, eastUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, eastUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, { "CanSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, { "CanSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, eastUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!CanSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!CanSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; } void PrismModulesPrinter::printSlipperyTurnActionsForSouth(const AgentName &a) { - actionStream << printSlipperyTurnGuard(a, "right", { "CanSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, southUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "right", {"!CanSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { "CanSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!CanSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, southUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, southUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, { "CanSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, { "CanSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, southUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!CanSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!CanSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; } void PrismModulesPrinter::printSlipperyTurnActionsForWest(const AgentName &a) { - actionStream << printSlipperyTurnGuard(a, "right", { "CanSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, westUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "right", {"!CanSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { "CanSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!CanSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, westUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", { "CanSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, westUpdate(a)} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; - actionStream << printSlipperyTurnGuard(a, "left", {"!CanSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, { "CanSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"-1)"}, {1 - probIntended, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, { "CanSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"=3)"}, {1 - probIntended, westUpdate(a)} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!CanSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << "\n"; + actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!CanSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"=3)"} }) << "\n"; } - std::string PrismModulesPrinter::printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector &guards) const { - return "\t[" + a + "_move_" + direction + "] " + moveGuard(a) + viewVariable(a, viewDirection) + faultyBehaviourGuard(a, FORWARD) + a + "IsOnSlippery" + direction + " & " + buildConjunction(a, guards) + " -> "; + std::string PrismModulesPrinter::printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector &guards) { + std::string actionName = "[" + a + "_move_" + direction + "]"; + agentNameActionMap.at(a).insert({FORWARD, actionName}); + return "\t" + actionName + " " + viewVariable(a, viewDirection) + a + "IsOnSlippery" + direction + " & " + buildConjunction(a, guards) + " -> "; } std::string PrismModulesPrinter::printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const { - return updatesToString(u, a, FORWARD); + return updatesToString(u); } - std::string PrismModulesPrinter::printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const std::vector &guards, const std::string &cond) const { - return "\t[" + a + "_turn_" + direction + "] " + buildConjunction(a, guards) + " & " + cond + " -> "; + std::string PrismModulesPrinter::printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::vector &guards, const std::string &cond) { + std::string actionName = "[" + a + "_turn_" + direction + "]"; + agentNameActionMap.at(a).insert({actionId, actionName}); + return "\t" + actionName + " " + buildConjunction(a, guards) + " & " + cond + " -> "; } std::string PrismModulesPrinter::printSlipperyTurnUpdate(const AgentName &a, const updates &u) { - return updatesToString(u, a, NOFAULT); + return updatesToString(u); + } + + void PrismModulesPrinter::printFaultyMovementModule(const AgentName &a) { + os << "\nmodule " << a << "FaultyBehaviour" << std::endl; + os << "\tpreviousAction" << a << " : [-1..2] init -1;\n"; + + for(const auto [actionId, actionName] : agentNameActionMap.at(a)) { + os << "\t" << actionName << faultyBehaviourGuard(a, actionId) << " -> " << faultyBehaviourUpdate(a, actionId) << ";\n"; + } + os << "endmodule\n\n"; } std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) { @@ -587,9 +620,9 @@ namespace prism { std::string PrismModulesPrinter::faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const { if(faultyBehaviour()) { if(actionId == NOFAULT) { - return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + ") & "; + return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + ") "; } else { - return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + " | previousAction" + agentName + "=" + std::to_string(actionId) + ") & "; + return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + " | previousAction" + agentName + "=" + std::to_string(actionId) + ") "; } } else { return ""; @@ -597,7 +630,11 @@ namespace prism { } std::string PrismModulesPrinter::faultyBehaviourUpdate(const AgentName &agentName, const ActionId &actionId) const { - return "(previousAction" + agentName + "'=" + std::to_string(actionId) + ")"; + if(actionId != NOFAULT) { + return updatesToString({ {1 - faultyProbability, "(previousAction" + agentName + "'=" + std::to_string(NOFAULT) + ")"}, {faultyProbability, "(previousAction" + agentName + "'=" + std::to_string(actionId) + ")" } }); + } else { + return "true"; + } } std::string PrismModulesPrinter::moveUpdate(const AgentName &agentName) const { @@ -609,7 +646,7 @@ namespace prism { ""; } - std::string PrismModulesPrinter::updatesToString(const updates &updates, const AgentName &a, const ActionId &actionId) const { + std::string PrismModulesPrinter::updatesToString(const updates &updates) const { if(updates.empty()) return "true"; std::string updatesString = ""; bool first = true; @@ -617,7 +654,6 @@ namespace prism { if(first) first = false; else updatesString += " + "; updatesString += updateToString(update); - //if(faultyBehaviour()) updatesString += "&" + faultyBehaviourUpdate(a, actionId); } return updatesString; } diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 1e71404..4c5eb8e 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "MinigridGrammar.h" #include "PrismPrinter.h" @@ -83,15 +84,17 @@ namespace prism { void printSlipperyTurnActionsForSouth(const AgentName &a); void printSlipperyTurnActionsForWest(const AgentName &a); - std::string printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) const; + std::string printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection); std::string printMovementUpdate(const AgentName &a, const update &update) const; - std::string printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond = "") const; + std::string printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond = ""); std::string printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const; - std::string printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector &guards) const; + std::string printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector &guards); std::string printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const; - std::string printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const std::vector &guards, const std::string &cond) const; + std::string printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::vector &guards, const std::string &cond); std::string printSlipperyTurnUpdate(const AgentName &a, const updates &u); + void printFaultyMovementModule(const AgentName &a); + bool anyPortableObject() const; bool faultyBehaviour() const; bool slipperyBehaviour() const; @@ -99,7 +102,7 @@ namespace prism { std::string faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const; std::string faultyBehaviourUpdate(const AgentName &agentName, const ActionId &actionId) const; std::string moveUpdate(const AgentName &agentName) const; - std::string updatesToString(const updates &updates, const AgentName &a, const ActionId &actionId) const; + std::string updatesToString(const updates &updates) const; std::string updateToString(const update &u) const; std::string viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView = true) const; @@ -129,5 +132,7 @@ namespace prism { std::vector configuration; std::map viewDirectionMapping; std::vector viewDirections = {0, 1, 2, 3}; + + std::map>> agentNameActionMap; }; } From ff0369cfb14a3071844ae2b62934a332f21a91f0 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 15:54:40 +0100 Subject: [PATCH 30/38] added move module as arbiter --- util/PrismModulesPrinter.cpp | 27 ++++++++++++++++++--------- util/PrismModulesPrinter.h | 1 + 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 988df47..2c9af3f 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -494,6 +494,18 @@ namespace prism { os << "endmodule\n\n"; } + void PrismModulesPrinter::printMoveModule() { + os << "\nmodule " << "Arbiter" << std::endl; + os << "\tclock : [0.." << agentIndexMap.size() - 1 << "] init 0;\n"; + + for(const auto [agentName, actions] : agentNameActionMap) { + for(const auto [actionId, actionName] : actions) { + os << "\t" << actionName << " " << moveGuard(agentName) << " -> " << moveUpdate(agentName) << ";\n"; + } + } + os << "endmodule\n\n"; + } + std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) { for (auto& config : configuration) { if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) { @@ -613,10 +625,6 @@ namespace prism { return os; } - std::string PrismModulesPrinter::moveGuard(const AgentName &agentName) const { - return isGame() ? " move=" + std::to_string(agentIndexMap.at(agentName)) + " & " : " "; - } - std::string PrismModulesPrinter::faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const { if(faultyBehaviour()) { if(actionId == NOFAULT) { @@ -637,13 +645,14 @@ namespace prism { } } + std::string PrismModulesPrinter::moveGuard(const AgentName &agentName) const { + return "clock=" + std::to_string(agentIndexMap.at(agentName)); + } + std::string PrismModulesPrinter::moveUpdate(const AgentName &agentName) const { size_t agentIndex = agentIndexMap.at(agentName); - return isGame() ? - (agentIndex == numberOfPlayer - 1) ? - " & (move'=0) " : - " & (move'=" + std::to_string(agentIndex + 1) + ") " : - ""; + return (agentIndex == numberOfPlayer - 1) ? " & (clock'=0) " : " & (clock'=" + std::to_string(agentIndex + 1) + ") "; + } std::string PrismModulesPrinter::updatesToString(const updates &updates) const { diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 4c5eb8e..3284cfe 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -94,6 +94,7 @@ namespace prism { std::string printSlipperyTurnUpdate(const AgentName &a, const updates &u); void printFaultyMovementModule(const AgentName &a); + void printMoveModule(); bool anyPortableObject() const; bool faultyBehaviour() const; From aca9ee719caac4b59cdd4ceb16df67d5143e60d8 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 16:42:58 +0100 Subject: [PATCH 31/38] removed unused code --- util/Grid.cpp | 62 +++++++++------------------------------------------ 1 file changed, 11 insertions(+), 51 deletions(-) diff --git a/util/Grid.cpp b/util/Grid.cpp index eb4ef38..b2dd09f 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -135,7 +135,6 @@ void Grid::printToPrism(std::ostream& os, std::vector& configurat cells walkable = floor; walkable.insert(walkable.end(), goals.begin(), goals.end()); walkable.insert(walkable.end(), boxes.begin(), boxes.end()); - walkable.insert(walkable.end(), adversaries.begin(), adversaries.end()); walkable.insert(walkable.end(), lava.begin(), lava.end()); walkable.insert(walkable.end(), keys.begin(), keys.end()); walkable.insert(walkable.end(), balls.begin(), balls.end()); @@ -165,66 +164,27 @@ void Grid::printToPrism(std::ostream& os, std::vector& configurat for(const auto &agentName : agentNames) { formulas.print(agentName); } - if(modelType == prism::ModelType::SMG) modules.printGlobalMoveVariable(os, agentNameAndPositionMap.size()); - std::vector constants {"const double prop_zero = 0/9;", - "const double prop_intended = 6/9;", - "const double prop_turn_intended = 6/9;", - "const double prop_displacement = 3/9;", - "const double prop_turn_displacement = 3/9;", - "const int width = " + std::to_string(maxBoundaries.first) + ";", - "const int height = " + std::to_string(maxBoundaries.second) + ";" - }; - modules.printConstants(os, constants); + //std::vector constants {"const double prop_zero = 0/9;", + // "const double prop_intended = 6/9;", + // "const double prop_turn_intended = 6/9;", + // "const double prop_displacement = 3/9;", + // "const double prop_turn_displacement = 3/9;", + // "const int width = " + std::to_string(maxBoundaries.first) + ";", + // "const int height = " + std::to_string(maxBoundaries.second) + ";" + // }; + //modules.printConstants(os, constants); modules.print(); - //modules.printInitStruct(os, agentNameAndPositionMap, keyNameAndPositionMap, lockedDoors, unlockedDoors, modelType); - - - //size_t agentIndex = 0; - //for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition, agentIndex++) { - // AgentName agentName = agentNameAndPosition->first; - // //std::cout << "Agent Name: " << agentName << std::endl; - // bool agentWithView = std::find(gridOptions.agentsWithView.begin(), gridOptions.agentsWithView.end(), agentName) != gridOptions.agentsWithView.end(); - // bool agentWithProbabilisticBehaviour = std::find(gridOptions.agentsWithProbabilisticBehaviour.begin(), gridOptions.agentsWithProbabilisticBehaviour.end(), agentName) != gridOptions.agentsWithProbabilisticBehaviour.end(); - // std::set slipperyActions; // TODO AGENT POSITION INITIALIZATIN - // if(agentWithProbabilisticBehaviour) modules.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, gridOptions.probabilitiesForActions); - // else modules.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, {} ,faultyProbability); - // for(auto const& c : slipperyNorth) { - // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North); - // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North); - - // } - // for(auto const& c : slipperyEast) { - // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East); - // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East); - // } - // for(auto const& c : slipperySouth) { - // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South); - // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South); - // } - // for(auto const& c : slipperyWest) { - // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West); - // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West); - // } - - // modules.printEndmodule(os); - - // if(modelType == prism::ModelType::SMG) { - // if(agentWithProbabilisticBehaviour) modules.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, gridOptions.probabilitiesForActions, slipperyActions); - // else modules.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, {}, slipperyActions); - // } //if(!stateRewards.empty()) { // modules.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles); //} + //if (!configuration.empty()) { + // modules.printConfiguration(os, configuration); //} - - if (!configuration.empty()) { - modules.printConfiguration(os, configuration); - } } From 48dcee2a594d9b52d39b40c97dccb5982bd7f16e Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 16:44:03 +0100 Subject: [PATCH 32/38] small cleanup --- util/PrismModulesPrinter.cpp | 58 ++++-------------------------------- 1 file changed, 5 insertions(+), 53 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 2c9af3f..f837ced 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -17,7 +17,7 @@ std::string westUpdate(const AgentName &a) { return "(x"+a+"'=x"+a+"-1)"; } namespace prism { PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map &slipperyTiles, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector config, const float probIntended, const float faultyProbability) - : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), agentNameAndPositionMap(agentNameAndPositionMap), configuration(config), probIntended(probIntended), faultyProbability(faultyProbability), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) { + : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), agentNameAndPositionMap(agentNameAndPositionMap), configuration(config), probIntended(probIntended), faultyProbability(faultyProbability) { numberOfPlayer = agentNameAndPositionMap.size(); size_t index = 0; for(auto begin = agentNameAndPositionMap.begin(); begin != agentNameAndPositionMap.end(); begin++, index++) { @@ -25,7 +25,7 @@ namespace prism { } } - std::ostream& PrismModulesPrinter::printModelType(const ModelType &modelType) { + void PrismModulesPrinter::printModelType(const ModelType &modelType) { switch(modelType) { case(ModelType::MDP): os << "mdp"; @@ -35,7 +35,6 @@ namespace prism { break; } os << "\n\n"; - return os; } std::ostream& PrismModulesPrinter::print() { @@ -73,68 +72,21 @@ namespace prism { } - std::ostream& PrismModulesPrinter::printConfiguration(std::ostream& os, const std::vector& configurations) { + void PrismModulesPrinter::printConfiguration(const std::vector& configurations) { for (auto& configuration : configurations) { if (configuration.overwrite_ || configuration.type_ == ConfigType::Module) { continue; } os << configuration.expression_ << std::endl; } - return os; } - std::ostream& PrismModulesPrinter::printConstants(std::ostream &os, const std::vector &constants) { + void PrismModulesPrinter::printConstants(const std::vector &constants) { for (auto& constant : constants) { os << constant << std::endl; } - return os; - } - - std::ostream& PrismModulesPrinter::printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType) { - /* - os << "init\n"; - os << "\t"; - - bool first = true; - for (auto const& agent : agents) { - if (first) first = false; - else os << " & "; - os << "(!" << agent.first << "IsInGoal & !" << agent.first << "IsInLava & !" << agent.first << "Done & !" << agent.first << "IsOnWall & "; - os << "x" << agent.first << "=" << agent.second.second << " & y" << agent.first << "=" << agent.second.first << ")"; - os << " & !" << agent.first << "_is_carrying_object"; - // os << " & ( !AgentIsOnSlippery ) "; - } - - for (auto const& key : keys) { - os << " & ( !" << agent.first << "_has_" << key.first << "_key )"; - } - } - - for (auto const& key : keys) { - os << " & ( xKey" << key.first << "="<< key.second.second<< ")"; - os << " & ( yKey" << key.first << "=" << key.second.first << ")"; - } - - for (auto const& locked : lockedDoors) { - os << " & (Door" << locked.getColor() << "locked & !Door" << locked.getColor() << "open)"; - } - - for (auto const& unlocked : unlockedDoors) { - os << " & (!Door" << unlocked.getColor() << "locked & !Door" << unlocked.getColor() << "open)"; - } - - if (modelType == ModelType::SMG) { - os << " & move=0"; - } - - - os << "\nendinit\n\n"; - - */ - return os; } - void PrismModulesPrinter::printPortableObjectModule(const cell &object) { std::string identifier = capitalize(object.getColor()) + object.getType(); os << "\nmodule " << identifier << std::endl; @@ -506,7 +458,7 @@ namespace prism { os << "endmodule\n\n"; } - std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) { + void PrismModulesPrinter::printConfiguredActions(const AgentName &agentName) { for (auto& config : configuration) { if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) { os << config.expression_ ; From 6de02b7ab88443a315bf2adeb6e5657f4c918ec0 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 16:44:40 +0100 Subject: [PATCH 33/38] moved PrismModulesPrinter methods to private --- util/PrismModulesPrinter.h | 47 ++++++++------------------------------ 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 3284cfe..6a68b2b 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -20,43 +20,7 @@ namespace prism { std::ostream& print(); - std::ostream& printModelType(const ModelType &modelType); - - - std::ostream& printConstants(std::ostream &os, const std::vector &constants); - /* - * Representation for Slippery Tile. - * -) North: Slips from North to South - * -) East: Slips from East to West - * -) South: Slips from South to North - * -) West: Slips from West to East - */ - enum class SlipperyType { North, East, South, West }; - - /* - * Prints Slippery on move action. - * - * @param neighborhood: Information of wall-blocks in 8-neighborhood { n, nw, e, se, s, sw, w, nw }. If entry is false, then corresponding neighboorhood position is a wall. - * @param orientation: Information of slippery type (either north, south, east, west). - */ - std::ostream& printSlipperyMove(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation); - - /* - * Prints Slippery on turn action. - * - * @param neighborhood: Information of wall-blocks in 8-neighborhood { n, nw, e, se, s, sw, w, nw }. If entry is false, then corresponding neighboorhood position is a wall. - * @param orientation: Information of slippery type (either north, south, east, west). - */ - std::ostream& printSlipperyTurn(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set &slipperyActions, const std::array& neighborhood, SlipperyType orientation); - - std::ostream& printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType); - std::ostream& printDoneActions(std::ostream &os, const AgentName &agentName); - std::ostream& printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector &probabilities = {}, const std::set &slipperyActions = {}); - std::ostream& printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer); - std::ostream& printRewards(std::ostream &os, const AgentName &agentName, const std::map &stateRewards, const cells &lava, const cells &goals, const std::map &backgroundTiles); - - std::ostream& printConfiguration(std::ostream &os, const std::vector& configurations); - std::ostream& printConfiguredActions(std::ostream &os, const AgentName &agentName); + void printModelType(const ModelType &modelType); bool isGame() const; @@ -96,6 +60,15 @@ namespace prism { void printFaultyMovementModule(const AgentName &a); void printMoveModule(); + void printConstants(const std::vector &constants); + + void printDoneActions(const AgentName &agentName); + void printPlayerStruct(const AgentName &agentName, const std::vector &probabilities = {}, const std::set &slipperyActions = {}); + void printRewards(const AgentName &agentName, const std::map &stateRewards, const cells &lava, const cells &goals, const std::map &backgroundTiles); + + void printConfiguration(const std::vector& configurations); + void printConfiguredActions(const AgentName &agentName); + bool anyPortableObject() const; bool faultyBehaviour() const; bool slipperyBehaviour() const; From cdd9611f8b46f313bd9900b3a43b2b3844a05de5 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 16:45:02 +0100 Subject: [PATCH 34/38] removed viewVariable --- util/PrismModulesPrinter.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index 6a68b2b..eefaef8 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -79,7 +79,7 @@ namespace prism { std::string updatesToString(const updates &updates) const; std::string updateToString(const update &u) const; - std::string viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView = true) const; + std::string viewVariable(const AgentName &agentName, const size_t &agentDirection) const; std::string buildConjunction(const AgentName &a, std::vector formulae) const; @@ -104,7 +104,6 @@ namespace prism { float const faultyProbability; float const probIntended; std::vector configuration; - std::map viewDirectionMapping; std::vector viewDirections = {0, 1, 2, 3}; std::map>> agentNameActionMap; From 6cfa9a619ba536e3c2ad31364295b447f6229330 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 16:45:15 +0100 Subject: [PATCH 35/38] include agent stuck action for faultyBehaviour --- util/PrismModulesPrinter.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index f837ced..6334ea7 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -227,10 +227,19 @@ namespace prism { } void PrismModulesPrinter::printMovementActionsForRobot(const AgentName &a) { - actionStream << printMovementGuard(a, "North", 3) << printMovementUpdate(a, {1.0, "(y"+a+"'=y"+a+"-1)"}); - actionStream << printMovementGuard(a, "East", 0) << printMovementUpdate(a, {1.0, "(x"+a+"'=x"+a+"+1)"}); - actionStream << printMovementGuard(a, "South", 1) << printMovementUpdate(a, {1.0, "(y"+a+"'=y"+a+"+1)"}); - actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, {1.0, "(x"+a+"'=x"+a+"-1)"}); + actionStream << printMovementGuard(a, "North", 3) << printMovementUpdate(a, {1.0, northUpdate(a)}); + actionStream << printMovementGuard(a, "East", 0) << printMovementUpdate(a, {1.0, eastUpdate(a)}); + actionStream << printMovementGuard(a, "South", 1) << printMovementUpdate(a, {1.0, southUpdate(a)}); + actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, {1.0, westUpdate(a)}); + if(faultyBehaviour()) { + std::string actionName = "[" + a + "_stuck]"; + agentNameActionMap.at(a).insert({FORWARD, actionName}); + actionStream << "\t" << actionName << " " << "previousAction" << a << "=" << std::to_string(FORWARD); + actionStream << " & ((view" << a << "=0 & " << a << "CannotMoveEastWall) |"; + actionStream << " (view" << a << "=1 & " << a << "CannotMoveSouthWall) |"; + actionStream << " (view" << a << "=2 & " << a << "CannotMoveWestWall) |"; + actionStream << " (view" << a << "=3 & " << a << "CannotMoveNorthWall) ) -> true;\n"; + } } std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) { @@ -623,8 +632,8 @@ namespace prism { return std::to_string(u.first) + ": " + u.second; } - std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView) const { - return agentWithView ? " view" + agentName + "=" + std::to_string(agentDirection) + " & " : " "; + std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection) const { + return "view" + agentName + "=" + std::to_string(agentDirection) + " & "; } bool PrismModulesPrinter::anyPortableObject() const { From 8d33f43c7e4bb32258d79cb64b76a0455ed133d6 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 16:46:11 +0100 Subject: [PATCH 36/38] refactored arbiter for smgs --- util/PrismModulesPrinter.cpp | 49 +++++++++--------------------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 6334ea7..4e73af2 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -62,6 +62,10 @@ namespace prism { printRobotModule(agentName, initialPosition); } + if(agentNameAndPositionMap.size() > 1) { + printMoveModule(); + } + if(faultyBehaviour()) { for(const auto [agentName, initialPosition] : agentNameAndPositionMap) { printFaultyMovementModule(agentName); @@ -475,52 +479,24 @@ namespace prism { } os << "\n"; - - return os; } - std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName) { + void PrismModulesPrinter::printDoneActions(const AgentName &agentName) { os << "\t[" << agentName << "_done]" << moveGuard(agentName) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n"; - return os; } - std::ostream& PrismModulesPrinter::printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector &probabilities, const std::set &slipperyActions) { + void PrismModulesPrinter::printPlayerStruct(const AgentName &agentName, const std::vector &probabilities, const std::set &slipperyActions) { os << "player " << agentName << "\n\t"; bool first = true; - std::list allActions = { "_move_north", "_move_east", "_move_south", "_move_west" }; - std::list movementActions = allActions; - for(auto const& probability : probabilities) { - std::string percentageString = std::to_string((int)(100 * probability)); - for(auto const& movement : movementActions) { - allActions.push_back(movement + "_" + percentageString); - } - } - if(agentWithView) { - allActions.push_back("_turn_left"); - allActions.push_back("_turn_right"); - } else { - allActions.push_back("_turns"); - } - - for(auto const& action : allActions) { - if(first) first = false; else os << ", "; - os << "[" << agentName << action << "]"; - } - for(auto const& action : slipperyActions) { - os << ", " << action; + for(const auto [actionId, actionName] : agentNameActionMap.at(agentName)) { + if(first) first = false; + else os << ", "; + os << actionName; } - - os << ", [" << agentName << "_done]"; os << "\nendplayer\n"; - return os; } - std::ostream& PrismModulesPrinter::printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer) { - os << "\nglobal move : [0.." << std::to_string(numberOfPlayer - 1) << "];\n\n"; - return os; - } - - std::ostream& PrismModulesPrinter::printRewards(std::ostream &os, const AgentName &agentName, const std::map &stateRewards, const cells &lava, const cells &goals, const std::map &backgroundTiles) { + void PrismModulesPrinter::printRewards(const AgentName &agentName, const std::map &stateRewards, const cells &lava, const cells &goals, const std::map &backgroundTiles) { if(lava.size() != 0) { os << "rewards \"" << agentName << "SafetyNoBFS\"\n"; os << "\t" < Date: Mon, 8 Jan 2024 16:50:00 +0100 Subject: [PATCH 37/38] removed unused function --- util/Grid.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/util/Grid.cpp b/util/Grid.cpp index b2dd09f..294ddd1 100644 --- a/util/Grid.cpp +++ b/util/Grid.cpp @@ -186,18 +186,3 @@ void Grid::printToPrism(std::ostream& os, std::vector& configurat // modules.printConfiguration(os, configuration); //} } - - -std::array Grid::getWalkableDirOf8Neighborhood(cell c) /* const */ { - return (std::array) - { - !isBlocked(c.getNorth()), - !isBlocked(c.getNorth(allGridCells).getEast()), - !isBlocked(c.getEast()), - !isBlocked(c.getSouth(allGridCells).getEast()), - !isBlocked(c.getSouth()), - !isBlocked(c.getSouth(allGridCells).getWest()), - !isBlocked(c.getWest()), - !isBlocked(c.getNorth(allGridCells).getWest()) - }; -} From 54af53e03943ecefc86bee96cb4b267bb7c97cb0 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 8 Jan 2024 16:50:21 +0100 Subject: [PATCH 38/38] print player structs --- util/PrismModulesPrinter.cpp | 8 +++++++- util/PrismModulesPrinter.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/util/PrismModulesPrinter.cpp b/util/PrismModulesPrinter.cpp index 4e73af2..430dd3c 100644 --- a/util/PrismModulesPrinter.cpp +++ b/util/PrismModulesPrinter.cpp @@ -72,6 +72,12 @@ namespace prism { } } + if(agentNameAndPositionMap.size() > 1) { + for(const auto [agentName, index] : agentIndexMap) { + printPlayerStruct(agentName); + } + } + return os; } @@ -485,7 +491,7 @@ namespace prism { os << "\t[" << agentName << "_done]" << moveGuard(agentName) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n"; } - void PrismModulesPrinter::printPlayerStruct(const AgentName &agentName, const std::vector &probabilities, const std::set &slipperyActions) { + void PrismModulesPrinter::printPlayerStruct(const AgentName &agentName) { os << "player " << agentName << "\n\t"; bool first = true; for(const auto [actionId, actionName] : agentNameActionMap.at(agentName)) { diff --git a/util/PrismModulesPrinter.h b/util/PrismModulesPrinter.h index eefaef8..a66d68f 100644 --- a/util/PrismModulesPrinter.h +++ b/util/PrismModulesPrinter.h @@ -63,7 +63,7 @@ namespace prism { void printConstants(const std::vector &constants); void printDoneActions(const AgentName &agentName); - void printPlayerStruct(const AgentName &agentName, const std::vector &probabilities = {}, const std::set &slipperyActions = {}); + void printPlayerStruct(const AgentName &agentName); void printRewards(const AgentName &agentName, const std::map &stateRewards, const cells &lava, const cells &goals, const std::map &backgroundTiles); void printConfiguration(const std::vector& configurations);