예제 #1
0
// lazy constaint callback 
int callback (CPXCENVptr env,
            void *cbdata,
            int wherefrom,
            void *cbhandle,
            int *useraction_p)
    {
        int status;
        CPXLPptr lp = malloc(0);
        status = CPXgetcallbacknodelp (env, cbdata, wherefrom, &lp);


      fprintf(stdout,"lazy: ");

        // char s[50];
        // sprintf(s,"%f", objval);
        // printf("%s\n", s);

        double x[3];
        CPXgetcallbacknodex (env, cbdata, wherefrom, x, 0, 3-1);

        printf("\n");
        for(int i = 0; i < 3; i++)
        {
            char xs[10];

            sprintf(xs,"%f, ", x[i]);
            printf("%s", xs);
        }
        printf("\n");

        // if(x[0] > 20)
        // {
        //     x[0]--;
        //     *isfeas_p = 0;
        // }


        int cmatbeg[1] = {0};
        int cmatind[3] = {0,1,2};
        double cmatval[3] = {0,1,0};
        char csense[1] = {'L'};
        double crhs[1] = {20};
        // CPXaddusercuts doesnt work for some f*****g reason

        if(x[2] > 20)
        {
            //*isfeas_p = 0;
            //status = CPXaddlazyconstraints(env, lp, 1, 3, crhs, csense, cmatbeg, cmatind, cmatval, NULL );
            status = CPXcutcallbackadd(env, cbdata, wherefrom, 3, 20, 'L', cmatind, cmatval, CPX_USECUT_FORCE);
        }
        //
        if ( status ) {
          fprintf (stderr, "Some f*****g error, status = %d.\n", status);
        }

        //*useraction_p = CPX_CALLBACK_SET;

        return 0;
    }
예제 #2
0
파일: coin.cpp 프로젝트: kdbanman/browseRDF
	int CPXPUBLIC CPX_CutCallback(CPXCENVptr xenv, void *cbdata,
			int wherefrom, void *cbhandle, int *useraction_p) {
//		cout << "Entering CPX Callback\n" << flush;
		CPXLPptr nodelp;
		CPXgetcallbacknodelp(xenv, cbdata, wherefrom, &nodelp);

		CoinCallbacks* ccc = (CoinCallbacks*)cbhandle;
		
		int length = CPXgetnumcols(xenv,nodelp) - 1; //hey, don't ask me! some VERY WIERD PHENOMENON... crap
		double objVal;
		double* solution = new double[length];
		CPXgetcallbacknodeobjval(xenv, cbdata, wherefrom, &objVal);
		CPXgetcallbacknodex(xenv, cbdata, wherefrom, solution, 0, length-1);

		OsiCuts* cuts = new OsiCuts();		
		CoinCallbacks::CutReturn ret = ccc->cutCallback(objVal, solution, cuts);
		
		if(ret == CoinCallbacks::CR_AddCuts) {
			for(int i = cuts->sizeRowCuts(); i-->0;) {
				const OsiRowCut& c = cuts->rowCut(i);				
				const CoinPackedVector& vec = c.row();

				if(c.globallyValid())
					/* Old Cplex-Versions did NOT have the last parameter (now set to "false").
					 * If you compile agains an older CPLEX version, simple *REMOVE*
					 *   ", false"
					 * from the calls to CPXcutscallbackadd
					 */
					CPXcutcallbackadd(xenv, cbdata, wherefrom,
						vec.getNumElements(), c.rhs(), c.sense(), vec.getIndices(), vec.getElements(), false);  //default to non-purgable cuts
				else
					CPXcutcallbackaddlocal(xenv, cbdata, wherefrom,
						vec.getNumElements(), c.rhs(), c.sense(), vec.getIndices(), vec.getElements());
				cuts->eraseRowCut(i);
			}
			if(cuts->sizeColCuts() > 0) {
				cerr << "ColCuts currently not supported...\n";
				OGDF_THROW_PARAM(LibraryNotSupportedException, lnscFunctionNotImplemented);
			}
		}
		
		*useraction_p = ( ret == CoinCallbacks::CR_Error) ? CPX_CALLBACK_FAIL :
		                ( ret == CoinCallbacks::CR_AddCuts ) ? CPX_CALLBACK_SET : 
		                CPX_CALLBACK_DEFAULT;
		delete cuts;
		delete[] solution;
//		cout << "Leaving CPX Callback\n" << flush;
		return 0; // success
	}