Last active
          August 29, 2015 14:00 
        
      - 
      
 - 
        
Save wisaruthk/2637d44cf94ea3e66666 to your computer and use it in GitHub Desktop.  
    GLPK Example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /* mysample.c */ | |
| /* gcc -I../src -o mysample mysample.c ../src/*.o */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <glpk.h> | |
| /* | |
| minimize | |
| z = 14.7x1 + 10.7x2 + 7.2x4 | |
| subject to | |
| n0 = x1 + x2 + x4 | |
| n2 = 3450x1 + 3300x2 + 3250x4 | |
| n3 = 7.6x1 + 7.29x2 + 2.16x4 | |
| n6 = 1.15x1 + 3.43x2 + 0.33x4 | |
| and bounds of variables | |
| 2950 <= n2 < +inf 0.2 <= x1 < +inf | |
| 6 <= n3 <= 21 0 <= x2 <= 0.30 | |
| 0.5 <= n6 <= 7 0 <= x4 < +inf | |
| n0=1 | |
| */ | |
| int main(void) | |
| { glp_prob *lp; | |
| int ia[1+1000], ja[1+1000]; | |
| double ar[1+1000], z, x1, x2, x3; | |
| lp = glp_create_prob(); | |
| glp_set_prob_name(lp, "sample"); | |
| glp_set_obj_dir(lp, GLP_MIN); | |
| // Constraint Nutrient Spec | |
| glp_add_rows(lp, 4); | |
| glp_set_row_name(lp, 1,"n0"); | |
| glp_set_row_bnds(lp, 1, GLP_FX, 1,1); | |
| glp_set_row_name(lp, 2, "n2"); | |
| glp_set_row_bnds(lp, 2, GLP_LO, 2950.0, 0.0); | |
| glp_set_row_name(lp, 3, "n3"); | |
| glp_set_row_bnds(lp, 3, GLP_DB, 6.0, 21.0); | |
| glp_set_row_name(lp, 4, "n6"); | |
| glp_set_row_bnds(lp, 4, GLP_DB, 0.5, 7.0); | |
| // Constraint Weight of Ingredient Spec | |
| glp_add_cols(lp, 3); | |
| glp_set_col_name(lp, 1, "x1"); | |
| glp_set_col_bnds(lp, 1, GLP_LO, 0.2, 0.0); | |
| glp_set_obj_coef(lp, 1, 14.7); | |
| glp_set_col_name(lp, 2, "x2"); | |
| glp_set_col_bnds(lp, 2, GLP_DB, 0.0, 0.30); | |
| glp_set_obj_coef(lp, 2, 10.7); | |
| glp_set_col_name(lp, 3, "x4"); | |
| glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0); | |
| glp_set_obj_coef(lp, 3, 7.2); | |
| // Fill Value of nutrient for each ingredient | |
| ia[1] = 1, ja[1] = 1, ar[1] = 1.0; /* a[1,1] = 1 */ | |
| ia[2] = 1, ja[2] = 2, ar[2] = 1.0; /* a[1,2] = 1 */ | |
| ia[3] = 1, ja[3] = 3, ar[3] = 1.0; /* a[1,3] = 1 */ | |
| ia[4] = 2, ja[4] = 1, ar[4] = 3450.0; /* a[2,1] = 3450.0 */ | |
| ia[5] = 2, ja[5] = 2, ar[5] = 3300.0; /* a[2,2] = 3300.0 */ | |
| ia[6] = 2, ja[6] = 3, ar[6] = 3250.0; /* a[2,3] = 3250.0 */ | |
| ia[7] = 3, ja[7] = 1, ar[7] = 7.6; /* a[3,1] = 7.6 */ | |
| ia[8] = 3, ja[8] = 2, ar[8] = 7.29; /* a[3,2] = 7.29 */ | |
| ia[9] = 3, ja[9] = 3, ar[9] = 2.16; /* a[3,3] = 2.16 */ | |
| ia[10] = 4, ja[10] = 1, ar[10] = 1.15; /* a[4,1] = 1.15 */ | |
| ia[11] = 4, ja[11] = 2, ar[11] = 3.43; /* a[4,2] = 3.43 */ | |
| ia[12] = 4, ja[12] = 3, ar[12] = 0.33; /* a[4,3] = 0.33 */ | |
| glp_load_matrix(lp, 12, ia, ja, ar); | |
| glp_simplex(lp, NULL); | |
| z = glp_get_obj_val(lp); | |
| x1 = glp_get_col_prim(lp, 1); | |
| x2 = glp_get_col_prim(lp, 2); | |
| x3 = glp_get_col_prim(lp, 3); | |
| printf("\nz = %g; x1 = %g; x2 = %g; x3 = %g\n", | |
| z, x1, x2, x3); | |
| glp_delete_prob(lp); | |
| return 0; | |
| } | |
| /* eof */ | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment