import numpy as np
from [Link] import minimize
def objective(X):
# Newspaper、Radio 、TV、Direct Mail
x1, x2, x3, x4 = X
profit_newspaper = 100 * (x1 ** 0.7)
profit_radio = 125 * (x2 ** 0.65)
profit_tv = 180 * (x3 ** 0.6)
profit_mail = 250 * (x4 ** 0.5)
total_profit = profit_newspaper + profit_radio + profit_tv +
profit_mail
return -total_profit
constraint1 = {
'type': 'eq',
'fun': lambda X: X[0] + X[1] + X[2] + X[3] - 20000
}
constraints = [constraint1]
bounds = [(500, 20000), (500, 20000), (500, 20000), (500, 20000)]
X0 = [5000, 5000, 5000, 5000]
result = minimize(
fun=objective,
x0=X0,
method='SLSQP',
bounds=bounds,
constraints=constraints,
options={'disp': True}
)
print("Optimal Advertising Budget Allocation Result:")
x1, x2, x3, x4 = result.x
print(f"1. Newspaper advertising expenses:{x1:.2f} $")
print(f"2. Radio advertising expenditure:{x2:.2f} $")
print(f"3. TV advertising expenses:{x3:.2f} $")
print(f"4. Direct mail advertising expenses:{x4:.2f} $")
print(f"5. Total budget consumption:{x1+x2+x3+x4:.2f} $")
print(f"6. Maximum total profit:{-[Link]:.2f} $")
Optimization terminated successfully (Exit mode 0)
Current function value: -129096.39680063355
Iterations: 36
Function evaluations: 200
Gradient evaluations: 36
Optimal Advertising Budget Allocation Result:
1. Newspaper advertising expenses:11321.80 $
2. Radio advertising expenditure:4568.71 $
3. TV advertising expenses:3246.21 $
4. Direct mail advertising expenses:863.28 $
5. Total budget consumption:20000.00 $
6. Maximum total profit:129096.40 $
def objective(X):
# X = [x1, x2]:Sofa 、Table
x1, x2 = X
profit_sofa = (220 - 0.4 * x1) - 60
profit_table = (180 - 0.2 * x2) - 45
total_profit = x1 * profit_sofa + x2 * profit_table
return -total_profit
constraint1 = {
'type': 'ineq',
'fun': lambda X: 800 - (2 * X[0] + 3 * X[1])
}
constraint2 = {
'type': 'ineq',
'fun': lambda X: 500 - (2 * X[0] + X[1])
}
constraints = [constraint1, constraint2]
bounds = [(0, None), (0, None)] # None means no upper limit
X0 = [100, 100]
result = minimize(
fun=objective,
x0=X0,
method='SLSQP',
bounds=bounds,
constraints=constraints,
options={'disp': True}
)
print("Problem 2 Optimal product combination results:")
x1, x2 = result.x
p1 = 220 - 0.4 * x1 # Sofa price
p2 = 180 - 0.2 * x2 # Table price
print(f"[Link] production of sofas:{x1:.2f} P")
print(f"[Link] output of dining table:{x2:.2f} P")
print(f"[Link] selling price for sofa:{p1:.2f} $/P")
print(f"[Link] selling price for dining table:{p2:.2f} $/P")
print(f"[Link] time consumption:{2*x1 + 3*x2:.2f} hour(≤800)")
print(f"[Link] of labor consumption:{2*x1 + x2:.2f}
hour(≤500)")
print(f"[Link] total profit:{-[Link]:.2f} $")
Optimization terminated successfully (Exit mode 0)
Current function value: -31960.22727273803
Iterations: 4
Function evaluations: 12
Gradient evaluations: 4
Problem 2 Optimal product combination results:
[Link] production of sofas:144.32 P
[Link] output of dining table:170.45 P
[Link] selling price for sofa:162.27 $/P
[Link] selling price for dining table:145.91 $/P
[Link] time consumption:800.00 hour(≤800)
[Link] of labor consumption:459.09 hour(≤500)
[Link] total profit:31960.23 $
import pandas as pd
from [Link] import curve_fit
import [Link] as plt
df = pd.read_excel('M2_Problem_Set_P3.xlsx')
def s_curve(t, a, b, mu, sigma):
return a + (b - a) / (1 + [Link](-(t - mu) / sigma))
# read data
t_data = df["Independent variable, x"].values
y_data = df["Dependent variable, y"].values
initial_guess = [min(y_data), max(y_data), [Link](t_data), 1.0]
popt, pcov = curve_fit(
f=s_curve,
xdata=t_data,
ydata=y_data,
p0=initial_guess,
bounds=([0, min(y_data)+1, min(t_data), 0.1], [max(y_data)/2,
max(y_data)*2, max(t_data), 10]))
a, b, mu, sigma = popt
t_fit = [Link](min(t_data), max(t_data), 100)
y_fit = s_curve(t_fit, a, b, mu, sigma)
[Link](figsize=(10, 6))
[Link](t_data, y_data, label="actual data", color="blue")
[Link](t_fit, y_fit, label="S Curve fitting results", color="red",
linewidth=2)
[Link]("independent variable")
[Link]("dependent variable")
[Link]("S Curve fitting")
[Link]()
[Link]()
print("S Optimal parameters for curve fitting:")
print(f"[Link] parameters a:{a:.2f}")
print(f"[Link] parameter b:{b:.2f}")
print(f"[Link] argument mu:{mu:.2f}")
print(f"[Link] parameter sigma:{sigma:.2f}")
S Optimal parameters for curve fitting:
[Link] parameters a:4.35
[Link] parameter b:9.62
[Link] argument mu:1.82
[Link] parameter sigma:0.25
def objective(a):
s = 0.3
total_share = 0.0
for ai in a:
s = s * (1 - 0.1) + (1 - s) * (0.2 * [Link](ai))
total_share += s
else:
avg_share = total_share / 12
return -avg_share
constraint1 = {
'type': 'eq',
'fun': lambda a: [Link](a) - 12
}
constraints = [constraint1]
bounds = [(0.01, 12) for _ in range(12)]
a0 = [1.0 for _ in range(12)]
result = minimize(
fun=objective,
x0=a0,
method='SLSQP',
bounds=bounds,
constraints=constraints,
options={'disp': True, 'maxiter': 1000}
)
print("Problem 4 Optimal advertising policy results:")
a_opt = result.x
s = 0.3
monthly_share = []
for i, ai in enumerate(a_opt, 1):
s = s * 0.9 + (1 - s) * 0.2 * [Link](ai)
monthly_share.append(s)
avg_share = [Link](monthly_share)
print("[Link] optimal advertising expenditure(eq:million
dollars):")
for i, j in enumerate(a_opt, 1):
print(f"-- {i:2d}Month:{j:.4f}")
print(f"[Link] budget consumption:{[Link](a_opt):.4f} million
dollars")
print("[Link] share at the end of each month:")
for i, s_month in enumerate(monthly_share, 1):
print(f" -- {i:2d}Month:{s_month:.4f}({s_month*100:.2f}%)")
print(f"4.12 Month average end of term market share:{avg_share:.4f}
({avg_share*100:.2f}%)")
Optimization terminated successfully (Exit mode 0)
Current function value: -0.6145136242727388
Iterations: 26
Function evaluations: 340
Gradient evaluations: 26
Problem 4 Optimal advertising policy results:
[Link] optimal advertising expenditure(eq:million dollars):
-- 1Month:2.9155
-- 2Month:1.7586
-- 3Month:1.2278
-- 4Month:1.0127
-- 5Month:0.9330
-- 6Month:0.8954
-- 7Month:0.8570
-- 8Month:0.7945
-- 9Month:0.6892
-- 10Month:0.5311
-- 11Month:0.2859
-- 12Month:0.0992
[Link] budget consumption:12.0000 million dollars
[Link] share at the end of each month:
-- 1Month:0.5090(50.90%)
-- 2Month:0.5884(58.84%)
-- 3Month:0.6207(62.07%)
-- 4Month:0.6350(63.50%)
-- 5Month:0.6420(64.20%)
-- 6Month:0.6456(64.56%)
-- 7Month:0.6466(64.66%)
-- 8Month:0.6450(64.50%)
-- 9Month:0.6394(63.94%)
-- 10Month:0.6280(62.80%)
-- 11Month:0.6050(60.50%)
-- 12Month:0.5694(56.94%)
4.12 Month average end of term market share:0.6145(61.45%)
transport_cost = [
[23, 30, 32, 26],
[33, 27, 25, 24]
]
demand = [300, 250, 150, 400]
def objective(vars):
x1, x2 = vars[0], vars[1]
s11, s12, s13, s14 = vars[2:6]
s21, s22, s23, s24 = vars[6:10]
cost_plant1 = 2 * (x1 ** 2) - x1 + 15
cost_plant2 = 1 * (x2 ** 2) + 0.3 * x2 + 10
cost_transport = (s11*transport_cost[0][0] + s12*transport_cost[0]
[1] + s13*transport_cost[0][2] + s14*transport_cost[0][3] +
s21*transport_cost[1][0] + s22*transport_cost[1]
[1] + s23*transport_cost[1][2] + s24*transport_cost[1][3])
total_cost = cost_plant1 + cost_plant2 + cost_transport
return total_cost
constraints = [
{'type': 'eq', 'fun': lambda vars: vars[0] - (vars[2] + vars[3] +
vars[4] + vars[5])},
{'type': 'eq', 'fun': lambda vars: vars[1] - (vars[6] + vars[7] +
vars[8] + vars[9])},
{'type': 'eq', 'fun': lambda vars: vars[2] + vars[6] - demand[0]},
{'type': 'eq', 'fun': lambda vars: vars[3] + vars[7] - demand[1]},
{'type': 'eq', 'fun': lambda vars: vars[4] + vars[8] - demand[2]},
{'type': 'eq', 'fun': lambda vars: vars[5] + vars[9] - demand[3]},
{'type': 'ineq', 'fun': lambda vars: 600 - vars[0]},
{'type': 'ineq', 'fun': lambda vars: 600 - vars[1]}
]
bounds = [
(0, 600), (0, 600),
(0, None), (0, None), (0, None), (0, None),
(0, None), (0, None), (0, None), (0, None)
]
s11, s21 = demand[0]/2, demand[0]/2
s12, s22 = demand[1]/2, demand[1]/2
s13, s23 = demand[2]/2, demand[2]/2
s14, s24 = demand[3]/2, demand[3]/2
x1_init = s11 + s12 + s13 + s14
x2_init = s21 + s22 + s23 + s24
vars0 = [x1_init, x2_init, s11, s12, s13, s14, s21, s22, s23, s24]
result = minimize(
fun=objective,
x0=vars0,
method='SLSQP',
bounds=bounds,
constraints=constraints,
options={'disp': True, 'maxiter': 1000}
)
print("Problem 5 Optimal Production and Transportation Plan Results:")
x1, x2 = result.x[0], result.x[1]
s11, s12, s13, s14 = result.x[2:6]
s21, s22, s23, s24 = result.x[6:10]
print("[Link] production planning:")
print(f"Factory 1 output:{x1:.2f} P(≤600)")
print(f"Factory 2 output:{x2:.2f} P(≤600)")
print("[Link] Plan(eq:piece):")
print(f"Factory1 Customer1:{s11:.2f} | Factory1→Customer2:{s12:.2f} |
Factory1→Customer3:{s13:.2f} | Factory1→Customer4:{s14:.2f}")
print(f"Factory2→Customer1:{s21:.2f} | Factory2→Customer2:{s22:.2f} |
Factory2→Customer3:{s23:.2f} | Factory2→Customer4:{s24:.2f}")
print("[Link] satisfaction verification:")
for i in range(4):
total_demand = [s11+s21, s12+s22, s13+s23, s14+s24][i]
print(f"Customer{i+1}:requirement{demand[i]}pieces,Actual
supply{total_demand:.2f}pieces")
print(f"[Link] total cost:{[Link]:.2f} ")
Optimization terminated successfully (Exit mode 0)
Current function value: 887105.0000034004
Iterations: 12
Function evaluations: 138
Gradient evaluations: 12
Problem 5 Optimal Production and Transportation Plan Results:
[Link] production planning:
Factory 1 output:500.00 P(≤600)
Factory 2 output:600.00 P(≤600)
[Link] Plan(eq:piece):
Factory1 Customer1:300.00 | Factory1→Customer2:0.00 |
Factory1→Customer3:0.00 | Factory1→Customer4:200.00
Factory2→Customer1:0.00 | Factory2→Customer2:250.00 |
Factory2→Customer3:150.00 | Factory2→Customer4:200.00
[Link] satisfaction verification:
Customer1:requirement300pieces,Actual supply300.00pieces
Customer2:requirement250pieces,Actual supply250.00pieces
Customer3:requirement150pieces,Actual supply150.00pieces
Customer4:requirement400pieces,Actual supply400.00pieces
[Link] total cost:887105.00