Example #1
0
# start point
x0 = [8,15, 80]

#optional: gradient
df = (lambda x: [3*x[0]**2, 3*x[1]**2,  0], lambda x: [1, -0.5,  0], lambda x:[1, 0, -sin(x[2])])

#w/o gradient:
#p = NLSP(f, x0)
p = SNLE(f, x0, df = df)

#optional: user-supplied gradient check:
#p.checkdf()

#optional: graphical output, requires matplotlib installed
p.plot = 1

p.maxFunEvals = 1e5
p.iprint = 10

#r = p.solve('scipy_fsolve')
#r = p.solve('nssolve')
#or using converter nlsp2nlp, try to minimize sum(f_i(x)^2):
r = p.solve('nlp:ralg', plot=1)

print('solution: %s' % r.xf)
print('max residual: %e' % r.ff)
###############################
#should print:
#solution: [  1.           2.          55.50147021] (3rd coord may differ due to cos is periodic)
#max residual: 2.72366951215e-09
for i in range(n):
    no_a_i = no_a[i]
    Aeq.append([0.] * actagg[i] + [1.] * no_a_i + [0.] *
               (total_no_a - actagg[i] - no_a_i))
    x0 = x0 + [1.0 / no_a_i] * no_a_i
    actagg.append(actagg[i] + no_a_i)


#multiplies the vectors strat and Delta(strat); this product has to be 0 in equilibrium
def product(x):
    strat = []
    for i in range(n):
        strat.append(x[actagg[i]:actagg[i + 1]])
    return np.dot(x, Delta(strat))


p = SNLE(product, x0, lb=lb, ub=ub, Aeq=Aeq, beq=beq)
p.iprint = -1
r = p.solve('nssolve')

if r.stopcase == 1:
    print 'there is an equilibrium in which '
    for i in range(n):
        out = [round(item, 3) for item in r.xf[actagg[i]:actagg[i + 1]]]
        print 'player', i, 'uses the mixed strategy', out
else:
    print 'Error: solver cannot find an equilibrium'

print("--- %s seconds ---" % (time.time() - start_time))
Example #3
0
x0 = [8, 15, 80]

#optional: gradient
df = (lambda x: [3 * x[0]**2, 3 * x[1]**2, 0], lambda x: [1, -0.5, 0],
      lambda x: [1, 0, -sin(x[2])])

#w/o gradient:
#p = NLSP(f, x0)
p = SNLE(f, x0, df=df)

#optional: user-supplied gradient check:
#p.checkdf()

#optional: graphical output, requires matplotlib installed
p.plot = 1

p.maxFunEvals = 1e5
p.iprint = 10

#r = p.solve('scipy_fsolve')
#r = p.solve('nssolve')
#or using converter nlsp2nlp, try to minimize sum(f_i(x)^2):
r = p.solve('nlp:ralg', plot=1)

print('solution: %s' % r.xf)
print('max residual: %e' % r.ff)
###############################
#should print:
#solution: [  1.           2.          55.50147021] (3rd coord may differ due to cos is periodic)
#max residual: 2.72366951215e-09
beq = [1.]*n#dito
x0 = []
actagg = [0]

for i in range(n):
    no_a_i = no_a[i]
    Aeq.append([0.]*actagg[i] + [1.]*no_a_i + [0.]*(total_no_a-actagg[i]-no_a_i))
    x0 = x0 + [1.0/no_a_i]*no_a_i
    actagg.append(actagg[i]+no_a_i)

#multiplies the vectors strat and Delta(strat); this product has to be 0 in equilibrium
def product(x):
    strat = []
    for i in range(n):
        strat.append(x[actagg[i]:actagg[i+1]])
    return np.dot(x,Delta(strat))

p = SNLE(product,x0,lb=lb,ub=ub,Aeq=Aeq,beq=beq)
p.iprint = -1
r = p.solve('nssolve')

if r.stopcase==1:
    print 'there is an equilibrium in which '
    for i in range(n):
        out = [round(item,3) for item in r.xf[actagg[i]:actagg[i+1]]]
        print 'player',i,'uses the mixed strategy',out
else:
    print 'Error: solver cannot find an equilibrium'

print("--- %s seconds ---" % (time.time() - start_time))