[Python] How to "squeeze" bounds to some number on both sides of a calculation [Python]

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    So I have this piece of code where I want to squeeze both bounds (b1 and b2) so that eventually y1 < 0.01 and y2 > -0.01, then takes the average from both x-intercept values, putting that in a dictionary.

    Right now I am just using -1 and 1 for testing purposes to speed up the calculation.

    Currently the code does get close to the result I'm looking for, but for accuracy's I want it to adjust both bounds instead of just one.

    #...More code above this line...
    b1 = 0.005 # lower bound
    b2 = 0.8 # upper bound

    beta = {}

    for i in purposes:
    # Beta value for work
    if ( i == 'work' ):
    ideal = staline_euclidean_odin[0]
    print('Ideal for '+i+':', ideal)

    # To determine the distances using b1
    first_avg_dist = Distance_array(i,n_zones,trip_prod,trip_attr,dist_matrix,b1,balance_constant)
    y1 = first_avg_dist - ideal # Should always be above 0

    # To determine the distances using b2
    second_avg_dist = Distance_array(i,n_zones,trip_prod,trip_attr,dist_matrix,b2,balance_constant)
    y2 = second_avg_dist - ideal # Should always be below 0

    print('y1 is:', y1)
    print('y2 is:', y2)

    print('\nUpper bound')

    while( y2 < -1):
    x_intercept = ((b1 * y2)-(b2 * y1))/(y2-y1)
    x_int_float = sum(x_intercept)
    print('\nX-intercept:',x_int_float)

    b2 = x_int_float

    second_avg_dist = Distance_array(i,n_zones,trip_prod,trip_attr,dist_matrix,b2,balance_constant)
    y2 = second_avg_dist - ideal

    print('\nThe',i,'value is',x_int_float)
    beta = x_int_float

    print('\nFinal beta values are:', beta)


    As it stands, I removed the second while loop (while( y > 1)) because it basically did nothing lol. Here is an example of the output though of the above code:

    Ideal for work: 12.544276968293396
    Average Trip Distance: [82.47777808]
    Average Trip Distance: [2.43391276]
    y1 is: [69.93350111]
    y2 is: [-10.11036421]

    Upper bound

    X-intercept: 0.6995833157953546
    Average Trip Distance: [2.6939947]

    X-intercept: 0.6138285241716822
    Average Trip Distance: [2.98388599]

    X-intercept: 0.5406073171738418
    Average Trip Distance: [3.30435637]

    X-intercept: 0.478099357080947
    Average Trip Distance: [3.65585204]

    X-intercept: 0.4247498852729269
    Average Trip Distance: [4.03831264]

    X-intercept: 0.3792322681820317
    Average Trip Distance: [4.45095828]

    X-intercept: 0.34041508943238846
    Average Trip Distance: [4.89207298]

    X-intercept: 0.30733341952072446
    Average Trip Distance: [5.35880098]

    X-intercept: 0.27916383522587485
    Average Trip Distance: [5.84697726]

    X-intercept: 0.25520274705938095
    Average Trip Distance: [6.35105819]

    X-intercept: 0.23484773433432096
    Average Trip Distance: [6.86423559]

    X-intercept: 0.2175817182463294
    Average Trip Distance: [7.37877097]

    X-intercept: 0.20295979202234204
    Average Trip Distance: [7.88651161]

    X-intercept: 0.19059842177765768
    Average Trip Distance: [8.37949335]

    X-intercept: 0.18016663825325702
    Average Trip Distance: [8.8505177]

    X-intercept: 0.17137881442565908
    Average Trip Distance: [9.29361024]

    X-intercept: 0.16398866309095483
    Average Trip Distance: [9.7043061]

    X-intercept: 0.1577841608300955
    Average Trip Distance: [10.0797501]

    X-intercept: 0.15258318119652844
    Average Trip Distance: [10.41863338]

    X-intercept: 0.14822968457439337
    Average Trip Distance: [10.72100868]

    X-intercept: 0.14459036044782916
    Average Trip Distance: [10.98803384]

    X-intercept: 0.14155165130996347
    Average Trip Distance: [11.22168922]

    X-intercept: 0.13901710944153153
    Average Trip Distance: [11.42450479]

    X-intercept: 0.13690505149520227
    Average Trip Distance: [11.59932024]

    The work value is 0.13690505149520227

    Final beta values are: {'work': 0.13690505149520227}


    Also, if there is a more efficient/better way to do this, let me know. This has so far been the fastest method I've used.

    Continue reading...

Compartilhe esta Página