import java.sql.SQLException; import org.gnu.glpk.GLPK; import org.gnu.glpk.GLPKConstants; import org.gnu.glpk.SWIGTYPE_p_double; import org.gnu.glpk.SWIGTYPE_p_int; import org.gnu.glpk.glp_prob; import org.gnu.glpk.glp_smcp; public class DbTest { /* X1 X2 X3 X4 X5 Price 90 110 200 150 120 Fe % 51,481 54,954 66,788 62,369 55,571 S % 0,42 0,032 0,004 0,002 0,214 I have 5 different iron ore material. Price, Fe%,S% are given above. I'd like to create 40.000 kg composition using materials. I'd like to create the best composition with minumum price. Constraints are; 1) X1+X2+X3+X4+X5=40000 Composition amount 2) Fe % must be upper than 55% (40000*55/100=22000 kg) It means, I will have 22000 kg Fe amount in the composition X1*0,51481+X2*0,54954+X3*0,66788+X4*0,62369+X5*0,55571>=22000 3) S % must be lower than 0,03% (40000*0,03/100=12 kg) It means, I will have 12 kg Fe amount in the composition X1*0,0042+X2*0,00032+X3*0,00004+X4*0,00002+X5*0,00214<=12 Z(min)= X1*90+X2*110+X3*200+X4*150+X5*120 */ public static void main(String[] args) throws SQLException { glp_prob lp; glp_smcp parm; SWIGTYPE_p_int ind; SWIGTYPE_p_double val; int ret; double compositionTotal=40000; //There are 5 items and we would like to create 40000 kg composition // Create problem lp = GLPK.glp_create_prob(); System.out.println("Problem created"); GLPK.glp_set_prob_name(lp, "Composition Calculation"); try { //Define columns count GLPK.glp_add_cols(lp, 5); 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); GLPK.glp_set_col_name(lp, 3, "x3"); GLPK.glp_set_col_kind(lp, 3, GLPKConstants.GLP_IV); GLPK.glp_set_col_bnds(lp, 3, GLPKConstants.GLP_LO, 0, 0); GLPK.glp_set_col_name(lp, 4, "x4"); GLPK.glp_set_col_kind(lp, 4, GLPKConstants.GLP_IV); GLPK.glp_set_col_bnds(lp, 4, GLPKConstants.GLP_LO, 0, 0); GLPK.glp_set_col_name(lp, 5, "x5"); GLPK.glp_set_col_kind(lp, 5, GLPKConstants.GLP_IV); GLPK.glp_set_col_bnds(lp, 5, GLPKConstants.GLP_LO, 0, 0); // Create constraints GLPK.glp_add_rows(lp, 3); //Toplam harman miktarý GLPK.glp_set_row_name(lp, 1, "c1"); GLPK.glp_set_row_bnds(lp, 1, GLPKConstants.GLP_FX, compositionTotal, compositionTotal); ind = GLPK.new_intArray(3); GLPK.intArray_setitem(ind, 1, 1); GLPK.intArray_setitem(ind, 2, 2); GLPK.intArray_setitem(ind, 3, 3); val = GLPK.new_doubleArray(6); GLPK.doubleArray_setitem(val, 1, 1); GLPK.doubleArray_setitem(val, 2, 1); GLPK.doubleArray_setitem(val, 3, 1); GLPK.doubleArray_setitem(val, 4, 1); GLPK.doubleArray_setitem(val, 5, 1); GLPK.glp_set_mat_row(lp, 1, 2, ind, val); //Total Fe amount int fePercentage=55; double feAmount=compositionTotal*fePercentage/100; System.out.println("Desired FE amount in composition(kg):"+fePercentage); GLPK.glp_set_row_name(lp, 2, "c2"); GLPK.glp_set_row_bnds(lp, 2, GLPKConstants.GLP_LO, feAmount, 0); ind = GLPK.new_intArray(3); GLPK.intArray_setitem(ind, 1, 1); GLPK.intArray_setitem(ind, 2, 2); GLPK.intArray_setitem(ind, 3, 3); val = GLPK.new_doubleArray(6); GLPK.doubleArray_setitem(val, 1, 0.51481); GLPK.doubleArray_setitem(val, 2, 0.54954); GLPK.doubleArray_setitem(val, 3, 0.66788); GLPK.doubleArray_setitem(val, 4, 0.62369); GLPK.doubleArray_setitem(val, 5, 0.55571); GLPK.glp_set_mat_row(lp, 2, 2, ind, val); //Total S amount float SPercentage=(float) 0.03; double SAmount=Math.round(compositionTotal*SPercentage/100); System.out.println("Desired S amount in composition(kg):"+SAmount); GLPK.glp_set_row_name(lp, 3, "c3"); GLPK.glp_set_row_bnds(lp, 3, GLPKConstants.GLP_UP, 0, SAmount); ind = GLPK.new_intArray(3); GLPK.intArray_setitem(ind, 1, 1); GLPK.intArray_setitem(ind, 2, 2); GLPK.intArray_setitem(ind, 3, 3); GLPK.intArray_setitem(ind, 4, 4); GLPK.intArray_setitem(ind, 5, 5); val = GLPK.new_doubleArray(6); GLPK.doubleArray_setitem(val, 1, 0.0042); GLPK.doubleArray_setitem(val, 2, 0.00032); GLPK.doubleArray_setitem(val, 3, 0.00004); GLPK.doubleArray_setitem(val, 4, 0.00002); GLPK.doubleArray_setitem(val, 5, 0.00214); GLPK.glp_set_mat_row(lp, 3, 2, ind, val); //Define objective GLPK.glp_set_obj_name(lp, "obj"); GLPK.glp_set_obj_dir(lp, GLPKConstants.GLP_MIN); GLPK.glp_set_obj_coef(lp, 0, 0); GLPK.glp_set_obj_coef(lp, 1, 90); GLPK.glp_set_obj_coef(lp, 2, 110); GLPK.glp_set_obj_coef(lp, 3, 200); GLPK.glp_set_obj_coef(lp, 4, 150); GLPK.glp_set_obj_coef(lp, 5, 120); // Solve model parm = new glp_smcp(); GLPK.glp_init_smcp(parm); ret = GLPK.glp_simplex(lp, parm); // Retrieve solution if (ret == 0) { write_mip_solution(lp); } else { System.out.println("The problem could not be solved"); }; // free memory GLPK.glp_delete_prob(lp); }catch (Exception e) { e.printStackTrace(); } /*finally { con.close(); } */ } static void write_mip_solution(glp_prob lp) { int i; int n; String name; double val; name = GLPK.glp_get_obj_name(lp); val = GLPK.glp_get_obj_val(lp); System.out.print(name); System.out.print(" = "); System.out.println(val); n = GLPK.glp_get_num_cols(lp); for (i = 1; i <= n; i++) { name = GLPK.glp_get_col_name(lp, i); val = GLPK.glp_get_col_prim(lp, i); System.out.print(name); System.out.print(" = "); System.out.println(val); } } } /* Problem created Desired FE amount in composition(kg):55 Desired S amount in composition(kg):12.0 GLPK Simplex Optimizer, v4.47 3 rows, 5 columns, 6 non-zeros 0: obj = 0.000000000e+000 infeas = 6.200e+004 (1) 1: obj = 4.125000000e+006 infeas = 3.892e+003 (1) PROBLEM HAS NO FEASIBLE SOLUTION obj = 4124999.999999999 x1 = 0.0 x2 = 37499.99999999999 x3 = 0.0 x4 = 0.0 x5 = 0.0 Question: Why am not getting a solution? I have tried to solve the same problem in Excel and I get following result X1 X2 X3 X4 X5 0 37333 0 2667 0 */