虽然我已经知道没有太多关于使用GLPK-Java库的文档,但我还是要问...(而且我不能使用其他求解器)
我有一个涉及调度的基本问题。学生在课程中学习一些基本限制。
示例问题是:
We consider {s1, s2} a set of two students. They need to take two
courses {c1, c2} during two semesters {t1, t2}.
We assume the courses are the same. We also assume that the students
cannot take more than one course per semester, and we'd like to determine the
minimum capacity X that must be offered by the classroom, assuming they
all offer the same capacity.
我们以CPLEX格式提供的示例如下所示:
minimize X
subject to
y111 + y112 = 1
y121 + y122 = 1
y211 + y212 = 1
y221 + y222 = 1
y111 + y112 <= 1
y121 + y122 <= 1
y211 + y212 <= 1
y221 + y222 <= 1
y111 + y112 -X <= 0
y121 + y122 -X <= 0
y211 + y212 -X <= 0
y221 + y222 -X <= 0
end
我可以通过glpsol命令通过解算器运行它并让它解决但我需要使用API编写它。我从来没有真正使用线性编程,文档还有一些不足之处。虽然这最简单,但真正的问题包括解决600名学生超过12个学期,他们必须参加18个学期的12个课程,某些课程只有某些学期和一些有先决条件的课程。
我需要帮助它将简单问题转换为使用API的编码示例。我假设一旦我看到这个非常简单的问题如何映射到API调用,我就可以弄清楚如何为更复杂的问题创建应用程序。
从库中的示例我可以看到你设置的列在这种情况下是学期,行是学生
// Define Columns
GLPK.glp_add_cols(lp, 2); // adds the number of columns
GLPK.glp_set_col_name(lp, 1, "Sem_1");
GLPK.glp_set_col_kind(lp, 1, GLPKConstants.GLP_IV);
GLPK.glp_set_col_bnds(lp, 1, GLPKConstants.GLP_LO, 0, 0);
GLPK.glp_set_col_name(lp, 2, "Sem_2");
GLPK.glp_set_col_kind(lp, 2, GLPKConstants.GLP_IV);
GLPK.glp_set_col_bnds(lp, 2, GLPKConstants.GLP_LO, 0, 0);
此时我会假设您需要设置行限制,但我不知所措。任何方向都将不胜感激。
答案 0 :(得分:1)
使用API时,优化问题基本上由问题的矩阵表示,其中列是您的变量,行是您的约束。 对于您的问题,您必须定义9列,表示y111,y112,...和X。
然后,您可以通过设置使用的变量(列)来继续约束(行)
GLPK.glp_set_row_name(lp, 2, "constraint1");
GLPK.glp_set_row_bnds(lp, 2, GLPKConstants.GLP_FX, 0, 1.0); // equal to 1.0
GLPK.intArray_setitem(ind, 1, 1); // Add first column at first position
GLPK.intArray_setitem(ind, 2, 2); // Add second column at second position
// set the coefficients to the variables (for all 1. except X is -1. in the example)
GLPK.doubleArray_setitem(val, 1, 1.);
GLPK.doubleArray_setitem(val, 2, 1.);
GLPK.glp_set_mat_row(lp, 1, 2, ind, val);
这将代表y111 + y112 = 1
约束 - 11要去。
在GLPK for Java包中也应该是glpk的文档,其中包含GLPK函数的良好文档(在glpk for java中也可以使用)。另请参阅lp.java示例。