python - Constrained maximization of function and not-successful attempts in Python3 -


firstly, present problem. latex description of problem

my attempt

i tried solve problem scipy.optimize package. had pass initial vector (initial guess). , value, given algorithm dependent on initial guess. tried take random initial guesses , tried find maximal values among more , more initial guesses

import numpy np scipy.optimize import minimize  def j(data):     l = len(data)     if (l%2==0 | l<2):         raise exception('wrong length or arguments!')      phi = data[0:l//2]     p = data[l//2:l-1]     theta = data[l-1]      phi = np.array(phi)         p = np.array(p)      w = (1 + np.cos(phi)*np.cos(theta))*p     lam = (1+np.cos(phi-theta))/(2*(1+np.cos(phi)*np.cos(theta)))      return -(1 - sum(w * ((-lam*np.log2(lam)) - (1-lam)*np.log2(1-lam))))  def probability_constraint(x):     return sum(x[len(x)//2:len(x)-1])-1  def lambda_w_constraint(data):     l = len(data)     phi = data[0:l//2]     p = data[l//2:-1]     theta = data[l-1]      phi = np.array(phi)         p = np.array(p)      w = (1 + np.cos(phi)*np.cos(theta))*p     lam = (1+np.cos(phi-theta))/(2*(1+np.cos(phi)*np.cos(theta)))         return sum(w*lam)-1/2  def lambda_greater_than_zero(data):     l = len(data)     phi = data[0:l//2]     p = data[l//2:-1]     theta = data[l-1]      lam = (1+np.cos(phi-theta))/(2*(1+np.cos(phi)*np.cos(theta)))         return lam  def probability_positive(x):     return x  cons = ({'type': 'ineq', 'fun' : lambda x: lambda_greater_than_zero(x)},         {'type': 'eq', 'fun' : lambda x: probability_constraint(x)},         {'type': 'eq', 'fun' : lambda x: lambda_w_constraint(x)},         {'type':'ineq',  'fun' : lambda x: probability_positive(x)}  )  def maximize(numiter):     maxj = -1000     maxx = []      in range(0, numiter):         x0 = np.random.rand(5)*10         res = minimize(j, x0, constraints=cons)         r = j(res.x)         if (r>maxj):             maxj = r             maxx = res.x      return maxj, maxx  maximize(10)  maximize(100) 

i know, method naive , incorrect.

so question is: know how solve problem? can in python3, other languages (matlab?) me.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -