1

Say that I want to solve a parametric constrained optimization, method. Is there any method I can use to avoid looping over the parameters?

I would use a code (adapted to my problem) such as (credit):

import numpy as np
from scipy.optimize import minimize
import random
import pandas as pd
pd.options.display.float_format = '{:.5f}'.format

def uncovered(x):
    ## note x[0]--> x ; x[1]--> y
    first_prod = x[0]**2-2*x[0]-4*x[1]+1
    second_prod_first = x[0]**3*(8*a**2*(b-1)*b-4*a*(b-2)-3)
    second_prod_second = x[0]**2*(-8*a**2*(b-1)*b-8*a+5)
    second_prod_third = x[0]*(4*x[1]*(4*a*(b-1)+1)+4*a*b - 1) -4*x[1] - 1
    denom = 8*(x[0]-1)*(x[0]*(2*a*(b-1)+1)-1)**2
    if (x[0]-1==0):
        final_func=0
    else:
        final_func = first_prod*(second_prod_first+second_prod_second+second_prod_third)/denom
    return -final_func

def constraint_strict(x):
    return x[1]-0.5*a*x[0]*(x[0]-1)*(1-b)-0.0001

def constraint_nonstrict(x):
    return 0.25*(1-2*x[0]+x[0]**2) - x[1]

def constraint_1(x):
    return 1-x[0]

def constraint_2(x):
    return x[0]

def run_uncovered_market_maximization(alpha,xi):
    a=alpha
    b=xi
    # initial guesses
    n = 2
    x0 = np.zeros(n)
    x0[0] = 1
    x0[1] = 1.0
    
    success = False
    # show initial objective
    print("We are maximizing uncovered welfare:")
    print('Initial Objective: ' + str(uncovered(x0)))
    
    while success == False:
        # optimize
        bnds = ((0,1),(-np.inf,np.inf))

        con1 = {'type': 'ineq', 'fun': constraint_strict}
        con2 = {'type': 'ineq', 'fun': constraint_nonstrict}
        cons = ([con1,con2])
        solution = minimize(uncovered,x0,method='SLSQP',                            bounds=bnds,constraints=cons,options={'disp': False})
        x = solution.x
        success = solution.success

        if success == False:
            x0[0] = x0[0]-random.uniform(0, 1)
            x0[1] = 5.0-random.uniform(0, 1)
    
    indcons=(x[1]+a*(1-x[0])*x[0]*(1-b)/2)/((((1-x[0])**2)/4)+a*(1-x[0])*(1-b)*x[0]/2)
    
    # show final objective
    print("Success:"+str(solution.success))
    print('Final Objective: ' + str(uncovered(x)))
    print('First Constraint: ' + str(constraint_strict(x)))
    print('Second Constraint: ' + str(constraint_nonstrict(x)))

    # print solution
    print('Solution')
    print('d = ' + str(x[0]))
    print('p = ' + str(x[1]))
    print('market demand:'+ str(1-indcons))

    return (solution.success, str(x[0]),str(x[1]),str(uncovered(x)),indcons)

I am unsure on how to handle a and b given that the maximization should not be over these and they only represent parameters. I want to have a pair (x,y) for any acceptable value of (a,b). How to set the boundaries? and how to insert a and b if not by looping over them?

For now I do:

optlist =[]
for b in np.linspace(start=0, stop=1, num=2000):
    for a in np.linspace(start=0, stop=1, num=2000):
        print('-------------Optimization for a:{} and b:{}-------------------'.format(a,b))   
        
        print('-------------Uncovered market')

        (usuccess, du, pu,ufinal,uindcons) = run_uncovered_market_maximization(a,b)
KArrow'sBest
  • 150
  • 9
  • I am not sure why you would have to loop over anything. You provide boundaries and constraits to `minimize` function and it does whole optimization by itself, doing gradient (or other) methods which are faster than looping through all combinations of a and b. – dankal444 Oct 05 '22 at 21:08
  • So is it enough to code the constraints and boundaries and throw them in there? In Mathematica Wolfram I wasn't able to obtain optimal x and y without providing a and b values first. – KArrow'sBest Oct 06 '22 at 11:21
  • @dankal444 how would you code the strict inequality one? – KArrow'sBest Oct 06 '22 at 11:25
  • 1
    Same as [in this answer](https://stackoverflow.com/a/42304099/4601890). To make it "kind of" strict you just need to add/subtract epsilon (very small number). – dankal444 Oct 06 '22 at 13:28
  • @dankal444 thanks to your comments I could pinpoint my issue with this problem and edited the question – KArrow'sBest Oct 07 '22 at 07:26
  • @dankal444 Let me add in response to the first comment that I don't have to maximize using a and b as decision variables, so they cannot be treated as x and y. Hence, the loop was my option to do it. I think now the question is clearer. – KArrow'sBest Oct 07 '22 at 07:33

0 Answers0