Input/output in GLPK for Java

2019-07-16 17:59发布

问题:

I find a lot of GLPK for Java examples about how to specify the model (problem/constraints) to the solver and read parameters from a data file, but very little about programmatic parameter input/output. In my case I need to submit values (array of weights and values) to a knapsack problem programmatically and postprocess the solution as well (perform addtional numeric checks on the solution found) in order to decide whether to proceed or not. Think of the equivalent of reading a param: line from a data file without calling glp_mpl_read_data or printing details of a solution to a file without calling glp_print_mip/sol/itp. Can you provide example code or point me to the right resource?

回答1:

This is only a partial answer. I managed to solve the output part using the

GLPK.get_ipt_obj_val
GLPK.get_mip_obj_val
GLPK.get_ipt_col_val
GLPK.get_mip_col_val

functions as in the following example

    static void writeMipSolution(glp_prob lp) {

    String name = GLPK.glp_get_obj_name(lp);
    double val = GLPK.glp_mip_obj_val(lp);

    System.out.println(name + " = " + val);

    int n = GLPK.glp_get_num_cols(lp);

    for (int i = 1; i <= n; i++) {
        name = GLPK.glp_get_col_name(lp, i);
        val = GLPK.glp_mip_col_val(lp, i);
        System.out.println(name + " = " + val);
    }
}

Still investigating the input part, though.