source: trunk/Samples/driverC.c @ 272

Last change on this file since 272 was 272, checked in by ladanyi, 16 years ago

Makefiles cleaned up

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