From 0521014b466b17207055b21457c9f78b27ce5246 Mon Sep 17 00:00:00 2001 From: Joshua Mayhugh <123776276+jmayhugh1@users.noreply.github.com> Date: Mon, 17 Feb 2025 19:49:18 -0600 Subject: [PATCH 1/3] started ppsat.py --- agents.ipynb | 514 ++++++++++- logic.ipynb | 4 +- logic4e_modify_v01.py | 1969 +++++++++++++++++++++++++++++++++++++++++ ppsat.py | 155 ++++ 4 files changed, 2611 insertions(+), 31 deletions(-) create mode 100644 logic4e_modify_v01.py create mode 100644 ppsat.py diff --git a/agents.ipynb b/agents.ipynb index 636df75e3..76b35c41a 100644 --- a/agents.ipynb +++ b/agents.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -43,9 +43,152 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "
\n", + "class Agent(Thing):\n",
+ " """An Agent is a subclass of Thing with one required instance attribute \n",
+ " (aka slot), .program, which should hold a function that takes one argument,\n",
+ " the percept, and returns an action. (What counts as a percept or action \n",
+ " will depend on the specific environment in which the agent exists.)\n",
+ " Note that 'program' is a slot, not a method. If it were a method, then the\n",
+ " program could 'cheat' and look at aspects of the agent. It's not supposed\n",
+ " to do that: the program can only look at the percepts. An agent program\n",
+ " that needs a model of the world (and of the agent itself) will have to\n",
+ " build and maintain its own model. There is an optional slot, .performance,\n",
+ " which is a number giving the performance measure of the agent in its\n",
+ " environment."""\n",
+ "\n",
+ " def __init__(self, program=None):\n",
+ " self.alive = True\n",
+ " self.bump = False\n",
+ " self.holding = []\n",
+ " self.performance = 0\n",
+ " if program is None or not isinstance(program, collections.abc.Callable):\n",
+ " print("Can't find a valid program for {}, falling back to default.".format(self.__class__.__name__))\n",
+ "\n",
+ " def program(percept):\n",
+ " return eval(input('Percept={}; action? '.format(percept)))\n",
+ "\n",
+ " self.program = program\n",
+ "\n",
+ " def can_grab(self, thing):\n",
+ " """Return True if this agent can grab this thing.\n",
+ " Override for appropriate subclasses of Agent and Thing."""\n",
+ " return False\n",
+ "
class Environment:\n",
+ " """Abstract class representing an Environment. 'Real' Environment classes\n",
+ " inherit from this. Your Environment will typically need to implement:\n",
+ " percept: Define the percept that an agent sees.\n",
+ " execute_action: Define the effects of executing an action.\n",
+ " Also update the agent.performance slot.\n",
+ " The environment keeps a list of .things and .agents (which is a subset\n",
+ " of .things). Each agent has a .performance slot, initialized to 0.\n",
+ " Each thing has a .location slot, even though some environments may not\n",
+ " need this."""\n",
+ "\n",
+ " def __init__(self):\n",
+ " self.things = []\n",
+ " self.agents = []\n",
+ "\n",
+ " def thing_classes(self):\n",
+ " return [] # List of classes that can go into environment\n",
+ "\n",
+ " def percept(self, agent):\n",
+ " """Return the percept that the agent sees at this point. (Implement this.)"""\n",
+ " raise NotImplementedError\n",
+ "\n",
+ " def execute_action(self, agent, action):\n",
+ " """Change the world to reflect this action. (Implement this.)"""\n",
+ " raise NotImplementedError\n",
+ "\n",
+ " def default_location(self, thing):\n",
+ " """Default location to place a new thing with unspecified location."""\n",
+ " return None\n",
+ "\n",
+ " def exogenous_change(self):\n",
+ " """If there is spontaneous change in the world, override this."""\n",
+ " pass\n",
+ "\n",
+ " def is_done(self):\n",
+ " """By default, we're done when we can't find a live agent."""\n",
+ " return not any(agent.is_alive() for agent in self.agents)\n",
+ "\n",
+ " def step(self):\n",
+ " """Run the environment for one time step. If the\n",
+ " actions and exogenous changes are independent, this method will\n",
+ " do. If there are interactions between them, you'll need to\n",
+ " override this method."""\n",
+ " if not self.is_done():\n",
+ " actions = []\n",
+ " for agent in self.agents:\n",
+ " if agent.alive:\n",
+ " actions.append(agent.program(self.percept(agent)))\n",
+ " else:\n",
+ " actions.append("")\n",
+ " for (agent, action) in zip(self.agents, actions):\n",
+ " self.execute_action(agent, action)\n",
+ " self.exogenous_change()\n",
+ "\n",
+ " def run(self, steps=1000):\n",
+ " """Run the Environment for given number of time steps."""\n",
+ " for step in range(steps):\n",
+ " if self.is_done():\n",
+ " return\n",
+ " self.step()\n",
+ "\n",
+ " def list_things_at(self, location, tclass=Thing):\n",
+ " """Return all things exactly at a given location."""\n",
+ " if isinstance(location, numbers.Number):\n",
+ " return [thing for thing in self.things\n",
+ " if thing.location == location and isinstance(thing, tclass)]\n",
+ " return [thing for thing in self.things\n",
+ " if all(x == y for x, y in zip(thing.location, location)) and isinstance(thing, tclass)]\n",
+ "\n",
+ " def some_things_at(self, location, tclass=Thing):\n",
+ " """Return true if at least one of the things at location\n",
+ " is an instance of class tclass (or a subclass)."""\n",
+ " return self.list_things_at(location, tclass) != []\n",
+ "\n",
+ " def add_thing(self, thing, location=None):\n",
+ " """Add a thing to the environment, setting its location. For\n",
+ " convenience, if thing is an agent program we make a new agent\n",
+ " for it. (Shouldn't need to override this.)"""\n",
+ " if not isinstance(thing, Thing):\n",
+ " thing = Agent(thing)\n",
+ " if thing in self.things:\n",
+ " print("Can't add the same thing twice")\n",
+ " else:\n",
+ " thing.location = location if location is not None else self.default_location(thing)\n",
+ " self.things.append(thing)\n",
+ " if isinstance(thing, Agent):\n",
+ " thing.performance = 0\n",
+ " self.agents.append(thing)\n",
+ "\n",
+ " def delete_thing(self, thing):\n",
+ " """Remove a thing from the environment."""\n",
+ " try:\n",
+ " self.things.remove(thing)\n",
+ " except ValueError as e:\n",
+ " print(e)\n",
+ " print(" in Environment delete_thing")\n",
+ " print(" Thing to be removed: {} at {}".format(thing, thing.location))\n",
+ " print(" from list: {}".format([(thing, thing.location) for thing in self.things]))\n",
+ " if thing in self.agents:\n",
+ " self.agents.remove(thing)\n",
+ "