File: ErrorDemo.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 (146 lines) | stat: -rw-r--r-- 5,002 bytes parent folder | download | duplicates (4)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

import org.gnu.glpk.GLPK;
import org.gnu.glpk.GLPKConstants;
import org.gnu.glpk.GlpkCallback;
import org.gnu.glpk.GlpkCallbackListener;
import org.gnu.glpk.GlpkException;
import org.gnu.glpk.SWIGTYPE_p_double;
import org.gnu.glpk.SWIGTYPE_p_int;
import org.gnu.glpk.glp_iocp;
import org.gnu.glpk.glp_prob;
import org.gnu.glpk.glp_tree;

/**
 * This example file demonstrates how to safely treat errors when calling the
 * glpk library, if the error occurs in the callback function.
 *
 * It creates a problem and tries to add -1 row in the callback function. This
 * will cause an error to occur.
 */
public class ErrorDemo implements GlpkCallbackListener {

    static boolean forceError = true;

    public void callback(glp_tree tree) {
        glp_prob prob;
        if (GLPK.glp_ios_reason(tree) == GLPKConstants.GLP_IROWGEN) {
            prob = GLPK.glp_ios_get_prob(tree);
            if (forceError) {
                GLPK.glp_java_set_msg_lvl(GLPKConstants.GLP_JAVA_MSG_LVL_ALL);
                try {
                    GLPK.glp_add_rows(prob, -1);
                } catch (GlpkException ex) {
                    System.out.println("Error in callback: " + ex.getMessage());
                }
                GLPK.glp_java_set_msg_lvl(GLPKConstants.GLP_JAVA_MSG_LVL_OFF);
            }
        }
    }

    /**
     * This is the main function.
     */
    public static void main(String[] args) {
        ErrorDemo d = new ErrorDemo();
        System.out.println("GLPK version: " + GLPK.glp_version());

        GlpkCallback.addListener(d);

        for (int i = 1; i < 5; i++) {
            forceError = !forceError;
            System.out.print("\nIteration " + i);
            if (forceError) {
                System.out.println(", error expected to occur.");
            } else {
                System.out.println(", success expected.");
            }
            if (d.run()) {
                System.out.println("An error has occured.");
                if (!forceError) {
                    System.exit(1);
                }
            } else {
                System.out.println("Successful execution.");
                if (forceError) {
                    System.exit(1);
                }
            }
        }
    }

    /**
     * Build a model with one column
     *
     * @return error error occurred
     */
    private boolean run() {
        glp_prob lp;
        glp_iocp iocp;
        SWIGTYPE_p_int ind;
        SWIGTYPE_p_double val;
        boolean ret = false;

        try {
            //  Create problem
            lp = GLPK.glp_create_prob();
            System.out.println("Problem created");
            GLPK.glp_set_prob_name(lp, "myProblem");

            //  Define columns
            GLPK.glp_add_cols(lp, 2);
            GLPK.glp_set_col_name(lp, 1, "x1");
            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, "x2");
            GLPK.glp_set_col_kind(lp, 2, GLPKConstants.GLP_IV);
            GLPK.glp_set_col_bnds(lp, 2, GLPKConstants.GLP_LO, 0, 0);

            //  Create constraints
            GLPK.glp_add_rows(lp, 2);
            GLPK.glp_set_row_name(lp, 1, "c1");
            GLPK.glp_set_row_bnds(lp, 1, GLPKConstants.GLP_UP, 0, 40);
            ind = GLPK.new_intArray(3);
            GLPK.intArray_setitem(ind, 1, 1);
            GLPK.intArray_setitem(ind, 2, 2);
            val = GLPK.new_doubleArray(3);
            GLPK.doubleArray_setitem(val, 1, 10);
            GLPK.doubleArray_setitem(val, 2, 7);
            GLPK.glp_set_mat_row(lp, 1, 2, ind, val);
            GLPK.delete_intArray(ind);
            GLPK.delete_doubleArray(val);

            ind = GLPK.new_intArray(3);
            GLPK.intArray_setitem(ind, 1, 1);
            GLPK.intArray_setitem(ind, 2, 2);
            val = GLPK.new_doubleArray(3);
            GLPK.glp_set_row_name(lp, 2, "c2");
            GLPK.glp_set_row_bnds(lp, 2, GLPKConstants.GLP_UP, 0, 5);
            GLPK.doubleArray_setitem(val, 1, 1);
            GLPK.doubleArray_setitem(val, 2, 1);
            GLPK.glp_set_mat_row(lp, 2, 2, ind, val);
            GLPK.delete_intArray(ind);
            GLPK.delete_doubleArray(val);

            //  Define objective
            GLPK.glp_set_obj_name(lp, "obj");
            GLPK.glp_set_obj_dir(lp, GLPKConstants.GLP_MAX);
            GLPK.glp_set_obj_coef(lp, 0, 0);
            GLPK.glp_set_obj_coef(lp, 1, 17);
            GLPK.glp_set_obj_coef(lp, 2, 12);

            //  solve model
            iocp = new glp_iocp();
            GLPK.glp_init_iocp(iocp);
            iocp.setPresolve(GLPKConstants.GLP_ON);
            iocp.setMsg_lev(GLPKConstants.GLP_MSG_OFF);
            GLPK.glp_intopt(lp, iocp);

            // free memory
            GLPK.glp_delete_prob(lp);
        } catch (GlpkException ex) {
            System.out.println(ex.getMessage());
            ret = true;
        }
        return ret;
    }
}