Changoleon
Changoleon

Reputation: 31

how to extract two outputs from scipy minimize

I want to have a function that based on length, width, and height (within a certain bound) minimizes the volume. But in addition to volume, I also want to calculate half_volume = volume /2.

My minimization algorithm successfully determines the correct height, width, and length (obviously the lower band values). But how do I extract the half_volume value?

def calcVolume(x):
    length = x[0]
    width  = x[1]
    height = x[2]
    volume = length * width * height
    half_volume = volume / 2
    return volume

sol = minimize(calcVolume, initial_guess, method = 'SLSQP', bounds = x_bounds,options = {'ftol': 1e-8, 'maxiter': 2000, 'disp': True})

Because I am running minimization, I can not return more than one output (which is volume in this case). If I print solution.x it will give me correct height, weight, and lenght as well as the minimized volume. How do I get access to half_volume?

Upvotes: 0

Views: 179

Answers (1)

1__
1__

Reputation: 1571

You can't get access to half_volume since the function is not returning it.

The only way to get to is through using the half_volume calculation outside the function like ...

def calcVolume(x):
    length = x[0]
    width  = x[1]
    height = x[2]
    volume = length * width * height
    return volume

half_volume = calcVolume(x) / 2

Upvotes: 0

Related Questions