{"id":29293,"date":"2023-12-19T20:49:10","date_gmt":"2023-12-19T15:19:10","guid":{"rendered":"https:\/\/tocxten.com\/?page_id=29293"},"modified":"2023-12-19T20:57:35","modified_gmt":"2023-12-19T15:27:35","slug":"model-based-reflex-agent","status":"publish","type":"page","link":"https:\/\/tocxten.com\/index.php\/model-based-reflex-agent\/","title":{"rendered":"Model based Reflex Agent"},"content":{"rendered":"\n<p class=\"has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background has-medium-font-size\"><strong>Example 1 : <\/strong> <strong>Model based Vacuum Cleaner Agent<\/strong><\/p>\n\n\n\n<p><strong>Source Code :<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ModelBasedReflexAgent:\n    def __init__(self):\n        self.state = {\"location\": \"A\", \"status\": \"Clean\"}\n        self.model = {\"A\": {\"Clean\": {\"Suck\": \"Dirty\", \"MoveRight\": \"Clean\"}},\n                      \"B\": {\"Clean\": {\"Suck\": \"Dirty\", \"MoveLeft\": \"Clean\"}}}\n        self.rules = &#91;\n            {\"condition\": {\"location\": \"A\", \"status\": \"Dirty\"}, \"action\": \"Suck\"},\n            {\"condition\": {\"location\": \"A\", \"status\": \"Clean\"}, \"action\": \"MoveRight\"},\n            {\"condition\": {\"location\": \"B\", \"status\": \"Dirty\"}, \"action\": \"Suck\"},\n            {\"condition\": {\"location\": \"B\", \"status\": \"Clean\"}, \"action\": \"MoveLeft\"}\n        ]\n        self.action = None\n\n    def update_state(self, action, percept):\n        if action == \"MoveRight\":\n            self.state&#91;\"location\"] = \"B\"\n        elif action == \"MoveLeft\":\n            self.state&#91;\"location\"] = \"A\"\n        elif action == \"Suck\":\n            self.state&#91;\"status\"] = \"Clean\"\n            percept&#91;\"status\"] = \"Clean\"\n\n    def rule_match(self, state, rules):\n        for rule in rules:\n            if rule&#91;\"condition\"] == state:\n                return rule\n        return None  # Return None if no matching rule is found\n\n    def model_based_reflex_agent(self, percept):\n        self.state = self.update_state(self.action, percept)\n        rule = self.rule_match(self.state, self.rules)\n        if rule is not None:\n            self.action = rule&#91;\"action\"]\n        else:\n            self.action = None\n        return self.action\n\n# Example percept sequence\npercept_sequence = &#91;{\"location\": \"A\", \"status\": \"Dirty\"},\n                    {\"location\": \"A\", \"status\": \"Clean\"},\n                    {\"location\": \"B\", \"status\": \"Dirty\"},\n                    {\"location\": \"B\", \"status\": \"Clean\"}]\n\n# Create and run the agent\nagent = ModelBasedReflexAgent()\n\nfor percept in percept_sequence:\n    action = agent.model_based_reflex_agent(percept)\n    print(f\"Percept: {percept} - Action: {action}\")<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Percept: {'location': 'A', 'status': 'Dirty'} - Action: None\nPercept: {'location': 'A', 'status': 'Clean'} - Action: None\nPercept: {'location': 'B', 'status': 'Dirty'} - Action: None\nPercept: {'location': 'B', 'status': 'Clean'} - Action: None<\/code><\/pre>\n\n\n\n<p class=\"has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background has-medium-font-size\"><strong>Example 2: Linear Model based Simple Agent<\/strong><\/p>\n\n\n\n<p>Source Code <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class LinearModelBasedAgent:\n    def __init__(self, initial_state):\n        self.state = initial_state\n        self.model_coefficient = 2  # Example coefficient for the linear equation\n        self.action = None\n\n    def update_state(self, action):\n        # Simple linear equation: next_state = coefficient * current_state + action\n        next_state = self.model_coefficient * self.state + action\n        self.state = next_state\n        return next_state\n\n    def model_based_agent(self, action):\n        self.action = action\n        next_state = self.update_state(action)\n        return next_state\n\n# Example usage\ninitial_state = 1\nagent = LinearModelBasedAgent(initial_state)\n\n# Perform actions and observe the next states\nactions = &#91;3, -2, 5, 1]\nfor action in actions:\n    next_state = agent.model_based_agent(action)\n    print(f\"Action: {action} - Next State: {next_state}\")<\/code><\/pre>\n\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Action: 3 - Next State: 5\nAction: -2 - Next State: 8\nAction: 5 - Next State: 21\nAction: 1 - Next State: 43<\/code><\/pre>\n\n\n\n<p class=\"has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background has-medium-font-size\"><strong>Example 3:<\/strong> Let&#8217;s consider a simple example of a model-based agent that predicts the temperature for the next day based on historical data and uses a mathematical equation as a model. The agent&#8217;s goal is to decide whether to bring an umbrella depending on the predicted temperature. Here&#8217;s a Python program demonstrating this model-based agent:<\/p>\n\n\n\n<p><strong>Source Code<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TemperaturePredictorAgent:\n    def __init__(self):\n        self.historical_data = &#91;(25, \"No\"), (18, \"Yes\"), (30, \"No\"), (22, \"Yes\")]\n        self.model_coefficients = {\"intercept\": 5, \"slope\": 0.8}\n        self.threshold = 25  # If predicted temperature is above this threshold, don't bring an umbrella\n        self.action = None\n\n    def predict_temperature(self, historical_data):\n        # Simple linear regression model: temperature = intercept + slope * day\n        days = &#91;i + 1 for i in range(len(historical_data))]\n        predicted_temperature = self.model_coefficients&#91;\"intercept\"] + self.model_coefficients&#91;\"slope\"] * (len(historical_data) + 1)\n        return predicted_temperature\n\n    def decide_action(self, predicted_temperature):\n        if predicted_temperature &gt; self.threshold:\n            self.action = \"No Umbrella\"\n        else:\n            self.action = \"Bring Umbrella\"\n\n    def model_based_agent(self):\n        predicted_temperature = self.predict_temperature(self.historical_data)\n        self.decide_action(predicted_temperature)\n        return predicted_temperature, self.action  # Return both temperature and action\n\n# Create and run the agent\nagent = TemperaturePredictorAgent()\ntemperature, action = agent.model_based_agent()\nprint(f\"Predicted Temperature: {round(temperature)}\u00b0C - Action: {action}\")\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Predicted Temperature: 9\u00b0C - Action: Bring Umbrella<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example 1 : Model based Vacuum Cleaner Agent Source Code : Output Example 2: Linear Model based Simple Agent Source Code Output: Example 3: Let&#8217;s consider a simple example of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-29293","page","type-page","status-publish","hentry"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/29293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/comments?post=29293"}],"version-history":[{"count":5,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/29293\/revisions"}],"predecessor-version":[{"id":29305,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/29293\/revisions\/29305"}],"wp:attachment":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/media?parent=29293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}