Iteration to get variables below target value in python

Viewed 42

I'm trying to create a code that both calculates two contaminant levels and iterates w.r.t to one of the inputs to get them below their respective targets. There's a huge amount of code before the contaminants are calculated, so it would only be messy if I shared the entire code. I'll try my best with some pseudo-code/snippets of the calculations instead.

# pseudo inputs.

a = 10.0; b = 5.0; c = 15.0
d = 0.5; e = 1.0

# pseudo outputs
contaminant_1 = a * b + c
contaminant_2 = a * d + e

# target values
contaminant_1_target = 10.0
contaminant_2_target = 2.0

I want the code to use the input a to iterate in order to get contaminant_1 < contaminant_1_target and contaminant_2 < contaminant_2_target. Any tips on how I can do this?

1 Answers

If you want to iterate to find value a which met conditions then you would need for-loop or while-loop and change value a in this loop

a = 10.0
b = 5.0
c = 15.0
d = 0.5
e = 1.0

# target values
contaminant_1_target = 10.0
contaminant_2_target = 2.0

step = -0.1  # precision

while True:
    # pseudo outputs
    contaminant_1 = a * b + c
    contaminant_2 = a * d + e
    #print(contaminant_1, contaminant_2)
    
    if contaminant_1 < contaminant_1_target and contaminant_2 < contaminant_2_target:
        print(a)
        break
    else:
        a += step

EDIT:

But if you use wrong step then it may calculate in wrong direction and it will run forever. It would need to check if current value is bigger than previous value and make correcion.

It looks similar to machine learning and linear regresion which use Least squares to find matching function

it compares (contaminant_1 - contaminant_1_target)^2 with previous value to see if it has to change direction.


EDIT:

Version which compare current and previous (contaminant_1 - contaminant_1_target)^2 and change step = -step - and it can start with a below or above result.

(Wikipedia: least squares)

# - constants -  `UPPER_CASE_NAMES`

B = 5.0
C = 15.0
D = 0.5
E = 1.0

# - variables - `lower_case_names`

a = 1.0
#a = 10.0

# --------------------------------

# target values
contaminant_1_target = 10.0
contaminant_2_target = 2.0

step = -0.1  # precision

# at start it should be rather `infinity`
previous_LS_1 = 0  # least squares
previous_LS_2 = 0  # least squares

while True:
    # pseudo outputs
    contaminant_1 = a * B + C
    contaminant_2 = a * D + E
    #print(contaminant_1, contaminant_2)
    
    # ---

    current_LS_1 = (contaminant_1 - contaminant_1_target)**2
    current_LS_2 = (contaminant_2 - contaminant_2_target)**2

    if current_LS_1 > previous_LS_1 and current_LS_2 > previous_LS_2:
        print('change direction:', a)
        step = -step
        
    previous_LS_1 = current_LS_1
    previous_LS_2 = current_LS_2

    # ---
        
    if contaminant_1 < contaminant_1_target and contaminant_2 < contaminant_2_target:
        print('a:', a)
        break
    else:
        a += step
Related