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

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

example for solution callback

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