如何在AMPL+CPLEX中用二进制变量建模机器效率阶跃下降?
I’ll walk you through exactly how to model this 10% efficiency drop after 100 hours of machine operation using binary variables. We’ll build on your existing AMPL setup (3 machines, 5 parts) with a complete, runnable example.
Core Idea
Each machine has two operational states:
- High-efficiency state: Total runtime ≤ 100 hours, producing at full capacity
- Low-efficiency state: Total runtime > 100 hours, producing at 90% capacity
We use a binary variable y[m] to track if machine m has entered the low-efficiency state (y[m] = 1 if yes, 0 otherwise). Then split production time into two variables: one for high-efficiency hours, one for low-efficiency hours, and link them to y[m] via constraints to enforce the state switch.
Complete AMPL Code
Model File (production.mod)
# Define parameters param M; # Number of machines param N; # Number of parts param r{M, N}; # Base production rate (parts per hour) for machine m making part n param d{N}; # Demand for each part param T{M}; # Total available hours for each machine # Define variables var x1{M, N} >= 0; # High-efficiency hours: machine m making part n (max 100 total per machine) var x2{M, N} >= 0; # Low-efficiency hours: machine m making part n (only after 100 hours) var y{M} binary; # 1 if machine m has used >100 hours, 0 otherwise # Objective: Minimize total machine runtime (adjust to your goal, e.g., minimize cost) minimize Total_Runtime: sum{m in 1..M, n in 1..N} (x1[m,n] + x2[m,n]); # Constraints subject to Demand_Satisfaction{n in 1..N}: sum{m in 1..M} (r[m,n] * x1[m,n] + 0.9 * r[m,n] * x2[m,n]) >= d[n]; subject to Max_High_Efficiency_Time{m in 1..M}: sum{n in 1..N} x1[m,n] <= 100; # High-efficiency can't exceed 100 hours per machine subject to Low_Efficiency_Only_If_Over_100{m in 1..M}: sum{n in 1..N} x2[m,n] <= (T[m] - 100) * y[m]; # Can't use low-efficiency hours unless y[m]=1 subject to Trigger_Low_Efficiency{m in 1..M}: sum{n in 1..N} (x1[m,n] + x2[m,n]) >= 100 * y[m]; # If y[m]=1, total time must hit at least 100 subject to Total_Available_Time{m in 1..M}: sum{n in 1..N} (x1[m,n] + x2[m,n]) <= T[m]; # Don't exceed machine's total available hours
Data File (production.dat)
data; param M := 3; # Your existing machine count param N := 5; # Your existing part count # Example production rates (replace with your actual data) param r: 1 2 3 4 5 := 1 10 15 8 12 9 2 12 10 14 7 11 3 9 13 11 15 8; # Example part demands param d := 1 500 2 700 3 600 4 800 5 650; # Example total available hours per machine param T := 1 150 2 200 3 180;
Key Explanations
- Binary Variable
y[m]: Acts as a state switch. Wheny[m] = 0, theLow_Efficiency_Only_If_Over_100constraint forces allx2[m,n]to 0 (no low-efficiency hours allowed). Wheny[m] = 1, this constraint unlocks low-efficiency hours up to the machine’s remaining available time after 100 hours. - Efficiency Calculation: The
Demand_Satisfactionconstraint uses the full production rate forx1(high-efficiency hours) and a 90% rate forx2(low-efficiency hours) to reflect the step drop. - Trigger Constraint:
Trigger_Low_Efficiencyprevents the solver from settingy[m] = 1unnecessarily—it ensures the machine only switches to low efficiency if it actually uses at least 100 hours.
How to Run
- Save the model and data files
- In AMPL, execute these commands:
model production.mod; data production.dat; option solver cplex; solve; display x1, x2, y;
This will output the optimal production plan that accounts for the efficiency drop after 100 hours of machine operation.
内容的提问来源于stack exchange,提问作者Madras

