aimacode / aima-python

Python implementation of algorithms from Russell And Norvig's "Artificial Intelligence - A Modern Approach"
MIT License
8.01k stars 3.79k forks source link

AttributeError in search.ipynb #1082

Open AndreasSaelinger opened 5 years ago

AndreasSaelinger commented 5 years ago

In short: AttributeError: 'PeakFindingProblem' object has no attribute 'two_opt'

Calling:

%%timeit
solution = problem.value(hill_climbing(problem))

results in this:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-75-71d650c5d254> in <module>
----> 1 get_ipython().run_cell_magic('timeit', '', 'solution = problem.value(hill_climbing(problem))\n')

/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2350             with self.builtin_trap:
   2351                 args = (magic_arg_s, cell)
-> 2352                 result = fn(*args, **kwargs)
   2353             return result
   2354 

</anaconda3/lib/python3.7/site-packages/decorator.py:decorator-gen-60> in timeit(self, line, cell, local_ns)

/anaconda3/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

/anaconda3/lib/python3.7/site-packages/IPython/core/magics/execution.py in timeit(self, line, cell, local_ns)
   1145             for index in range(0, 10):
   1146                 number = 10 ** index
-> 1147                 time_number = timer.timeit(number)
   1148                 if time_number >= 0.2:
   1149                     break

/anaconda3/lib/python3.7/site-packages/IPython/core/magics/execution.py in timeit(self, number)
    159         gc.disable()
    160         try:
--> 161             timing = self.inner(it, self.timer)
    162         finally:
    163             if gcold:

<magic-timeit> in inner(_it, _timer)

<ipython-input-59-795c2a3e788f> in hill_climbing(problem)
     21     current = Node(problem.initial)
     22     while iterations:
---> 23         neighbors = find_neighbors(current.state)
     24         if not neighbors:
     25             break

<ipython-input-59-795c2a3e788f> in find_neighbors(state, number_of_neighbors)
     10 
     11         for i in range(number_of_neighbors):
---> 12             new_state = problem.two_opt(state)
     13             neighbors.append(Node(new_state))
     14             state = new_state

AttributeError: 'PeakFindingProblem' object has no attribute 'two_opt'
stessaris commented 5 years ago

I confirm the bug, two_opt is defined only for class TSP_problem in search.ipynb; while PeakFindingProblem is defined in search.py. I guess that the hill_climbing function should use only Problem class API.

The problem is that hill_climbing function is redefined in the notebook, shadowing the original one in search.py.