Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark mode
Gurobi Example Tour
Light LogoDark Logo
Gurobi
Back to top

Genconstr Examples#

This section includes source code for all of the Gurobi genconstr examples.The same source code can be found in theexamples directory of theGurobi distribution.

/* Copyright 2025, Gurobi Optimization, LLC *//* In this example we show the use of general constraints for modeling * some common expressions. We use as an example a SAT-problem where we * want to see if it is possible to satisfy at least four (or all) clauses * of the logical form * * L = (x0 or ~x1 or x2)  and (x1 or ~x2 or x3)  and *     (x2 or ~x3 or x0)  and (x3 or ~x0 or x1)  and *     (~x0 or ~x1 or x2) and (~x1 or ~x2 or x3) and *     (~x2 or ~x3 or x0) and (~x3 or ~x0 or x1) * * We do this by introducing two variables for each literal (itself and its * negated value), one variable for each clause, one variable indicating * whether we can satisfy at least four clauses, and one last variable to * identify the minimum of the clauses (so if it is one, we can satisfy all * clauses). Then we put these last two variables in the objective. * The objective function is therefore * * maximize Obj0 + Obj1 * *  Obj0 = MIN(Clause1, ... , Clause8) *  Obj1 = 1 -> Clause1 + ... + Clause8 >= 4 * * thus, the objective value will be two if and only if we can satisfy all * clauses; one if and only if at least four but not all clauses can be satisfied, * and zero otherwise. */#include<stdlib.h>#include<stdio.h>#include<math.h>#include<string.h>#include"gurobi_c.h"#define MAXSTR    128#define NLITERALS 4#define NCLAUSES  8#define NOBJ      2#define NVARS     (2 * NLITERALS + NCLAUSES + NOBJ)#define LIT(n)    (n)#define NOTLIT(n) (NLITERALS + n)#define CLA(n)    (2 * NLITERALS + n)#define OBJ(n)    (2 * NLITERALS + NCLAUSES + n)intmain(void){GRBenv*env=NULL;GRBmodel*model=NULL;interror=0;intcind[NVARS];doublecval[NVARS];charbuffer[MAXSTR];intcol,i,status;doubleobjval;/* Example data */constintClauses[][3]={{LIT(0),NOTLIT(1),LIT(2)},{LIT(1),NOTLIT(2),LIT(3)},{LIT(2),NOTLIT(3),LIT(0)},{LIT(3),NOTLIT(0),LIT(1)},{NOTLIT(0),NOTLIT(1),LIT(2)},{NOTLIT(1),NOTLIT(2),LIT(3)},{NOTLIT(2),NOTLIT(3),LIT(0)},{NOTLIT(3),NOTLIT(0),LIT(1)}};/* Create environment */error=GRBloadenv(&env,"genconstr_c.log");if(error)gotoQUIT;/* Create initial model */error=GRBnewmodel(env,&model,"genconstr_c",NVARS,NULL,NULL,NULL,NULL,NULL);if(error)gotoQUIT;/* Initialize decision variables and objective */for(i=0;i<NLITERALS;i++){col=LIT(i);sprintf(buffer,"X%d",i);error=GRBsetcharattrelement(model,"VType",col,GRB_BINARY);if(error)gotoQUIT;error=GRBsetstrattrelement(model,"VarName",col,buffer);if(error)gotoQUIT;col=NOTLIT(i);sprintf(buffer,"notX%d",i);error=GRBsetcharattrelement(model,"VType",col,GRB_BINARY);if(error)gotoQUIT;error=GRBsetstrattrelement(model,"VarName",col,buffer);if(error)gotoQUIT;}for(i=0;i<NCLAUSES;i++){col=CLA(i);sprintf(buffer,"Clause%d",i);error=GRBsetcharattrelement(model,"VType",col,GRB_BINARY);if(error)gotoQUIT;error=GRBsetstrattrelement(model,"VarName",col,buffer);if(error)gotoQUIT;}for(i=0;i<NOBJ;i++){col=OBJ(i);sprintf(buffer,"Obj%d",i);error=GRBsetcharattrelement(model,"VType",col,GRB_BINARY);if(error)gotoQUIT;error=GRBsetstrattrelement(model,"VarName",col,buffer);if(error)gotoQUIT;error=GRBsetdblattrelement(model,"Obj",col,1.0);if(error)gotoQUIT;}/* Link Xi and notXi */for(i=0;i<NLITERALS;i++){sprintf(buffer,"CNSTR_X%d",i);cind[0]=LIT(i);cind[1]=NOTLIT(i);cval[0]=cval[1]=1;error=GRBaddconstr(model,2,cind,cval,GRB_EQUAL,1.0,buffer);if(error)gotoQUIT;}/* Link clauses and literals */for(i=0;i<NCLAUSES;i++){sprintf(buffer,"CNSTR_Clause%d",i);error=GRBaddgenconstrOr(model,buffer,CLA(i),3,Clauses[i]);if(error)gotoQUIT;}/* Link objs with clauses */for(i=0;i<NCLAUSES;i++){cind[i]=CLA(i);cval[i]=1;}error=GRBaddgenconstrMin(model,"CNSTR_Obj0",OBJ(0),NCLAUSES,cind,GRB_INFINITY);if(error)gotoQUIT;/* note that passing 4 instead of 4.0 will produce undefined behavior */error=GRBaddgenconstrIndicator(model,"CNSTR_Obj1",OBJ(1),1,NCLAUSES,cind,cval,GRB_GREATER_EQUAL,4.0);if(error)gotoQUIT;/* Set global objective sense */error=GRBsetintattr(model,GRB_INT_ATTR_MODELSENSE,GRB_MAXIMIZE);if(error)gotoQUIT;/* Save problem */error=GRBwrite(model,"genconstr_c.mps");if(error)gotoQUIT;error=GRBwrite(model,"genconstr_c.lp");if(error)gotoQUIT;/* Optimize */error=GRBoptimize(model);if(error)gotoQUIT;/* Status checking */error=GRBgetintattr(model,"Status",&status);if(error)gotoQUIT;if(status==GRB_INF_OR_UNBD||status==GRB_INFEASIBLE||status==GRB_UNBOUNDED){printf("The model cannot be solved ""because it is infeasible or unbounded\n");gotoQUIT;}if(status!=GRB_OPTIMAL){printf("Optimization was stopped with status %i\n",status);gotoQUIT;}/* Print result */error=GRBgetdblattr(model,GRB_DBL_ATTR_OBJVAL,&objval);if(error)gotoQUIT;if(objval>1.9)printf("Logical expression is satisfiable\n");elseif(objval>0.9)printf("At least four clauses can be satisfied\n");elseprintf("At most three clauses may be satisfied\n");QUIT:if(model!=NULL)GRBfreemodel(model);if(env!=NULL)GRBfreeenv(env);returnerror;}

Help and Feedback


[8]
ページ先頭

©2009-2025 Movatter.jp