Question: Use the following python code to solve the next problems: Program for local searches implemented by me Knapsack problem State: ( 1
Use the following python code to solve the next problems:
Program for local searches implemented by me
Knapsack problem
State: kpkpkkk
Values:
Dictionary for the values of each product
import simpleai.search as ss
from simpleai.search.local import expschedule
# Possible scheduler for simulatedannealing, based on the aima example.
class KnapssSearchProblem:
def initself:
# Maximum load of the knapsack
self.maxLoadKnap
# Initialize the initial state in the parent class
superKnap selfinitinitialstateself.generaterandomstate
def knapsackweightself curstate:
weight curstate curstate
curstate curstate curstate
return weight
def actionsself curstate:
Returns the actions available to perform from state"""
actionslist
# Check if the rule can be triggered in the current state
# before adding it to the list of possible actions
if selfknapsackweightcurstate self.maxLoadKnap:
actionslist.appendkp
if selfknapsackweightcurstate self.maxLoadKnap:
actionslist.appendkp
if selfknapsackweightcurstate self.maxLoadKnap:
actionslist.appendk
if selfknapsackweightcurstate self.maxLoadKnap:
actionslist.appendk
if selfknapsackweightcurstate self.maxLoadKnap:
actionslist.appendk
return actionslist
def resultself curstate, action:
Returns the resulting state of applying action to state"""
if action kp:
curstate
elif action kp:
curstate
elif action k:
curstate
elif action k:
curstate
elif action k:
curstate
return curstate
def valueself curstate:
Returns the value of state as it is needed by optimization
problems.
Value is a number integer or floating point
value curstate curstate
curstate curstate curstate
return value
def generaterandomstateself:
Generates a random state for genetic search. It's mainly used for the
seed states in the initialization of genetic search.
import random
statelist
weight
while weight self.maxLoadKnap:
statelist random.randint self.maxLoadKnap
statelist random.randint self.maxLoadKnap
statelist random.randintselfmaxLoadKnap
statelist random.randint self.maxLoadKnap
statelist random.randint self.maxLoadKnap
weight self.knapsackweightstatelist
return statelist
temp
def temperaturetime: # CORRECT: IT SHOULD BE A SCHEDULER
# Need to receive time and return a value
# As required by the simulatedannealing function in the library
# global temp
res temp time
printTemperature: time, res
return res
if namemain:
# Initialize the object
problem Knap
# Solve problem
# output sshillclimbingproblem
# output sshillclimbingstochasticproblem
output sshillclimbingrandomrestartsproblem # the one that runs better
# output sssimulatedannealingproblem temperature
# output sssimulatedannealingproblemexpschedule
# The path is a single element since it is not being saved, it doesn't matter to know it
print
Path to the solution:
for item, state in output.path:
printstate
printfValue problemvaluestate
printfWeight problemknapsackweightstate
Use the simple hillclimbing algorithm to solve the problem.
Compare the performance of this algorithm with the solution with simulated annealing. What algorithm allows you to find a solution to the problem?
Modify the code to now have a "backpack" that supports kilos and wants to store the greatest possible value of the following elements:
kilos, $
kilo, $
kilo, $
kilos, $
kilos, $
kilos, $
What is the best allocation of items for the backpack? Justify your answer with the results of at least two types of algorithms.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
