LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Introduction to Optimization in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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 bearbeiten und ausführen