1

I need to minimize a two variables function and I have a constraint to respect.

I wrote the following code

def deflection_constraint(inputs):
    #return value must come back as 0 to be accepted
    #if return value is anything other than 0 it's rejected
    #as not a valid answer.
    
    inputs = [a,b]
    mass_propeller_motor =0.0075
    weight_propeller_motor = mass_propeller_motor * 9.81
    L = 0.035
    density_material = 1200 #material dependent
    density_air = 1.2
    mass_propeller_motor_battery = 0.150
    C_d = 0.7
    v_v = 3
    v_l = 7
    alpha = math.pi/6
    beta = math.pi/3
    I = (a*(b**3))/12
    M = weight_propeller_motor * L
    sigma_0 = (M*b)/(2*I)

    V_arm = a*b*L
    V_box = 1.045 * 10**(-5)
    V_frame = 4*V_arm + V_box
    M_frame = density_material*V_frame
    A_r = 0.0184
    F_w = (M_frame + mass_propeller_motor_battery)*9.81
    F_d = 0.5*density_air*A_r*C_d*v_v**2
    F_v = (F_w+F_d)/4
    M_v = F_v*L
    sigma_v = (M_v*b)/(2*I)

    A_rl = A_r*math.cos(beta)
    F_dl = 0.5*density_air*A_rl*C_d*v_l**2
    F_parallel = F_dl * math.sin(alpha)+F_w*math.cos(alpha)
    F_perpendicular = F_dl * math.cos(alpha)+F_w*math.sin(alpha) 
    F_l = math.sqrt(F_parallel**2+F_perpendicular**2)*0.25
    M_l = F_l*L
    sigma_l = (M_l*b)/(2*I)
    
    t1 = 13.33/3600
    t2 = 7186.67/3600
    t3 = 2
    t4 = 6
    t5 = 21613.33/3600
    t6 = 28786.67/3600
    t7 = 8
    t8 = 12
    t9 = 43213.33/3600
    t10 = 50386.67/3600
    t11 = 14
    t12 = 18
    
    f_t10minus = (M_v * D(t10) + (M_l-M_v)*D(t10-t1)-(M_l-M_v)*D(t10-t2)-M_v*D(t10-t3)+M_v*D(t10-t4)+(M_l-M_v)*D(t10-t5)-(M_l-M_v)*D(t10-t6)-M_v*D(t10-t7)+M_v*D(t10-t8)+M_v*D(t10-t9))*L**2/(2*I)

    total = 0.0031 - f_t10minus
    return total

This is the constraint to be respected while the function is the following:

def g(parameters):
    a,b = parameters
    return a*b

So basically I have to minimize a cross sectional area but I have the constraint about the maximum deflection of a beam. Then I tried to minimize the function by using optimize.minimize

first_guess =np.array([0.01,0.005]) 

my_constraints = ({'type': 'ineq', "fun": deflection_constraint})
result = optimize.minimize(g, 
                      first_guess, 
                      method='SLSQP', 
                      #args=(a, b),
                      bounds=((0.005, 0.1), (0.001, 0.005)),
                      options={'disp': True},
                      tol = 10**(-5),
                      constraints=my_constraints)

result.x

but what I obtained is that the final result does not satisfy the constraint and the values of the variables are the lower bounds I put. How can I make it work properly?

4
  • What does result show when you print it out? Commented Sep 8, 2024 at 15:23
  • inputs = [a,b] This line doesn't look correct. This is setting inputs to a fixed value, when inputs is the value you're trying to constrain. Can you check if this ought to be a, b = inputs ? Commented Sep 9, 2024 at 14:20
  • I tried to change to a,b = inputs, but now the algorithm is not optimizing properly. However, now seems that the variables are not fixed anymore. Commented Sep 9, 2024 at 17:04
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Sep 10, 2024 at 9:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.