eomahony / Numberjack

Python Combinatorial Optimisation Platform
http://numberjack.ucc.ie
GNU Lesser General Public License v2.1
157 stars 36 forks source link

KeyError ('f') when running the examples from the website #25

Closed Zulko closed 8 years ago

Zulko commented 8 years ago

Hi there,

I am using the newest pip version of NumberJack (1.1.4). When I run examples from the website, for instance this one:

from Numberjack import *

def model_costas_array(N):
    sequence = VarArray(N,1,N)
    model = Model(
        AllDiff(sequence),
        [AllDiff([sequence[j] - sequence[j+i+1] for j in range(N-i-1)]) for i in range(N-2)]
        )
    return(sequence,model)

def solve_costas_array(param):
    (sequence,model) = model_costas_array(param['N'])
    solver = model.load(param['solver'])
    if solver.solve():
        print_costas_triangle(sequence)
    print 'Nodes:', solver.getNodes(), ' Time:', solver.getTime()

def print_costas_triangle(seq):
    N = len(seq)
    print ''.join([str(int(var.get_value())).rjust(3) for var in seq])
    for i in range(N-1):
        print ''.join([str(int(seq[j].get_value())-int(seq[j+i+1].get_value())).rjust(3)
                       for j in range(N-i-1)])

solve_costas_array(input({'solver':'Mistral','N':10}))

I get the following error

Warning: wrong parameter name, ignoring arguments following f

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-08cefaebbb14> in <module>()
     23                        for j in range(N-i-1)])
     24 
---> 25 solve_costas_array(input({'solver':'Mistral','N':10}))

/Library/Python/2.7/site-packages/Numberjack/__init__.pyc in input(default)
   2851                     param_list[option] = 'yes'
   2852                 elif len(params) == 1:
-> 2853                     if type(param_list[option]) == int:
   2854                         #print 'int'
   2855                         param_list[option] = int(params[0])

KeyError: 'f'
9thbit commented 8 years ago

Hi @Zulko, are you running the example from the command line by passing any additonal options like this?

python costas.py -f

Also, you could try downloading the Numberjack zip file, which has lots of other and more up to date examples. Or browse the examples folder in the github repo. https://www.github.com/eomahony/Numberjack/tree/master/examples

Zulko commented 8 years ago

Thanks for the quick answer, I found the issue, I was running the example from an IPython Notebook and I hadn't seen the "input" in the last line, removing the input everything is working fine. Sorry for the noise !

jclchan commented 4 years ago

I encounter the same issue when running from example script from Jupyter notebook for the following example https://github.com/eomahony/Numberjack/blob/master/examples/SendMoreMoney.py

I tried removing the line but without success. param = input(default)

Can you help?