Data Structures
Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually won’t usually need your flowcharts; they’ll be obvious.
- Fred Brooks
Traits
Strings representing particular parts of the state. Wrapped in the
class SolverTrait
.
name
: String
Operations
Holds the name of the action, preconditions required to apply it, and sets of traits that are added or removed when the action is performed.
Wrapped in SolverOp
class.
name
: Stringprecond
: Set ofSolverTrait
add
: Set ofSolverTrait
remove
: Set ofSolverTrait
SolverData
All of the data given in a problem. Used to drive the solver.
title
: Stringtraits
: Set ofSolverTrait
.initial_state
: Set ofSolverTrait
.goal_state
: Set ofSolverTrait
.ops
: List ofSolverOp
Also available:
trait_set
: list of string -> list of SolverTrait. Error if it encounters a string that is not listed in the traits.operation_lookup
: a dictionary with key being a the name of an operation and value being the corresponding SolverOp. This is not a function, so you use it with brackets. Example:lookup["put on shirt"]
.dump()
: Return a string representation in TOML format.
Check for Understanding
-
Create your own file
shirtdemo.toml
. It should include traits for shirt colors red, orange, yellow, green, blue, as well as operations for putting on each shirt. Add one extra trait called “seaswim” which you can get by wearing a blue shirt and using the “get wet” operation. Your initial state is [“pants on”]. Your goal state is [“pants on”,“blue shirt”, “seaswim”]. -
Read in the file and make a
SolverDemo
object calledshirtdemo
. -
Begin with
y = SolverTrait('yellow')
and do a linear search the set of traits ofshirtdemo
to findy
. Print how many comparisons you make before you findy
. -
Find the operation that adds a blue shirt.
-
Apply the operation to your initial state.
-
Find the “get wet” operation and apply it to your current state.
-
Print out the name of every trait in the current state.