定义目标函数
我们的目标是最大化利润,利润计算公式为:
Profit = Total Revenue − Total Cost \text{Profit} = \text{Total Revenue} - \text{Total Cost} Profit=Total Revenue−Total Cost
总收入:
Total Revenue = ( Selling Price A M 1 × dist A M 1 ) + ( Selling Price B M 1 × dist B M 1 ) + ( Selling Price A M 2 × dist A M 2 ) + ( Selling Price B M 2 × dist B M 2 ) \text{Total Revenue} = (\text{Selling Price}_A^{M1} \times \text{dist}_A^{M1}) + (\text{Selling Price}_B^{M1} \times \text{dist}_B^{M1}) + (\text{Selling Price}_A^{M2} \times \text{dist}_A^{M2}) + (\text{Selling Price}_B^{M2} \times \text{dist}_B^{M2}) Total Revenue=(Selling PriceAM1×distAM1)+(Selling PriceBM1×distBM1)+(Selling PriceAM2×distAM2)+(Selling PriceBM2×distBM2)
总成本:
Total Cost = ( Cost A × production A ) + ( Cost B × production B ) + ( Logistics Cost A M 1 × dist A M 1 ) + ( Logistics Cost B M 1 × dist B M 1 ) + ( Logistics Cost A M 2 × dist A M 2 ) + ( Logistics Cost B M 2 × dist B M 2 ) \text{Total Cost} = (\text{Cost}_A \times \text{production}_A) + (\text{Cost}_B \times \text{production}_B) + (\text{Logistics Cost}_A^{M1} \times \text{dist}_A^{M1}) + (\text{Logistics Cost}_B^{M1} \times \text{dist}_B^{M1}) + (\text{Logistics Cost}_A^{M2} \times \text{dist}_A^{M2}) + (\text{Logistics Cost}_B^{M2} \times \text{dist}_B^{M2}) Total Cost=(CostA×productionA)+(CostB×productionB)+(Logistics CostAM1×distAM1)+(Logistics CostBM1×distBM1)+(Logistics CostAM2×distAM2)+(Logistics CostBM2×distBM2)
目标函数:
Profit = Total Revenue − Total Cost \text{Profit} = \text{Total Revenue} - \text{Total Cost} Profit=Total Revenue−Total Cost
设定束缚条件
原材料束缚:每种原材料的消耗不能高出其供应量。设原材料的需求系数为Coefficient_A和Coefficient_B,原材料供应量为Supply:
Coefficient A × production A + Coefficient B × production B ≤ Supply \text{Coefficient}_A \times \text{production}_A + \text{Coefficient}_B \times \text{production}_B \leq \text{Supply} CoefficientA×productionA+CoefficientB×productionB≤Supply
生产本领束缚:
production A ≤ 400 \text{production}_A \leq 400 productionA≤400
production B ≤ 300 \text{production}_B \leq 300 productionB≤300
市场需求与分配束缚:
总生产需求束缚:
production A + production B ≥ 500 \text{production}_A + \text{production}_B \geq 500 productionA+productionB≥500
市场分配束缚:
dist A M 1 + dist A M 2 = production A \text{dist}_A^{M1} + \text{dist}_A^{M2} = \text{production}_A distAM1+distAM2=productionA
dist B M 1 + dist B M 2 = production B \text{dist}_B^{M1} + \text{dist}_B^{M2} = \text{production}_B distBM1+distBM2=productionB
self.prob += pulp.lpSum([row[f'Coefficient_{model}'] * self.variables[f'production_{model}'] for model in self.models]) <= row['Supply'], f"{row['Material']}_Supply"
self.prob += pulp.lpSum([self.variables[f'production_{model}'] for model in self.models]) >= self.config['total_demand'], "Total Production Demand"
for model in self.models:
self.prob += pulp.lpSum([self.variables[f'dist_{model}_{market}'] for market in self.markets]) == self.variables[f'production_{model}'], f"Total Distribution {model}"
def solve(self):
"""执行求解过程并返回结果。"""
self._define_objective_function()
self._define_constraints()
self.prob.solve()
return {v.name: pulp.value(v) for v in self.prob.variables()}
# 示例使用方法
if __name__ == "__main__":
config = {
"production_limits": {
"A": 400,
"B": 300
},
"total_demand": 500
}
# 初始化模型并求解
model = ProfitMaximizationModel('parameters.xlsx', config)