[英]C++ Algorithm for solving maximisation in linear programming
I am learning about maximisation with linear programming, and have come across an algorithm for maximisation with two variables ( silver and gold in this instance) but I am unsure what a certain section of the code is doing:我正在学习线性规划的最大化,并且遇到了一个带有两个变量(在本例中为银和金)的最大化算法,但我不确定代码的某个部分在做什么:
using namespace std;
class PreciousStones {
int n;
vector<int> as;
vector<int> ag;
The function below is the section I am unclear on:下面的功能是我不清楚的部分:
double maxg (double s) {
double g = 0;
for (int i=0; i<n; i++)
if (s == 0)
g += ag[i];
else if (as[i] <= s)
s -= as[i];
else {
g += (1-s/as[i])*ag[i];
s = 0;
}
return g;
}
The rest of the code is below (for context), if anyone knows of some relevant papers on this algorithm, or can provide a brief explanation on this function I would appreciate it greatly其余代码如下(用于上下文),如果有人知道有关此算法的一些相关论文,或者可以提供有关此功能的简要说明,我将不胜感激
public:
double value(vector <int> silver, vector <int> gold) {
n = silver.size();
as = silver;
ag = gold;
for (int i=0; i<n; i++)
for (int j=i+1; j<n; j++)
if (as[j]*ag[i] > as[i]*ag[j]) {
swap(as[i], as[j]);
swap(ag[i], ag[j]);
}
double lo = 0;
double hi = 51*100;
double D = 1e-10;
while (lo+D < hi && lo*(1+D) < hi) {
double mid = (lo+hi)/2;
if (mid <= maxg(mid))
lo = mid;
else
hi = mid;
}
return lo;
}
};
The magic words you need to google are "Simplex Algorithm" for linear programming constraint maximisation.您需要在 google 上搜索的魔术词是用于线性规划约束最大化的“Simplex Algorithm”。 This slide deck looks like it might be what you want.
这个幻灯片看起来可能是你想要的。 http://www.cs.nccu.edu/~melikyan/mat_fm/lec/lec5_4.pdf
http://www.cs.nccu.edu/~melikyan/mat_fm/lec/lec5_4.pdf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.