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: (1k1p,1k2p,2k,4k,12k)
01234
Values: (1,2,2,10,4)
Dictionary for the values of each product
"""
import simpleai.search as ss
from simpleai.search.local import _exp_schedule
# Possible scheduler for simulated_annealing, based on the aima example.
class Knap(ss.SearchProblem):
def _init_(self):
# Maximum load of the knapsack
self._maxLoadKnap =15
# Initialize the initial state in the parent class
super(Knap, self)._init_(initial_state=self.generate_random_state())
def knapsack_weight(self, cur_state):
weight = cur_state[0]*1+ cur_state[1]*1+\
cur_state[2]*2+ cur_state[3]*4+ cur_state[4]*12
return weight
def actions(self, cur_state):
"""Returns the actions available to perform from state"""
actions_list =[]
# Check if the rule can be triggered in the current state
# before adding it to the list of possible actions
if (self.knapsack_weight(cur_state)+1)<= self._maxLoadKnap:
actions_list.append("1k1p")
if (self.knapsack_weight(cur_state)+1)<= self._maxLoadKnap:
actions_list.append("1k2p")
if (self.knapsack_weight(cur_state)+2)<= self._maxLoadKnap:
actions_list.append("2k")
if (self.knapsack_weight(cur_state)+4)<= self._maxLoadKnap:
actions_list.append("4k")
if (self.knapsack_weight(cur_state)+12)<= self._maxLoadKnap:
actions_list.append("12k")
return actions_list
def result(self, cur_state, action):
"""Returns the resulting state of applying action to state"""
if action =="1k1p":
cur_state[0]+=1
elif action =="1k2p":
cur_state[1]+=1
elif action =="2k":
cur_state[2]+=1
elif action =="4k":
cur_state[3]+=1
elif action =="12k":
cur_state[4]+=1
return cur_state
def value(self, cur_state):
'''Returns the value of state as it is needed by optimization
problems.
Value is a number (integer or floating point).'''
value = cur_state[0]*1+ cur_state[1]*2+\
cur_state[2]*2+ cur_state[3]*10+ cur_state[4]*4
return value
def generate_random_state(self):
"""
Generates a random state for genetic search. It's mainly used for the
seed states in the initialization of genetic search.
"""
import random
state_list =[0,0,0,0,0]
weight =100
while weight > self._maxLoadKnap:
state_list[0]= random.randint(0, self._maxLoadKnap)
state_list[1]= random.randint(0, self._maxLoadKnap)
state_list[2]= random.randint(0,(self._maxLoadKnap //2))
state_list[3]= random.randint(0, self._maxLoadKnap //4)
state_list[4]= random.randint(0, self._maxLoadKnap //12)
weight = self.knapsack_weight(state_list)
return state_list
temp =50
def temperature(time): # CORRECT: IT SHOULD BE A SCHEDULER
# Need to receive time and return a value
# As required by the simulated_annealing function in the library
# global temp
res = temp - time
print("Temperature: ", time, res)
return res
if _name_=='_main_':
# Initialize the object
problem = Knap()
# Solve problem
# output = ss.hill_climbing(problem)
# output = ss.hill_climbing_stochastic(problem)
output = ss.hill_climbing_random_restarts(problem,500) # the one that runs better
# output = ss.simulated_annealing(problem, temperature)
# output = ss.simulated_annealing(problem,_exp_schedule)
# 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():
print(state)
print(f"Value ={problem.value(state)}")
print(f"Weight ={problem.knapsack_weight(state)}")
1. Use the simple hill-climbing 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?
2. Modify the code to now have a "backpack" that supports 50 kilos and wants to store the greatest possible value of the following elements:
2 kilos, $5
1 kilo, $2
1 kilo, $3
7 kilos, $13
3 kilos, $10
5 kilos, $15
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

To address these questions Ill guide you through setting up and executing algorithms to solve the given knapsack problem using Python Well be focusing ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!