source: stable/BSPsplit/Clp/examples/driverC.c @ 1465

Last change on this file since 1465 was 1465, checked in by stefan, 10 years ago

use new pkg-config based macros to search for required and optional packages

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