0

I want to find the minimum output of the function, as shown below, which contains 4 variables (bolded & emphasised), also with multiple constraints.

(width * length * t_base * rho) + (n_fin * t_fin * length * h_fin * rho)

Two constraints are function of variables such that the function output is less than the certain value.

Other constraints are that the varialbes should be strictly positive as these values are physical parameters (length etc.)

I was trying to use scipy.optimize.minimize module to solve this problem but I stuck at setting multiple constraints in the code.

def objective(w, l, n_f, h_f):
    return (w * l * t_base * rho_cp) + (n_f * t_fin * l * h_f * rho_cp)


const_T = dict(type='eq', fun=lambda w, l, n_f, h_f: 333 - (310 + (151.73 * l * ((w / (n_f - 1)) * 39.3701) / ((n_f - 1) * (h_f * 39.3701)))) #First constraint function with variables

const_P = dict(type='eq', fun=lambda w, l, n_f, h_f: 3.04 - (0.914 * (461.42 / n_f) / (w / n_f * 39.3701 * h_f * 39.3701 * l * 0.001))) #Second constraint function with variables

x0 = np.array([width, length, n_fin, h_fin])
res = optimize.minimize(objective, constraints=[const_T, const_P], x0=x0)

Note that other variables beside w, l, n_f and h_f are known and treated as constant in the calculation.

But I got error in the code stated as "Expected type 'dict | None', got 'list[dict[str, str | (w: Any, l: Any, n_f: Any, h_f: Any) -> float | Any] | dict[str, str | (w: Any, l: Any, n_f: Any, h_f: Any) -> float | Any]]' instead"

Also, I got error message when I try to run the code just with one constraint: TypeError: cold_plate_optimize..() missing 3 required positional arguments: 'l', 'n_f', and 'h_f'

I am wondering whether scipy.optimize.minimize module is appropriate approach to solve such problem.

If not, it would be really appreciated if anyone can guide me better method on solving this problem.

Thank you very much in advance.

Edit1: Sorry, I have simplified the constraint functions that contain variables. Frankly speaking, the constraint function doesn't have to be the function written above, as I just want to know how to set multiple constraints which are the function of multiple variables.

tkfahs
  • 1
  • 1
  • c'mon. You are asking somebody to wade into that swamp and untangle that mess? If there is a concept you are struggling with, whittle it down to a minimal example of the concept. If that spider web is the problem, break it out into a separate function, do some intermediate calculations and some housekeeping. – AirSquid Oct 11 '22 at 15:59
  • Sorry about the mess, and I have simplified the function in the question. – tkfahs Oct 11 '22 at 16:31

1 Answers1

0

Let's clean it up a bit.

Example:

w = 5
l = 5
n_f = 5
h_f = 5
    
testA = lambda w, l, n_f, h_f : 333 - (310 + (151.73 * l * ( (w / (n_f - 1) ) * 39.3701) / ( (n_f - 1)*(h_f * 39.3701) ) ))
testB = lambda w, l, n_f, h_f: 3.04 - (0.914 * (461.42 / n_f) / (w / n_f * 39.3701 * h_f * 39.3701 * l * 0.001))

const_T = dict(type='eq', fun = testA(w,l,n_f,h_f))

const_P = dict(type='eq', fun = testB(w,l,n_f,h_f)) 

then

Paste that code here and test it ====> https://hashcode.co.kr/code_runners

RealMan
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 17 '22 at 09:30