def objfunc(x, cap_array, Budg): cap_int = dot(cap_array.T, x) x_int = array(max_choquet(cap_int, Budg)) ch_diffs = array([ Choquet(x_int, v) - Choquet(array(max_choquet(v, Budg)), v) for v in cap_array ]) print dot(x, ch_diffs) return dot(x, ch_diffs)
def fprime(x, cap_array, Budg): cap_int = dot(cap_array.T, x) x_int = array(max_choquet(cap_int, Budg)) ch_diffs = array([ Choquet(x_int, v) - Choquet(array(max_choquet(v, Budg)), v) for v in cap_array ]) print "GRAD" return ch_diffs
def objfunc(x, cap_array, Budg): cap_int = dot(cap_array.T, x) x_int = array(max_choquet(cap_int, fx, dfx, Budg)) ch_xr = array([Choquet(x_int, v, fx) for v in cap_array]) ch_diffs = ch_xr - vmax # print dot(x, ch_diffs) return dot(x, ch_diffs)
def find_centre(fx, dfx, AZ, bZ, AeqZ, beqZ): """ Finds the point f_1 = ... = f_n by optimizing a capacity having v(A)=0,forall A \neq N """ capacity = zeros(2**dim) capacity[-1] = 1 centre = max_choquet(capacity, fx, dfx, AZ, bZ, AeqZ, beqZ) val_centre = Choquet(array(centre), capacity, fx) return array(centre), val_centre
def f_ineqcons_prime(x, cap_array, zr_array, fx): A = array([ append(-1, array([Choquet(zr, cap, fx) - gm for gm, cap in cap_array])) for zr in zr_array ]) B = diag(ones(len(x[1:])), 1)[:-1] D = vstack((A, B)) return D
def f_ineqcons(x, cap_array, zr_array, fx): # A = [x[0] - dot(x[1:],array([Choquet(zr,cap,fx)-gm for gm,cap in cap_array])) for zr in zr_array] # t - sum lambda_i[C(v_i,f(zr)) - max_z C(v_i,f(z))] >= 0 A = [ -x[0] + dot(x[1:], array([gm - Choquet(zr, cap, fx) for gm, cap in cap_array])) for zr in zr_array ] # A.extend(x) A.extend(x[1:]) # lambda >= 0 return array(A)
def max_choquet_glob(capacity, fx, dfx, AZ, bZ, AeqZ, beqZ): """ Calculates the global maximum of Choq. w.r.t any capacity by decomposing into convex parts """ mcap = Mobius(capacity) sols = [[max_choquet(p, fx, dfx, AZ, bZ, AeqZ, beqZ), p] for p in cap_dnf_t(mcap, nonzero(mcap < 0)[0][0], cmatr=zeros((len(fx), len(fx)), dtype=int), result=[])] s1 = [[Choquet(array(p[0]), p[1], fx), p[0]] for p in sols] return max(s1, key=operator.itemgetter(0))[1]
def solve_dual_sip_lp(cap_array, fx, dfx, AZ, bZ, AeqZ, beqZ): """ Another approach to dual problem Input:array of pairs (gm_value, capacity(e.g. vertices of U) or its nec.measure component), functions, gradients, constraints on Z Finds the robust capacity, i.e. the solution of dual problem Capacities assumed to be 2-MONOTONE Output: v^r """ zr_array = [] x = array([0] + [1. / len(cap_array) for i in range(len(cap_array))]) while 1: zr = array( max_choquet( dot(ravel(x[1:]), array([cap for gm, cap in cap_array])), fx, dfx, AZ, bZ, AeqZ, beqZ)) if any([(zr == x).all() for x in zr_array]): print "point already in zr_array" break zr_array.append(zr) A = array([[Choquet(zr, cap, fx) - gm for gm, cap in cap_array] for zr in zr_array]) A = cvx.matrix(r_[c_[ones(len(zr_array)), A], c_[zeros(len(cap_array)), diag(-ones(len(cap_array)))]], tc='d') b = cvx.matrix(zeros(len(zr_array) + len(cap_array)), tc='d') Aeq = cvx.matrix(array([insert(ones(len(cap_array)), 0, 0)]), tc='d') beq = cvx.matrix(array([1]), tc='d') c = cvx.matrix(r_[1, zeros(len(cap_array))]) x = cvx.solvers.lp(-c, A, b, Aeq, beq, 'glpk')['x'] vr = dot(ravel(x[1:]), array([cap for gm, cap in cap_array])) cons_val = dot(ravel(x[1:]), array( [gm for gm, cap in cap_array])) - Choquet( array(max_choquet(vr, fx, dfx, AZ, bZ, AeqZ, beqZ)), vr, fx) print "Constraints", max(cons_val, round(cons_val, 9)), cons_val print "Objective", min(x[0], round(x[0], 9)), x[0] if max(cons_val, round(cons_val, 9)) >= min(x[0], round(x[0], 9)): break return vr, x[0], zr
def solve_dual_sip(cap_array, fx, dfx, AZ, bZ, AeqZ, beqZ): """ Another approach to dual problem Input:array of pairs (gm_value, capacity(e.g. vertices of U) or its nec.measure component), functions, gradients, constraints on Z Finds the robust capacity, i.e. the solution of dual problem Capacities assumed to be 2-MONOTONE Output: v^r """ def f_eqcons(x): Aeq = ones(len(x) - 1) beq = 1 # sum lambda_i = 1 return array([dot(Aeq, x[1:]) - beq]) def fprime_eqcons(x): return append(0, ones(len(x) - 1)) def f_ineqcons(x, cap_array, zr_array, fx): # A = [x[0] - dot(x[1:],array([Choquet(zr,cap,fx)-gm for gm,cap in cap_array])) for zr in zr_array] # t - sum lambda_i[C(v_i,f(zr)) - max_z C(v_i,f(z))] >= 0 A = [ -x[0] + dot(x[1:], array([gm - Choquet(zr, cap, fx) for gm, cap in cap_array])) for zr in zr_array ] # A.extend(x) A.extend(x[1:]) # lambda >= 0 return array(A) def f_ineqcons_prime(x, cap_array, zr_array, fx): A = array([ append(-1, array([Choquet(zr, cap, fx) - gm for gm, cap in cap_array])) for zr in zr_array ]) B = diag(ones(len(x[1:])), 1)[:-1] D = vstack((A, B)) return D def fprime(x): return append(-1, zeros(len(x) - 1)) def objfunc(x): return -x[0] zr_array = [] f_ineqcons_w = lambda x: f_ineqcons(x, cap_array, zr_array, fx) fprime_ineqcons_w = lambda x: f_ineqcons_prime(x, cap_array, zr_array, fx) x = array([0] + [1. / len(cap_array) for i in range(len(cap_array))]) # x = array([0]+[rnd.random() for i in range(len(cap_array))]) print x while 1: zr_array.append( array( max_choquet(dot(x[1:], array([cap for gm, cap in cap_array])), fx, dfx, AZ, bZ, AeqZ, beqZ))) x = fmin_slsqp(objfunc, x, fprime=fprime, f_eqcons=f_eqcons, f_ieqcons=f_ineqcons_w, fprime_eqcons=fprime_eqcons, fprime_ieqcons=fprime_ineqcons_w, iprint=2, full_output=1, acc=1e-11, iter=900)[0] print x vr = dot(x[1:], array([cap for gm, cap in cap_array])) print vr cons_val = dot(x[1:], array([gm for gm, cap in cap_array])) - Choquet( array(max_choquet(vr, fx, dfx, AZ, bZ, AeqZ, beqZ)), vr, fx) print "Constraints", max(cons_val, round(cons_val, 9)) print "Objective", min(x[0], round(x[0], 9)) if max(cons_val, round(cons_val, 9)) >= min(x[0], round(x[0], 9)): break return vr, min(cons_val, round(cons_val, 9)), x[1:]
def fprime(x, cap_array, Budg): cap_int = dot(cap_array.T, x) x_int = array(max_choquet(cap_int, fx, dfx, Budg)) ch_xr = array([Choquet(x_int, v, fx) for v in cap_array]) ch_diffs = ch_xr - vmax return ch_diffs
def f_ineqcons(x, Umm): A = [x[0] - (gm - Choquet(x[1:], cap, fx)) for gm, cap in Umm] A.extend(bZ - dot(AZ, x[1:])) # A.extend(x) # A.extend(x[1:]) return array(A)
def f_ineqcons(x, Umm): A = [x[0] - (Choquet(array(p), v) - Choquet(x[1:], v)) for p, v in Umm] A.extend(x) # A.extend(x[1:]) return array(A)
def f_ineqcons(x, capacity): return append( Choquet(x[1:], capacity, fx) - x[0], bZ - dot(AZ, x[1:])) # C(v,x[1:])>=t, x[1:]>=0