source: trunk/Cbc/examples/modify.cpp

Last change on this file was 2469, checked in by unxusr, 7 weeks ago

formatting

  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1// $Id: modify.cpp 2469 2019-01-06 23:17:46Z unxusr $
2// Copyright (C) 2007, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6/*
7  This example shows the creation of a model from arrays, solution
8  and then changes to objective and adding a row
9*/
10
11#include "CbcModel.hpp"
12
13// Using as solver
14#include "OsiClpSolverInterface.hpp"
15
16int main(int argc, const char *argv[])
17{
18  // model is as exmip1.mps from Data/samples
19  int numberRows = 5;
20  int numberColumns = 8;
21  int numberElements = 14;
22  // matrix data - column ordered
23  CoinBigIndex start[9] = { 0, 2, 4, 6, 8, 10, 11, 12, 14 };
24  int length[8] = { 2, 2, 2, 2, 2, 1, 1, 2 };
25  int rows[14] = { 0, 4, 0, 1, 1, 2, 0, 3, 0, 4, 2, 3, 0, 4 };
26  double elements[14] = { 3, 5.6, 1, 2, 1.1, 1, -2, 2.8, -1, 1, 1, -1.2, -1, 1.9 };
27  CoinPackedMatrix matrix(true, numberRows, numberColumns, numberElements, elements, rows, start, length);
28
29  // rim data
30  double objective[8] = { 1, 0, 0, 0, 2, 0, 0, -1 };
31  double rowLower[5] = { 2.5, -COIN_DBL_MAX, 4, 1.8, 3 };
32  double rowUpper[5] = { COIN_DBL_MAX, 2.1, 4, 5, 15 };
33  double colLower[8] = { 2.5, 0, 0, 0, 0.5, 0, 0, 0 };
34  double colUpper[8] = { COIN_DBL_MAX, 4.1, 1, 1, 4, COIN_DBL_MAX, COIN_DBL_MAX, 4.3 };
35  OsiClpSolverInterface solver1;
36  // load problem
37  solver1.loadProblem(matrix, colLower, colUpper, objective,
38    rowLower, rowUpper);
39  // mark integer
40  solver1.setInteger(2);
41  solver1.setInteger(3);
42
43  // Solve
44  solver1.initialSolve();
45
46  // Pass data and solver to CbcModel
47  CbcModel model(solver1);
48
49  // reduce printout
50  model.setLogLevel(1);
51  model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
52  // Do complete search
53  model.branchAndBound();
54  /* Print solution.  CbcModel clones solver so we
55     need to get current copy */
56
57  const double *solution = model.solver()->getColSolution();
58  int i;
59
60  for (i = 0; i < numberColumns; i++) {
61    double value = solution[i];
62    if (fabs(value) > 1.0e-7 && model.solver()->isInteger(i))
63      printf("i %d has value %g\n", i, value);
64    else if (fabs(value) > 1.0e-7)
65      printf("c %d has value %g\n", i, value);
66  }
67
68  // Change objective
69  solver1.setObjCoeff(0, -100.0);
70
71  // Now model has too much information e.g. best solution
72  // simplest is to start again
73  // Pass data and solver to CbcModel
74  model = CbcModel(solver1);
75
76  // reduce printout
77  model.setLogLevel(1);
78  model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
79  // Do complete search
80  model.branchAndBound();
81
82  solution = model.solver()->getColSolution();
83
84  for (i = 0; i < numberColumns; i++) {
85    double value = solution[i];
86    if (fabs(value) > 1.0e-7 && model.solver()->isInteger(i))
87      printf("i %d has value %g\n", i, value);
88    else if (fabs(value) > 1.0e-7)
89      printf("c %d has value %g\n", i, value);
90  }
91
92  // Add constraint
93  int column[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
94  double element2[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };
95  solver1.addRow(8, column, element2, 7.8, COIN_DBL_MAX);
96
97  // Now model has too much information e.g. best solution
98  // simplest is to start again
99  // Pass data and solver to CbcModel
100  model = CbcModel(solver1);
101
102  // reduce printout
103  model.setLogLevel(1);
104  model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry);
105  // Do complete search
106  model.branchAndBound();
107
108  solution = model.solver()->getColSolution();
109
110  for (i = 0; i < numberColumns; i++) {
111    double value = solution[i];
112    if (fabs(value) > 1.0e-7 && model.solver()->isInteger(i))
113      printf("i %d has value %g\n", i, value);
114    else if (fabs(value) > 1.0e-7)
115      printf("c %d has value %g\n", i, value);
116  }
117  return 0;
118}
Note: See TracBrowser for help on using the repository browser.