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

Last change on this file since 2384 was 2384, checked in by unxusr, 12 months ago

example for event handler

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