一位农民有一块农田,比如说L km2,可种植小麦或大麦或两者的组合。农民的肥料量有限,F公斤,杀虫剂P公斤。每平方公里的小麦需要F1公斤的肥料和P1公斤的杀虫剂,而每平方公里的大麦需要F2公斤的肥料和P2公斤的杀虫剂。设S1为每平方公里小麦的售价,S2为大麦的售价。如果我们分别用x1和x2表示种植小麦和大麦的土地面积,那么通过选择x1和x2的最佳值可以使利润最大化。这个问题可以用标准形式的以下线性规划问题来表达: 看到这个页面,他们已经给出了约束。 http://en.wikipedia.org/wiki/Linear_programming
解决此类问题的程序是什么?
答案 0 :(得分:1)
您的问题是所谓的线性编程问题。存在各种算法和各种实现。我使用了一个名为GLPK的程序,效果很好。您使用特定于域的编程语言陈述您的问题,GLPK处理该程序以找到解决方案。对GLPK的网络搜索应该找到它。
答案 1 :(得分:1)
import java.text.DecimalFormat;
public class CandidateCode {
public String get_total_profit(String input1) {
String[] inputs = input1.split(",");
// Piece of farm land in square kilometer
float L = Float.valueOf(inputs[0]);
// Fertilizer in kg
float F = Float.valueOf(inputs[1]);
// Insecticide in kg
float P = Float.valueOf(inputs[2]);
// Fertilizer required in kg for square kilometer of Wheat
float F1 = Float.valueOf(inputs[3]);
// Insecticide required in kg for square kilometer of Wheat
float P1 = Float.valueOf(inputs[4]);
// Fertilizer required in kg for square kilometer of Rice
float F2 = Float.valueOf(inputs[5]);
// Insecticide required in kg for square kilometer of Rice
float P2 = Float.valueOf(inputs[6]);
// Selling price of wheat per square kilometer
float S1 = Float.valueOf(inputs[7]);
// Selling price of rice per square kilometer
float S2 = Float.valueOf(inputs[8]);
// Result Variables
float totalRiceInsecUsed = 0f;
float totalRiceFertUsed = 0f;
float totalWheatInsecUsed = 0f;
float totalWheatFertUsed = 0f;
float areaOfWheat = 0.00f;
float areaOfRice = 0.00f;
float amount = 0.00f;
while (true) {
if ((L == areaOfRice + areaOfWheat)
|| P == totalRiceInsecUsed + totalWheatInsecUsed
|| F == totalRiceFertUsed + totalWheatFertUsed || F2 == 0
|| F1 == 0 || P2 == 0 || P1 == 0) {
break;
}
float calRiceProfit = Math.min(F / F2, P / P2) * S2;
float calWheatProfit = Math.min(F / F1, P / P1) * S1;
if (calRiceProfit > calWheatProfit) {
float areaInsecUsed = P / P2;
float areaFertUsed = F / F2;
if (areaInsecUsed > areaFertUsed) {
L = L - areaFertUsed;
F2 = 0;
totalRiceFertUsed = totalRiceFertUsed + F2;
areaOfRice = areaOfRice + areaFertUsed;
amount = amount + areaFertUsed * S2;
} else if (areaInsecUsed < areaFertUsed) {
L = L - areaInsecUsed;
P2 = 0;
totalRiceInsecUsed = totalRiceInsecUsed + areaInsecUsed;
areaOfRice = areaOfRice + areaInsecUsed;
amount = amount + areaInsecUsed * S2;
}
} else {
float areaInsecUsed = P / P1;
float areaFertUsed = F / F1;
if (areaInsecUsed > areaFertUsed) {
L = L - areaFertUsed;
F1 = 0;
totalWheatFertUsed = totalWheatFertUsed + F1;
areaOfWheat = areaOfWheat + areaFertUsed;
amount = amount + areaFertUsed * S1;
} else if (areaInsecUsed < areaFertUsed) {
L = L - areaInsecUsed;
P1 = 0;
totalWheatInsecUsed = totalWheatInsecUsed + areaInsecUsed;
areaOfWheat = areaOfWheat + areaInsecUsed;
amount = amount + areaInsecUsed * S1;
}
}
}
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
return df.format(amount) + "," + df.format(areaOfWheat) + ","
+ df.format(areaOfRice);
}
}