Recently do linear programming problem, need to use GLPK tool, VS2013 environment Tinker for a long time finally success. The following step shows the configuration of the environment.
1. Download GLPK4.45 version http://glpk-for-windows.soft112.com/then unzip.
2. Create a new project with VS2013, named Glpk_test, a CPP file in the source file named TESTGLPK.cpp
3. Right-click the project's properties, select General, see "Additional Include directory" on the right, tap it, select the Include folder path under GLPK, and then OK.
4. Find the property under "linker"-input, you can see the first on the right is to add a dependency, edit it, under the Glpk folder find Glpk_4_45.lib, select and OK.
5. Have glpk_4_45.dll file in the same directory as Glpk_4_45.lib and copy it directly to your project directory.
6. After completing the configuration, you can enter the code test, and in the GLPK documentation, find an example, enter the code.
1#include"stdio.h"2#include"glpk.h"3 4 intMain ()5 {6Glp_prob *LP;7 intia[1+ +], ja[1+ +];8 Doublear[1+ +], Z, x1, x2, x3;9S1:LP =Glp_create_prob ();TenS2:GLP_SET_PROB_NAME (LP,"Sample"); One S3:GLP_SET_OBJ_DIR (LP, Glp_max); AS4:GLP_ADD_ROWS (LP,3); -S5:GLP_SET_ROW_NAME (LP,1,"P"); -S6:glp_set_row_bnds (LP,1, Glp_up,0.0,100.0); theS7:GLP_SET_ROW_NAME (LP,2,"Q"); -S8:glp_set_row_bnds (LP,2, Glp_up,0.0,600.0); -S9:GLP_SET_ROW_NAME (LP,3,"R"); -S10:glp_set_row_bnds (LP,3, Glp_up,0.0,300.0); +S11:GLP_ADD_COLS (LP,3); -S12:GLP_SET_COL_NAME (LP,1,"X1"); +S13:glp_set_col_bnds (LP,1, Glp_lo,0.0,0.0); AS14:GLP_SET_OBJ_COEF (LP,1,10.0); atS15:GLP_SET_COL_NAME (LP,2,"X2"); -S16:glp_set_col_bnds (LP,2, Glp_lo,0.0,0.0); -S17:GLP_SET_OBJ_COEF (LP,2,6.0); -S18:GLP_SET_COL_NAME (LP,3,"X3"); -S19:glp_set_col_bnds (LP,3, Glp_lo,0.0,0.0); -S20:GLP_SET_OBJ_COEF (LP,3,4.0); ins21:ia[1] =1, ja[1] =1, ar[1] =1.0;/*a[1,1] = 1*/ -s22:ia[2] =1, ja[2] =2, ar[2] =1.0;/*a[1,2] = 1*/ tos23:ia[3] =1, ja[3] =3, ar[3] =1.0;/*a[1,3] = 1*/ +s24:ia[4] =2, ja[4] =1, ar[4] =10.0;/*a[2,1] = ten*/ -s25:ia[5] =3, ja[5] =1, ar[5] =2.0;/*a[3,1] = 2*/ thes26:ia[6] =2, ja[6] =2, ar[6] =4.0;/*a[2,2] = 4*/ *s27:ia[7] =3, ja[7] =2, ar[7] =2.0;/*a[3,2] = 2*/ $s28:ia[8] =2, ja[8] =3, ar[8] =5.0;/*a[2,3] = 5*/Panax Notoginsengs29:ia[9] =3, ja[9] =3, ar[9] =6.0;/*a[3,3] = 6*/ -S30:GLP_LOAD_MATRIX (LP,9, IA, JA, AR); the S31:glp_simplex (LP, NULL); +S32:z =glp_get_obj_val (LP); AS33:X1 = GLP_GET_COL_PRIM (LP,1); theS34:X2 = GLP_GET_COL_PRIM (LP,2); +S35:X3 = GLP_GET_COL_PRIM (LP,3); -s36:printf ("\nz =%g; x1 =%g; x2 =%g; x3 =%g\n", $ Z, x1, x2, x3); $ S37:GLP_DELETE_PROB (LP); - return 0; -}
Run and you can see the test results.
For more GLPK use and syntax issues, refer to the documentation under the Doc folder.
VS2013 call GLPK to solve linear programming