def delete_dom_act_Pi(game, i): no_a_game = list( game.shape) #will contain number of actions available for each player no_a_game.pop( ) #removes last element of list which simply was n: the number of payoffs in each action profile #as the first number is number of actions of last player etc., we turn the list around no_a_game = no_a_game[::-1] no_a_i = no_a_game.pop( i ) #remove the number of actions of player i from no_a_game and put it in no_a_i if no_a_i == 1: #if a single action remains it cannot be dominated return game var_no = np.prod(no_a_game) #size of the support of mu if var_no == 1: #special case: the support of mu has a single elment return del_dom_a_Pi_point_belief(game, i, no_a_i) temp_game = game f = [0.] * var_no #dummy objective used below lb = [0.] * var_no ub = [1.] * var_no Aeq = [[1.] * var_no] beq = (1., ) j = 0 while j < no_a_i: u_action = payoffi_builder(game, j, i) A = [] b = [] k = 0 while k < no_a_i: u_other_action = payoffi_builder(game, k, i) A.append(u_other_action - u_action) #elementwise difference b.append(0.) k += 1 p = LP( f, A=A, b=b, lb=lb, ub=ub, Aeq=Aeq, beq=beq ) #we use the artificial minimization problem under the constraint that action gives a weakly higher payoff than any other action; if no feasible solution is obtained than action is dominated p.iprint = -1 r = p.minimize('pclp') if r.stopcase != 1: #if no feasible solution was obtained then action is dominated... temp_game = np.delete( temp_game, j, n - i - 1 ) #...and therefore action j is removed; recall that players are "in the wrong order" count = 0 z = -1 while count <= j: z += 1 if undominated[i][z] != -1: count += 1 undominated[i][z] = -1 j += 1 return temp_game
def delete_dom_act_Pi(game,i): no_a_game = list(game.shape) #will contain number of actions available for each player no_a_game.pop()#removes last element of list which simply was n: the number of payoffs in each action profile #as the first number is number of actions of last player etc., we turn the list around no_a_game = no_a_game[::-1] no_a_i = no_a_game.pop(i)#remove the number of actions of player i from no_a_game and put it in no_a_i if no_a_i==1:#if a single action remains it cannot be dominated return game var_no = np.prod(no_a_game)#size of the support of mu if var_no == 1:#special case: the support of mu has a single elment return del_dom_a_Pi_point_belief(game, i,no_a_i) temp_game = game f = [0.]*var_no #dummy objective used below lb = [0.]*var_no ub = [1.]*var_no Aeq = [[1.]*var_no] beq = (1.,) j = 0 while j<no_a_i: u_action = payoffi_builder(game,j,i) A = [] b = [] k = 0 while k<no_a_i: u_other_action = payoffi_builder(game,k,i) A.append(u_other_action - u_action)#elementwise difference b.append(0.) k+=1 p = LP(f, A=A,b=b,lb=lb,ub=ub,Aeq=Aeq,beq=beq)#we use the artificial minimization problem under the constraint that action gives a weakly higher payoff than any other action; if no feasible solution is obtained than action is dominated p.iprint = -1 r = p.minimize('pclp') if r.stopcase!=1:#if no feasible solution was obtained then action is dominated... temp_game = np.delete(temp_game,j,n-i-1)#...and therefore action j is removed; recall that players are "in the wrong order" count = 0 z = -1 while count<=j: z+=1 if undominated[i][z]!=-1 : count+=1 undominated[i][z] = -1 j+=1 return temp_game
def __solver__(self, p): n = p.n x0 = copy(p.x0) xPrev = x0.copy() xf = x0.copy() xk = x0.copy() p.xk = x0.copy() f0 = p.f(x0) fk = f0 ff = f0 p.fk = fk df0 = p.df(x0) ##################################################################### ## #handling box-bounded problems ## if p.__isNoMoreThanBoxBounded__(): ## for k in range(int(p.maxIter)): ## ## #end of handling box-bounded problems isBB = p.__isNoMoreThanBoxBounded__() ## isBB = 0 H = diag(ones(p.n)) if not p.userProvided.c: p.c = lambda x: array([]) p.dc = lambda x: array([]).reshape(0, p.n) if not p.userProvided.h: p.h = lambda x: array([]) p.dh = lambda x: array([]).reshape(0, p.n) p.use_subproblem = 'QP' #p.use_subproblem = 'LLSP' for k in range(p.maxIter + 4): if isBB: f0 = p.f(xk) df = p.df(xk) direction = -df f1 = p.f(xk + direction) ind_l = direction <= p.lb - xk direction[ind_l] = (p.lb - xk)[ind_l] ind_u = direction >= p.ub - xk direction[ind_u] = (p.ub - xk)[ind_u] ff = p.f(xk + direction) ## print 'f0', f0, 'f1', f1, 'ff', ff else: mr = p.getMaxResidual(xk) if mr > p.contol: mr_grad = p.getMaxConstrGradient(xk) lb = p.lb - xk #- p.contol/2 ub = p.ub - xk #+ p.contol/2 c, dc, h, dh, df = p.c(xk), p.dc(xk), p.h(xk), p.dh(xk), p.df( xk) A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq)) b = concatenate((-c, p.b - p.matmult(p.A, xk))) #+ p.contol/2 beq = concatenate((-h, p.beq - p.matmult(p.Aeq, xk))) if b.size != 0: isFinite = isfinite(b) ind = where(isFinite)[0] A, b = A[ind], b[ind] if beq.size != 0: isFinite = isfinite(beq) ind = where(isFinite)[0] Aeq, beq = Aeq[ind], beq[ind] if p.use_subproblem == 'LP': #linear linprob = LP(df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub) linprob.iprint = -1 r2 = linprob.solve( 'cvxopt_glpk') # TODO: replace lpSolve by autoselect if r2.istop <= 0: p.istop = -12 p.msg = "failed to solve LP subproblem" return elif p.use_subproblem == 'QP': #quadratic qp = QP(H=H, f=df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub) qp.iprint = -1 r2 = qp.solve( 'cvxopt_qp') # TODO: replace solver by autoselect #r2 = qp.solve('qld') # TODO: replace solver by autoselect if r2.istop <= 0: for i in range(4): if p.debug: p.warn("iter " + str(k) + ": attempt Num " + str(i) + " to solve QP subproblem has failed") #qp.f += 2*N*sum(qp.A,0) A2 = vstack((A, Aeq, -Aeq)) b2 = concatenate( (b, beq, -beq)) + pow(10, i) * p.contol qp = QP(H=H, f=df, A=A2, b=b2, iprint=-5) qp.lb = lb - pow(10, i) * p.contol qp.ub = ub + pow(10, i) * p.contol # I guess lb and ub don't matter here try: r2 = qp.solve( 'cvxopt_qp' ) # TODO: replace solver by autoselect except: r2.istop = -11 if r2.istop > 0: break if r2.istop <= 0: p.istop = -11 p.msg = "failed to solve QP subproblem" return elif p.use_subproblem == 'LLSP': direction_c = getConstrDirection(p, xk, regularization=1e-7) else: p.err('incorrect or unknown subproblem') if isBB: X0 = xk.copy() N = 0 result, newX = chLineSearch(p, X0, direction, N, isBB) elif p.use_subproblem != 'LLSP': duals = r2.duals N = 1.05 * abs(duals).sum() direction = r2.xf X0 = xk.copy() result, newX = chLineSearch(p, X0, direction, N, isBB) else: # case LLSP direction_f = -df p2 = NSP(LLSsubprobF, [0.8, 0.8], ftol=0, gtol=0, xtol=1e-5, iprint=-1) p2.args.f = (xk, direction_f, direction_c, p, 1e20) r_subprob = p2.solve('ralg') alpha = r_subprob.xf newX = xk + alpha[0] * direction_f + alpha[1] * direction_c # dw = (direction_f * direction_c).sum() # cos_phi = dw/p.norm(direction_f)/p.norm(direction_c) # res_0, res_1 = p.getMaxResidual(xk), p.getMaxResidual(xk+1e-1*direction_c) # print cos_phi, res_0-res_1 # res_0 = p.getMaxResidual(xk) # optimConstrPoint = getDirectionOptimPoint(p, p.getMaxResidual, xk, direction_c) # res_1 = p.getMaxResidual(optimConstrPoint) # # maxConstrLimit = p.contol #xk = getDirectionOptimPoint(p, p.f, optimConstrPoint, -optimConstrPoint+xk+direction_f, maxConstrLimit = maxConstrLimit) #print 'res_0', res_0, 'res_1', res_1, 'res_2', p.getMaxResidual(xk) #xk = getDirectionOptimPoint(p, p.f, xk, direction_f, maxConstrLimit) #newX = xk.copy() result = 0 # x_0 = X0.copy() # N = j = 0 # while p.getMaxResidual(x_0) > Residual0 + 0.1*p.contol: # j += 1 # x_0 = xk + 0.75**j * (X0-xk) # X0 = x_0 # result, newX = 0, X0 # print 'newIterResidual = ', p.getMaxResidual(x_0) if result != 0: p.istop = result p.xf = newX return xk = newX.copy() fk = p.f(xk) p.xk, p.fk = copy(xk), copy(fk) #p._df = p.df(xk) #################### p.iterfcn() if p.istop: p.xf = xk p.ff = fk #p._df = g FIXME: implement me return
def __solver__(self, p): n = p.n x0 = copy(p.x0) xPrev = x0.copy() xf = x0.copy() xk = x0.copy() p.xk = x0.copy() f0 = p.f(x0) fk = f0 ff = f0 p.fk = fk df0 = p.df(x0) ##################################################################### ## #handling box-bounded problems ## if p.__isNoMoreThanBoxBounded__(): ## for k in range(int(p.maxIter)): ## ## #end of handling box-bounded problems isBB = p.__isNoMoreThanBoxBounded__() ## isBB = 0 H = diag(ones(p.n)) if not p.userProvided.c: p.c = lambda x : array([]) p.dc = lambda x : array([]).reshape(0, p.n) if not p.userProvided.h: p.h = lambda x : array([]) p.dh = lambda x : array([]).reshape(0, p.n) p.use_subproblem = 'QP' #p.use_subproblem = 'LLSP' for k in range(p.maxIter+4): if isBB: f0 = p.f(xk) df = p.df(xk) direction = -df f1 = p.f(xk+direction) ind_l = direction<=p.lb-xk direction[ind_l] = (p.lb-xk)[ind_l] ind_u = direction>=p.ub-xk direction[ind_u] = (p.ub-xk)[ind_u] ff = p.f(xk + direction) ## print 'f0', f0, 'f1', f1, 'ff', ff else: mr = p.getMaxResidual(xk) if mr > p.contol: mr_grad = p.getMaxConstrGradient(xk) lb = p.lb - xk #- p.contol/2 ub = p.ub - xk #+ p.contol/2 c, dc, h, dh, df = p.c(xk), p.dc(xk), p.h(xk), p.dh(xk), p.df(xk) A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq)) b = concatenate((-c, p.b-p.matmult(p.A,xk))) #+ p.contol/2 beq = concatenate((-h, p.beq-p.matmult(p.Aeq,xk))) if b.size != 0: isFinite = isfinite(b) ind = where(isFinite)[0] A, b = A[ind], b[ind] if beq.size != 0: isFinite = isfinite(beq) ind = where(isFinite)[0] Aeq, beq = Aeq[ind], beq[ind] if p.use_subproblem == 'LP': #linear linprob = LP(df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub) linprob.iprint = -1 r2 = linprob.solve('cvxopt_glpk') # TODO: replace lpSolve by autoselect if r2.istop <= 0: p.istop = -12 p.msg = "failed to solve LP subproblem" return elif p.use_subproblem == 'QP': #quadratic qp = QP(H=H,f=df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub = ub) qp.iprint = -1 r2 = qp.solve('cvxopt_qp') # TODO: replace solver by autoselect #r2 = qp.solve('qld') # TODO: replace solver by autoselect if r2.istop <= 0: for i in range(4): if p.debug: p.warn("iter " + str(k) + ": attempt Num " + str(i) + " to solve QP subproblem has failed") #qp.f += 2*N*sum(qp.A,0) A2 = vstack((A, Aeq, -Aeq)) b2 = concatenate((b, beq, -beq)) + pow(10,i)*p.contol qp = QP(H=H,f=df, A=A2, b=b2, iprint = -5) qp.lb = lb - pow(10,i)*p.contol qp.ub = ub + pow(10,i)*p.contol # I guess lb and ub don't matter here try: r2 = qp.solve('cvxopt_qp') # TODO: replace solver by autoselect except: r2.istop = -11 if r2.istop > 0: break if r2.istop <= 0: p.istop = -11 p.msg = "failed to solve QP subproblem" return elif p.use_subproblem == 'LLSP': direction_c = getConstrDirection(p, xk, regularization = 1e-7) else: p.err('incorrect or unknown subproblem') if isBB: X0 = xk.copy() N = 0 result, newX = chLineSearch(p, X0, direction, N, isBB) elif p.use_subproblem != 'LLSP': duals = r2.duals N = 1.05*abs(duals).sum() direction = r2.xf X0 = xk.copy() result, newX = chLineSearch(p, X0, direction, N, isBB) else: # case LLSP direction_f = -df p2 = NSP(LLSsubprobF, [0.8, 0.8], ftol=0, gtol=0, xtol = 1e-5, iprint = -1) p2.args.f = (xk, direction_f, direction_c, p, 1e20) r_subprob = p2.solve('ralg') alpha = r_subprob.xf newX = xk + alpha[0]*direction_f + alpha[1]*direction_c # dw = (direction_f * direction_c).sum() # cos_phi = dw/p.norm(direction_f)/p.norm(direction_c) # res_0, res_1 = p.getMaxResidual(xk), p.getMaxResidual(xk+1e-1*direction_c) # print cos_phi, res_0-res_1 # res_0 = p.getMaxResidual(xk) # optimConstrPoint = getDirectionOptimPoint(p, p.getMaxResidual, xk, direction_c) # res_1 = p.getMaxResidual(optimConstrPoint) # # maxConstrLimit = p.contol #xk = getDirectionOptimPoint(p, p.f, optimConstrPoint, -optimConstrPoint+xk+direction_f, maxConstrLimit = maxConstrLimit) #print 'res_0', res_0, 'res_1', res_1, 'res_2', p.getMaxResidual(xk) #xk = getDirectionOptimPoint(p, p.f, xk, direction_f, maxConstrLimit) #newX = xk.copy() result = 0 # x_0 = X0.copy() # N = j = 0 # while p.getMaxResidual(x_0) > Residual0 + 0.1*p.contol: # j += 1 # x_0 = xk + 0.75**j * (X0-xk) # X0 = x_0 # result, newX = 0, X0 # print 'newIterResidual = ', p.getMaxResidual(x_0) if result != 0: p.istop = result p.xf = newX return xk = newX.copy() fk = p.f(xk) p.xk, p.fk = copy(xk), copy(fk) #p._df = p.df(xk) #################### p.iterfcn() if p.istop: p.xf = xk p.ff = fk #p._df = g FIXME: implement me return
Aeq = [[1.]*len(Ulist)] beq = (1.,) A = [] b = [] player = 0 while player<len(no_action): action = 0 while action < no_action[player]: for k in range(0,no_action[player]): A.append(multiply(udiff(player,action,k),aik_indicator(player,action))) b.append(0.) action = action + 1 player = player +1 #print A,b p = LP(neg_welfare, A=A,b=b,lb=lb,ub=ub,Aeq=Aeq,beq=beq)#we use the artificial minimization problem under the constraint that action gives a weakly higher payoff than any other action; if no feasible solution is obtained than action is dominated p.iprint = -1 try: r = p.minimize('pclp') pminw = LP(welfare, A=A,b=b,lb=lb,ub=ub,Aeq=Aeq,beq=beq) pminw.iprint = -1 rminw = pminw.minimize('pclp') except: print "Solver returns error. Probably, each player has a unique rationalizable action (check with rationalizability solver)." quit() ###formatting the result back into the same format as the game input # first: rounding outr = [] for item in r.xf: outr.append(round(item,3))
def solve(self): p=LP(self.cost_function,Aeq=self.Aeq,beq=self.beq,lb=self.lb) p.iprint = -1 r=p.solve('pclp') return [r.xf,r.ff]