Answered step by step
Verified Expert Solution
Question
1 Approved Answer
# CMPT 3 1 7 : Problem Class Template # Copyright ( c ) 2 0 1 6 - 2 0 1 9 Michael C
# CMPT : Problem Class Template
# Copyright c Michael C Horsch and Jeff Long,
# Department of Computer Science, University of Saskatchewan
# This file is provided solely for the use of CMPT students. Students are permitted
# to use this file for their own studies, and to make copies for their own personal use.
# This file should not be posted on any public server, or made available to any party not
# enrolled in CMPT
# This implementation is provided on an asis basis, suitable for educational purposes only.
#
import random as rand
import math as math
class Stateobject:
def initself precedingaction:
Initialize the State object.
# each state MUST store the action that created it
# if you want to be able to actually see your solutions
self.action precedingaction
pass
def strself:
A string representation of the State
return
def eqself other:
Allows states to be compared"""
return False
class Problemobject:
The Problem class defines aspects of the problem.
One of the important definitions is the transition model for states.
To interact with search classes, the transition model is defined by:
isgoals: returns true if the state is a goal state.
actionss: returns a list of all legal actions in state s
resultsa: returns a new state, the result of doing action a in state s
def initself goal:
The problem is defined by a target value. We want to measure that many
liters of water.
:param goal: the number of liters of water we want to measure
self.goal goal
# INSERT WHATEVER ELSE YOU NEED HERE
def createinitialstateself:
returns an initial state
return None
def isgoalself astate:State:
Determine whether astate is a goal state"""
return False
def actionsself astate:State:
Returns all the actions that are legal in the given state.
return
def resultself astate:State, anaction:
Implements the transition function.
Given a state and an action, return the resulting state.
return None
# end of fileQuestion points:
Purpose: To apply uninformed search algorithms
AIMA Chapters:
For this question. your task is to impelement the Water Jugs problem as a search problem.
You have been provided with several Python files. These include a generalized, objectoriented imple
mentations of the basic search algorithms below.
Breadthfirst BFS
Depthfirst DFS
Iterativedeepening IDS
Each of the algorithms above can be run using either the Tree search or Graph search variants.
The only file you need to CHANGE is
WaterJugs.py This file implements both the State and Problem
classes that are used by the search algorithms. The provided version of
WaterJugs.py contains only
method headers. You need to implement these methods correctly to capture the specific dynamics of
the Water Jugs problem, using your understanding of search problem encoding and of AIMA Chapter
When you are done, you can test your implementation by running seesolutions py with the following
arguments:
python seesolutions.py ids graph
If everything is working correctly, this should find a solution to the Water Jug problem for the target value
of liter.
However, BEFORE you do this, it is crucial that you ensure your state and problem implementation is cor
rect. Testing via search is a BAD idea, as the search may examine thousands or even millions of nodes.
A simple example of a test file is provided to give you some testing ideas. You don't need to hand in this
testing. but there is virtually no chance that your implementation will be correct if you don't do it
Hint: By far the most common problem in implementing a search problem is "not copying things that
need to be copied" when you create a new state. For example, recall that in Python, you can easily create
a shallow copy of a list like so:
newlist x for in oldlist
Some students are tempted to use builtin Python methods like deepcopy, but this can be extraordinarily
inefficient if called blindly. Making manual copies with list comprehensions and copying only what you
need usually works out much better.
What to Hand In
Your problem implementation in a file called WaterJugs py You do NOT need to rehandin any of
the other provided files.
Be sure to include your name, NSID, student number, and course number at the top of all documents.
Evaluation
marks: Your implementation of the State and Problem classes are correct
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started