1 /* $Id: driverC.c 1662 2011-01-04 17:52:40Z lou $ */
2 /*
3 Copyright (C) 2003 International Business Machines
4 Corporation and others. All Rights Reserved.
5
6 This code is licensed under the terms of the Eclipse Public License (EPL).
7 */
8
9 /* This example shows the use of the "C" interface */
10
11 #include "Clp_C_Interface.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <assert.h>
16
17 /* Call back function - just says whenever it gets Clp0005 or Coin0002 */
callBack(Clp_Simplex * model,int messageNumber,int nDouble,const double * vDouble,int nInt,const int * vInt,int nString,char ** vString)18 static void callBack(Clp_Simplex * model, int messageNumber,
19 int nDouble, const double * vDouble,
20 int nInt, const int * vInt,
21 int nString, char ** vString)
22 {
23 if (messageNumber == 1000002) {
24 /* Coin0002 */
25 assert(nString == 1 && nInt == 3);
26 printf("Name of problem is %s\n", vString[0]);
27 printf("row %d col %d el %d\n", vInt[0], vInt[1], vInt[2]);
28 } else if (messageNumber == 5) {
29 /* Clp0005 */
30 int i;
31 assert(nInt == 4 && nDouble == 3); /* they may not all print */
32 for (i = 0; i < 3; i++)
33 printf("%d %g\n", vInt[i], vDouble[i]);
34 }
35 }
36
37
38
main(int argc,const char * argv[])39 int main(int argc, const char *argv[])
40 {
41 /* Get default model */
42 Clp_Simplex * model = Clp_newModel();
43 int status;
44 /* register callback */
45 Clp_registerCallBack(model, callBack);
46 /* Keep names when reading an mps file */
47 if (argc < 2) {
48 #if defined(SAMPLEDIR)
49 status = Clp_readMps(model, SAMPLEDIR "/p0033.mps", 1, 0);
50 #else
51 fprintf(stderr, "Do not know where to find sample MPS files.\n");
52 exit(1);
53 #endif
54 } else
55 status = Clp_readMps(model, argv[1], 1, 0);
56
57 if (status) {
58 fprintf(stderr, "Bad readMps %s\n", argv[1]);
59 fprintf(stdout, "Bad readMps %s\n", argv[1]);
60 exit(1);
61 }
62
63 if (argc < 3 || !strstr(argv[2], "primal")) {
64 /* Use the dual algorithm unless user said "primal" */
65 Clp_initialDualSolve(model);
66 } else {
67 Clp_initialPrimalSolve(model);
68 }
69
70 {
71 char modelName[80];
72 Clp_problemName(model, 80, modelName);
73 printf("Model %s has %d rows and %d columns\n",
74 modelName, Clp_numberRows(model), Clp_numberColumns(model));
75 }
76
77 /* remove this to print solution */
78
79 /*exit(0); */
80
81 {
82 /*
83 Now to print out solution. The methods used return modifiable
84 arrays while the alternative names return const pointers -
85 which is of course much more virtuous.
86
87 This version just does non-zero columns
88
89 */
90
91 /* Columns */
92
93 int numberColumns = Clp_numberColumns(model);
94 int iColumn;
95
96
97 /* Alternatively getColSolution(model) */
98 double * columnPrimal = Clp_primalColumnSolution(model);
99 /* Alternatively getReducedCost(model) */
100 double * columnDual = Clp_dualColumnSolution(model);
101 /* Alternatively getColLower(model) */
102 double * columnLower = Clp_columnLower(model);
103 /* Alternatively getColUpper(model) */
104 double * columnUpper = Clp_columnUpper(model);
105 /* Alternatively getObjCoefficients(model) */
106 double * columnObjective = Clp_objective(model);
107
108 printf("--------------------------------------\n");
109 /* If we have not kept names (parameter to readMps) this will be 0 */
110 assert(Clp_lengthNames(model));
111
112 printf(" Primal Dual Lower Upper Cost\n");
113
114 for (iColumn = 0; iColumn < numberColumns; iColumn++) {
115 double value;
116 value = columnPrimal[iColumn];
117 if (value > 1.0e-8 || value < -1.0e-8) {
118 char name[20];
119 Clp_columnName(model, iColumn, name);
120 printf("%6d %8s", iColumn, name);
121 printf(" %13g", columnPrimal[iColumn]);
122 printf(" %13g", columnDual[iColumn]);
123 printf(" %13g", columnLower[iColumn]);
124 printf(" %13g", columnUpper[iColumn]);
125 printf(" %13g", columnObjective[iColumn]);
126 printf("\n");
127 }
128 }
129 printf("--------------------------------------\n");
130 }
131 return 0;
132 }
133