[英]What are the constraints for this linear programming problem?
一位作家為網站創建數學問題。 她每道題的報酬為 5 美元,每道代數題的報酬為 2 美元。 平均而言,她創建一個單詞問題需要 4 分鍾,創建一個代數問題需要 2 分鍾。 她的老板希望她總共至少做 50 道題,而且代數題比單詞題多。 如果作者有三個小時,她能獲得的最大利潤是多少?
如果我的限制有誤,請更正。
4 分鍾創建一個單詞問題和 2 分鍾創建一個代數問題。 如果作者有 180 分鍾。 約束 1 是4x+2y<=180
她的老板希望她總共至少做 50 道題。 約束 2 是x<=50
並且代數問題比單詞問題多。 約束 3 是y>+x
您可能需要這些約束:
#!/usr/bin/env python3
import cvxpy as cp
import numpy as np
word_count = cp.Variable()
algebraic_count = cp.Variable()
constraints = [
4*word_count + 2*algebraic_count <= 180, # Time constraint
word_count + algebraic_count >= 50, # Need at least 50 problems
algebraic_count >= word_count, # More algebraic problems then word problems
]
objective = cp.Maximize(5*word_count + 2*algebraic_count)
prob = cp.Problem(objective, constraints)
optval = prob.solve()
print("Optimum profit", optval)
print("Word problem count", word_count.value)
print("Algebraic problem count", algebraic_count.value)
給予
Optimum profit 209.99999999414464
Word problem count 29.999999990727126
Algebraic problem count 30.000000020254507
請注意,這里的數字非常接近整數,我們可以相信它們是正確的。 然而,這在一般情況下並非如此。 如果您的字數問題的值為 29.5,則意味着您需要將字數強制為<=29
或>=30
以嘗試獲得整數值。
或者,您可以通過設置使用整數編程模式:
word_count = cp.Variable(integer=True)
algebraic_count = cp.Variable(integer=True)
這使
Optimum profit 210.0
Word problem count 30.0
Algebraic problem count 30.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.