What is the cost?

1 answer below »
Answered Same DayApr 18, 2021

Answer To: What is the cost?

Shivinder answered on Apr 21 2021
148 Votes
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 19 23:22:39 2019
@author: LENOVO
"""
class Decant:

def __init__(self, container_volume, target_state):
self.container_volume = container_volume #Volumne of each con
tainer. Keep the order as it is
self.target_state = target_state #Target state
self.state_memory = {} #States which are already covered will be stored in this dictionary i.e. state memory
self.state_values = [] #This will store all the states traversed

def start(self, init_state):

#Check if init state is a valid state
for val in range(len(init_state)):
if init_state[val] > self.container_volume[val]: #If filled volumne is more than maximum volume
return False

else:
return init_state

def goal(self, node):
if node == self.target_state: #Target state
return True

def calc_decant(self, state):

#It is a recursive function which will calculate all the states till the target state

if (self.goal(state)): #If the goal state reached
self.state_values.append(state)
return True

if( state in self.state_memory): #Check if the given state already exists in state memory
return False
self.state_memory[state] = 1 #If state does not exist in state memory, then add it in memory and assign a value of 1 to it

#First we will empty the first jug
if(state[0]>0):

#empty first into second
if(state[0]+state[1]<=self.container_volume[1]):
if( self.calc_decant((0,state[0]+state[1],state[2])) ):
self.state_values.append(state)
return True
else:
if( self.calc_decant((state[0]-(self.container_volume[1]-state[1]), self.container_volume[1], state[2])) ):
self.state_values.append(state)
return True

#empty first into third
if(state[0]+state[2]<=self.container_volume[2]):
if( self.calc_decant((0,state[0],state[0] + state[2]))):
self.state_values.append(state)
return...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here