0

The objective function is

def obj(w):
    a = f(w)
    return g(a)

The constraint function is

def cons(w):
    a = f(w)
    return a - 1

The function f(w) in both obj(w) and cons(w) is calculation heavy. How to calculate f(w) just once for every guess of w please?

Thanks.

Tony
  • 81
  • 3

1 Answers1

0

In general you can access variables from given function outside of it, using this mechanism:

def f(a):
    out = a * a + 2
    f.inside = out
    return out
f.inside = None

print(f(1))
print(f.inside)

or using class.

rpoleski
  • 988
  • 5
  • 12