annotate R/rdyncall/demo/glpk.R @ 37:8c8f848131c6

- version bump - better doc - made calling convention mode reset by default, as only way to specify convention used is via signature string
author Tassilo Philipp
date Mon, 13 Apr 2020 20:08:54 +0200
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 # Example from GLPK Reference Manual.. rewritten to R.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 dynport(glpk)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 lp = glp_create_prob();
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 glp_set_prob_name(lp, "sample");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 glp_set_obj_dir(lp, GLP_MAX);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 glp_add_rows(lp, 3);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 glp_set_row_name(lp, 1, "p");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 100.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 glp_set_row_name(lp, 2, "q");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 600.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 glp_set_row_name(lp, 3, "r");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 glp_set_row_bnds(lp, 3, GLP_UP, 0.0, 300.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 glp_add_cols(lp, 3);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 glp_set_col_name(lp, 1, "x1");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 glp_set_obj_coef(lp, 1, 10.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 glp_set_col_name(lp, 2, "x2");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 glp_set_obj_coef(lp, 2, 6.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 glp_set_col_name(lp, 3, "x3");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 glp_set_obj_coef(lp, 3, 4.0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 ia = integer(1+1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 ja = integer(1+1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 ar = double(1+1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 #we index at 1 in C (second position).. but in R we start at initial position..
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 ia[1]=1;ja[1]=1;ar[1]= 1.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 ia[2]=1;ja[2]=2;ar[2]= 1.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 ia[3]=1;ja[3]=3;ar[3]= 1.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 ia[4]=2;ja[4]=1;ar[4]= 10.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 ia[5]=3;ja[5]=1;ar[5]= 2.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 ia[6]=2;ja[6]=2;ar[6]= 4.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 ia[7]=3;ja[7]=2;ar[7]= 2.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 ia[8]=2;ja[8]=3;ar[8]= 5.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 ia[9]=3;ja[9]=3;ar[9]= 6.0;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 #now we prefix with '0' .. so that our values the above start at 1 in C.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 ia <- as.integer(c(0,ia))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 ja <- as.integer(c(0,ja))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 glp_load_matrix(lp, 9, ia, ja, ar);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 glp_simplex(lp, NULL);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 z = glp_get_obj_val(lp);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 x1 = glp_get_col_prim(lp, 1);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 x2 = glp_get_col_prim(lp, 2);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 x3 = glp_get_col_prim(lp, 3);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 print(list(z=z,x1=x1,x2=x2,x3=x3))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 glp_delete_prob(lp);