Aan de slagGa gratis aan de slag

Utility maximization

Bill is an aspiring piano student who allocates hours of study in classical \(c\) and modern \(m\) music. His preferences are represented by the same utility function you just plotted:

\(U(c, m)=c^{0.7}m^{0.3}\).

Hours of study sum up to 2 (\(c+m=2\)) daily. Help Bill find the optimal studying plan.

np and minimize have been loaded for you. We have already imported symbols, diff and solve from SymPy, defined c and m as symbols and defined the Utility function U for you.

Deze oefening maakt deel uit van de cursus

Introduction to Optimization in Python

Cursus bekijken

Oefeninstructies

  • Define the utility function by unpacking vars and returning the negated function.
  • Define the constraint function.
  • Set up the constraint with type and fun.
  • Perform optimization and extract the results for c and m.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Define the utility function
def utility_function(vars):
    ____
    return ____

# Define the constraint function
def constraint(vars):
    return ____

initial_guess = [12, 12]  

# Set up the constraint
constraint_definition = {____}

# Perform optimization
result = ____
____ = result.____

print("Optimal study hours for classical music:", round(c, 2))
print("Optimal study hours for modern music:", round(m, 2))
Code bewerken en uitvoeren