File: Gmpl.java

package info (click to toggle)
glpk-java 1.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,580 kB
  • sloc: sh: 3,609; java: 1,794; xml: 259; makefile: 154; ansic: 35
file content (111 lines) | stat: -rw-r--r-- 3,122 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

import org.gnu.glpk.*;

public class Gmpl implements GlpkCallbackListener, GlpkTerminalListener {
    private boolean hookUsed = false;

    public static void main(String[] arg) {
        if (1 != arg.length) {
            System.out.println("Usage: java Gmpl model.mod");
            return;
        }
        GLPK.glp_java_set_numeric_locale("C");
        new Gmpl().solve(arg);
    }

    public void solve(String[] arg) {
        glp_prob lp;
        glp_tran tran;
        glp_iocp iocp;

        String fname;
        int skip = 0;
        int ret;

        // listen to callbacks
        GlpkCallback.addListener(this);

        // listen to terminal output
        GlpkTerminal.addListener(this);

        try {

            // create problem
            lp = GLPK.glp_create_prob();

            // allocate workspace
            tran = GLPK.glp_mpl_alloc_wksp();

            // read model
            fname = arg[0];
            ret = GLPK.glp_mpl_read_model(tran, fname, skip);
            if (ret != 0) {
                GLPK.glp_mpl_free_wksp(tran);
                GLPK.glp_delete_prob(lp);
                throw new RuntimeException("Model file not valid: " + fname);
            }

            // generate model
            ret = GLPK.glp_mpl_generate(tran, null);
            if (ret != 0) {
                GLPK.glp_mpl_free_wksp(tran);
                GLPK.glp_delete_prob(lp);
                throw new RuntimeException("Cannot generate model: " + fname);
            }

            // build model
            GLPK.glp_mpl_build_prob(tran, lp);

            // set solver parameters
            iocp = new glp_iocp();
            GLPK.glp_init_iocp(iocp);
            iocp.setPresolve(GLPKConstants.GLP_ON);

            // do not listen to output anymore
            GlpkTerminal.removeListener(this);

            // solve model
            ret = GLPK.glp_intopt(lp, iocp);

            // postsolve model
            if (ret == 0) {
                GLPK.glp_mpl_postsolve(tran, lp,
                                       GLPKConstants.GLP_MIP);
            }

            // free memory
            GLPK.glp_mpl_free_wksp(tran);
            GLPK.glp_delete_prob(lp);

	} catch (org.gnu.glpk.GlpkException e) {
            System.err.println("An error inside the GLPK library occured.");
            System.err.println(e.getMessage());
        } catch (RuntimeException e) {
            System.err.println(e.getMessage());
        }

        // do not listen for callbacks anymore
        GlpkCallback.removeListener(this);

        // check that the terminal hook function has been used
        if (!hookUsed) {
            throw new RuntimeException(
                "The terminal output hook was not used.");
        }
    }

    @Override
    public boolean output(String str) {
        hookUsed = true;
        System.out.print(str);
        return false;
    }

    @Override
    public void callback(glp_tree tree) {
        int reason = GLPK.glp_ios_reason(tree);
        if (reason == GLPKConstants.GLP_IBINGO) {
            System.out.println("Better solution found");
        }
    }
}