source: trunk/Cbc/examples/inc.cpp @ 2496

Last change on this file since 2496 was 2469, checked in by unxusr, 7 months ago

formatting

File size: 3.1 KB
Line 
1// inc.cpp: example of event handler to save
2// every incumbent solution to a new file
3
4#include <cstdio>
5#include <cstdlib>
6#include <OsiCbcSolverInterface.hpp>
7#include <CbcModel.hpp>
8#include <CglPreProcess.hpp>
9#include <CbcSolver.hpp>
10#include <Cbc_C_Interface.h>
11#include "CbcEventHandler.hpp"
12
13static int callBack(CbcModel *model, int whereFrom)
14{
15  return 0;
16}
17
18class SolHandler : public CbcEventHandler {
19public:
20  virtual CbcAction event(CbcEvent whichEvent);
21  SolHandler();
22  SolHandler(CbcModel *model);
23  virtual ~SolHandler();
24  SolHandler(const SolHandler &rhs);
25  SolHandler &operator=(const SolHandler &rhs);
26  virtual CbcEventHandler *clone() const;
27
28  double bestCost;
29};
30
31SolHandler::SolHandler()
32  : CbcEventHandler()
33  , bestCost(COIN_DBL_MAX)
34{
35}
36
37SolHandler::SolHandler(const SolHandler &rhs)
38  : CbcEventHandler(rhs)
39  , bestCost(rhs.bestCost)
40{
41}
42
43SolHandler::SolHandler(CbcModel *model)
44  : CbcEventHandler(model)
45  , bestCost(COIN_DBL_MAX)
46{
47}
48
49SolHandler::~SolHandler()
50{
51}
52
53SolHandler &SolHandler::operator=(const SolHandler &rhs)
54{
55  if (this != &rhs) {
56    CbcEventHandler::operator=(rhs);
57    this->bestCost = rhs.bestCost;
58  }
59  return *this;
60}
61
62CbcEventHandler *SolHandler::clone() const
63{
64  return new SolHandler(*this);
65}
66
67CbcEventHandler::CbcAction SolHandler::event(CbcEvent whichEvent)
68{
69  // If in sub tree carry on
70  if ((model_->specialOptions() & 2048) == 0) {
71    if ((whichEvent == solution || whichEvent == heuristicSolution)) {
72      OsiSolverInterface *origSolver = model_->solver();
73      const OsiSolverInterface *pps = model_->postProcessedSolver(1);
74
75      const OsiSolverInterface *solver = pps ? pps : origSolver;
76
77      if (bestCost > solver->getObjValue() + 1e-6) {
78        bestCost = solver->getObjValue();
79        char outFName[256] = "", strCost[256] = "";
80        sprintf(strCost, "%.12e", bestCost);
81        char *s = strstr(strCost, ".");
82        if (s)
83          *s = 'p';
84        sprintf(outFName, "inc%s.sol", strCost);
85        printf("** solution improved to %g, saving it to file %s ** \n", solver->getObjValue(), outFName);
86
87        // saving .sol
88        FILE *f = fopen(outFName, "w");
89        assert(f);
90        fprintf(f, "Stopped on iterations - objective value %.8f\n", solver->getObjValue());
91        const double *x = solver->getColSolution();
92        const double *obj = solver->getObjCoefficients();
93        for (int i = 0; (i < solver->getNumCols()); ++i) {
94          if (fabs(x[i]) < 1e-6)
95            continue;
96          fprintf(f, "%-7d %22s %22g %g\n", i,
97            solver->getColName(i).c_str(), x[i], obj[i]);
98        }
99        fclose(f);
100      } // improved cost
101    } // solution found
102  } // not in subtree
103
104  return noAction;
105}
106
107int main(int argc, char **argv)
108{
109  if (argc < 2) {
110    fprintf(stderr, "enter instance name\n");
111    exit(EXIT_FAILURE);
112  }
113
114  CbcSolverUsefulData cbcData;
115  cbcData.noPrinting_ = false;
116  OsiClpSolverInterface lp;
117
118  lp.readMps(argv[1]);
119
120  CbcModel model(lp);
121
122  SolHandler sh;
123  model.passInEventHandler(&sh);
124
125  CbcMain0(model, cbcData);
126  CbcMain1(argc - 1, (const char **)(argv + 1), model, callBack, cbcData);
127
128  return 0;
129}
Note: See TracBrowser for help on using the repository browser.