0

My goal is to find the minimum of some function "V" on a condition curve "R=0" V and R are both functions of variables X, Y, D, L. I need as outputs all of {X,Y,D,L,V,R} so i can make a design around these parameters (which correspond to physical measurements and values). V and R are too complex to find the minimum analytically so I am trying to solve it numerically. I am using python and would prefer not to change language if I don't need to.

I am trying to loop through all combinations of X, Y, D, & L between some boundaries with small gradations of each (dX, dY, dD, dL) then run each iteration of that through R and V to find the smallest value of V within an acceptable range around R (see below). I am a mechanichal engineering student so apologies if this is a trivial question or poorly worded.

What i currently have is of the simplified form:

def compute_R (X,Y,D,L):
    ...
    return R = compute_R (X,Y,D,L)

def compute_V (X,Y,D,L):
    ...
    return V = compute_V (X,Y,D,L)

def compute_N (X,Y):
    ...
    return N = compute_N (X,Y)

while N > 1: #X loop#
    X += dX
    N = compute_N (X,Y)
    while N > 1: #Y loop#
        Y += dY
        N = compute_N (X,Y)
        while D/2<X and D/2<y: #D loop#
             D += dD
             while L<0.02: #L loop#
                 R = compute_R (X,Y,D,L) 
                 if a<R and b>R:
                     
                     if V < Smallest_V_Yet
                         Smallest_V_Yet=V
                         Best_Solution_Yet= (X,Y,L,D,R,V)

where a and b are values acceptably close to 0; X,Y,L have domains of about [0,0.02], D has a domain of about [0.001,0.02]; dX, dY, dD, dL all have size about 0.0001; all variables are floats, except for Best_Solution_Yet which is a tuple; all of the "..." represent some mathematical relationship, involving intermediate functions and other variables which are not optimization parameters.

The idea is to loop through this system for all combinations of X,Y,D,L in the domains above overwriting Best_Solution_Yet repeatedly until it has iterated through all valid inputs at which point Best_Solution_Yet would be acceptably close to the actual best solution (actual input set which minimizes V and lies exactly on R=0)

This solution, however only only loops through a small number of the solutions that I want to test (it goes to the last while loop then once it gets to L=0.02 it goes to the 2nd to last and so on but i want it to go to the L loop, then go down to the D loop, change D by dD, then go back to L and loop through all valid values of L and propagating in that way).

I have also tried using for loops but I can't get them to accept the conditions that i want them to.

I hope I have been sufficiently clear about my issue. Some final notes are that:

  • R, N, and V are themselves composed of other functions which perform functions such as solving the convergence for recursively defined inputs to R (R=1/a*b+c where a=f(b,X,...) and b=f(a,X,...) such that a and b must be simultaneously solved by taking a guess for a=a1, finding b(a1)=b1, a(b1)=a2, b2=b(a2) and so on until abs(1-bn/bn-1)<c and abs(1-an/an-1)<c for some convergence criteria c<<1) and finding intermediate values that are inputs to multiple other inputs to R, N, and/or V.
  • I would like everything to be parametric if possible (that is having all of X,Y,D,L,dX,dY,dD,dL,c,R,N,V free to be redefined as necessary)
  • This does not need to be extremely computationally efficient, however, any advice for improving the speed would be appreciated (other than not using python)

Edit: incorporating formatting feedback

6
  • Just to be more clear, you should improve your code sample. You can try (1) using consistent capitalization and naming (e.g. all parameter variables are capitalized, naming the function compute_V instead of V to avoid confusion, etc.), (2) showing variable assignment in the while loops (you can write R = compute_R(X, Y, D, L) instead of just R()), and (3) adding comments to add more clarity (e.g. while D/2<X and D/2<y: # the D loop instead of just while D/2<X and D/2<y:). It should help others understand your code better Commented Aug 11, 2024 at 9:23
  • ... once it gets to L=0.02 it goes to the 2nd to last and so on ... isn't it because you set the while condition to be L < 0.02? Since the domain of L is [0,0.02] and I assume L is initiated with 0 in the beginning of D-loop and is incremented by dL at the end of the L-loop, I think you should change the condition to L <= 0.02 Commented Aug 11, 2024 at 9:43
  • @RexyGamaliel, the issue isnt that it does not go far enough in L the problem that i am having is that after stepping in D the loop does not go back through L with the new D. I am happy with the domain of L that it uses but it only goes through that domain for a single value of D, rather than for all values of D Commented Aug 11, 2024 at 10:29
  • Looking at the order of the while loops, I guess it is expected to do so. It starts with the initial input set of (X_0, Y_0, D_0, L_0), iterates through all possible values of L: {L_0, L_1, ..., L_n} (the last value corresponds to (X_0, Y_0, D_0, L_n)), and only then increment D to its next value, i.e. the input set becomes (X_0, Y_0, D_1, L_0). ... It's that unless I misunderstood it, i.e. D never gets incremented even after iterating through all values of L Commented Aug 11, 2024 at 11:45
  • What it is doing is starting at '(X_0, Y_0, D_0, L_0)' iterating through 'L:{L_0, L_1, ..., L_N}' correctly. Stepping down to D-Loop and iterating through '(X_0, Y_0, D:{D_0, D_1, ..., D_N}, L_N)', stepping to the Y-loop iterating through '(X_0, Y:{Y_0, Y_1, ..., Y_N}, D_N, L_N)' and finally to the X-Loop for '(X:{X_0, X_1, ..., X_N}, Y_N, D_N, L_N)' Commented Aug 11, 2024 at 16:49

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.