Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This example creates a very simple Special Ordered Set (SOS) model.
- The model consists of 3 continuous variables, no linear constraints,
- and a pair of SOS constraints of type 1. */
- #include <stdlib.h>
- #include <stdio.h>
- #include "gurobi_c.h"
- #define GUR_INITIAL 0
- #define GUR_DONE_ENV 1
- #define GUR_DONE_MODEL 2
- #define GRP_EXIT_NORMAL 0
- #define GRP_EXIT_LIBRARY_ERROR 1
- #define GRP_EXIT_SOLVE_UNFEASABLE 2
- #define GRP_EXIT_ABORT 3
- #define GRB_EXCEPTION_EXIT(env,model,state) \
- do { \
- printf("ERROR: %s\n", GRBgeterrormsg(env)); \
- if (state == GUR_DONE_MODEL) GRBfreemodel(model); /* Free model */ \
- if (state == GUR_DONE_ENV) GRBfreeenv(env); /* Free environment */ \
- exit(GRP_EXIT_LIBRARY_ERROR); \
- } while(0);
- int
- main(int argc,
- char *argv[])
- {
- GRBenv *env = NULL;
- GRBmodel *model = NULL;
- int error = 0;
- double x[3];
- double obj[3];
- double ub[3];
- int sostype[2];
- int sosbeg[2];
- int sosind[4];
- double soswt[4];
- int optimstatus;
- double objval;
- int state = GUR_INITIAL;
- int ret = GRP_EXIT_NORMAL;
- /* Create environment */
- error = GRBloadenv(&env, "sos.log");
- if (error) GRB_EXCEPTION_EXIT(env,model,state)
- state = GUR_DONE_ENV;
- /* Create an empty model */
- error = GRBnewmodel(env, &model, "sos", 0, NULL, NULL, NULL, NULL, NULL);
- if (error) EXCEPTION_EXIT(env,model,state);
- state = GUR_DONE_MODEL;
- /* Add variables */
- obj[0] = -2; obj[1] = -1; obj[2] = -1;
- ub[0] = 1.0; ub[1] = 1.0; ub[2] = 2.0;
- error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, obj, NULL, ub, NULL, NULL);
- if (error) EXCEPTION_EXIT(env,model,state);
- /* Build first SOS1: x0=0 or x1=0 */
- sosind[0] = 0; sosind[1] = 1;
- soswt[0] = 1.0; soswt[1] = 2.0;
- sosbeg[0] = 0; sostype[0] = GRB_SOS_TYPE1;
- /* Build second SOS1: x0=0 or x2=0 */
- sosind[2] = 0; sosind[3] = 2;
- soswt[2] = 1.0; soswt[3] = 2.0;
- sosbeg[1] = 2; sostype[1] = GRB_SOS_TYPE1;
- /* Add SOSs to model */
- error = GRBaddsos(model, 2, 4, sostype, sosbeg, sosind, soswt);
- if (error) EXCEPTION_EXIT(env,model,state);
- /* Optimize model */
- error = GRBoptimize(model);
- if (error) EXCEPTION_EXIT(env,model,state);
- /* Write model to 'sos.lp' */
- error = GRBwrite(model, "sos.lp");
- if (error) EXCEPTION_EXIT(env,model,state);
- /* Capture solution information */
- error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
- if (error) EXCEPTION_EXIT(env,model,state);
- error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
- if (error) EXCEPTION_EXIT(env,model,state);
- error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, x);
- if (error) EXCEPTION_EXIT(env,model,state);
- printf("\nOptimization complete\n");
- if (optimstatus == GRB_OPTIMAL) {
- printf("Optimal objective: %.4e\n", objval);
- printf(" x=%.4f, y=%.4f, z=%.4f\n", x[0], x[1], x[2]);
- } else if (optimstatus == GRB_INF_OR_UNBD) {
- printf("Model is infeasible or unbounded\n");
- ret = GRP_EXIT_SOLVE_UNFEASABLE;
- } else {
- printf("Optimization was stopped early\n");
- ret = GRP_EXIT_ABORT;
- }
- GRBfreemodel(model); /* Free model */
- GRBfreeenv(env); /* Free environment */
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement