Advertisement
aircampro

gurobi example in c

Jun 25th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | None | 0 0
  1. /* This example creates a very simple Special Ordered Set (SOS) model.
  2.    The model consists of 3 continuous variables, no linear constraints,
  3.    and a pair of SOS constraints of type 1. */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include "gurobi_c.h"
  8.  
  9. #define GUR_INITIAL 0
  10. #define GUR_DONE_ENV 1
  11. #define GUR_DONE_MODEL 2
  12.  
  13. #define GRP_EXIT_NORMAL 0
  14. #define GRP_EXIT_LIBRARY_ERROR 1
  15. #define GRP_EXIT_SOLVE_UNFEASABLE 2
  16. #define GRP_EXIT_ABORT 3
  17.  
  18. #define GRB_EXCEPTION_EXIT(env,model,state)                                               \
  19. do {                                                                                      \
  20.     printf("ERROR: %s\n", GRBgeterrormsg(env));                                           \
  21.     if (state == GUR_DONE_MODEL)  GRBfreemodel(model);   /* Free model */                 \
  22.     if (state == GUR_DONE_ENV)  GRBfreeenv(env);        /* Free environment */            \
  23.     exit(GRP_EXIT_LIBRARY_ERROR);                                                         \
  24. } while(0);                                                                              
  25.  
  26. int
  27. main(int   argc,
  28.      char *argv[])
  29. {
  30.   GRBenv   *env   = NULL;
  31.   GRBmodel *model = NULL;
  32.   int       error = 0;
  33.   double    x[3];
  34.   double    obj[3];
  35.   double    ub[3];
  36.   int       sostype[2];
  37.   int       sosbeg[2];
  38.   int       sosind[4];
  39.   double    soswt[4];
  40.   int       optimstatus;
  41.   double    objval;
  42.   int state = GUR_INITIAL;
  43.   int ret = GRP_EXIT_NORMAL;
  44.  
  45.   /* Create environment */
  46.  
  47.   error = GRBloadenv(&env, "sos.log");
  48.   if (error) GRB_EXCEPTION_EXIT(env,model,state)
  49.   state = GUR_DONE_ENV;
  50.  
  51.   /* Create an empty model */
  52.  
  53.   error = GRBnewmodel(env, &model, "sos", 0, NULL, NULL, NULL, NULL, NULL);
  54.   if (error) EXCEPTION_EXIT(env,model,state);
  55.   state = GUR_DONE_MODEL;
  56.  
  57.   /* Add variables */
  58.  
  59.   obj[0] = -2; obj[1] = -1; obj[2] = -1;
  60.   ub[0] = 1.0; ub[1] = 1.0; ub[2] = 2.0;
  61.   error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, obj, NULL, ub, NULL, NULL);
  62.   if (error) EXCEPTION_EXIT(env,model,state);
  63.  
  64.   /* Build first SOS1: x0=0 or x1=0 */
  65.  
  66.   sosind[0] = 0; sosind[1] = 1;
  67.   soswt[0] = 1.0; soswt[1] = 2.0;
  68.   sosbeg[0] = 0; sostype[0] = GRB_SOS_TYPE1;
  69.  
  70.   /* Build second SOS1: x0=0 or x2=0 */
  71.  
  72.   sosind[2] = 0; sosind[3] = 2;
  73.   soswt[2] = 1.0; soswt[3] = 2.0;
  74.   sosbeg[1] = 2; sostype[1] = GRB_SOS_TYPE1;
  75.  
  76.   /* Add SOSs to model */
  77.  
  78.   error = GRBaddsos(model, 2, 4, sostype, sosbeg, sosind, soswt);
  79.   if (error) EXCEPTION_EXIT(env,model,state);
  80.  
  81.   /* Optimize model */
  82.  
  83.   error = GRBoptimize(model);
  84.   if (error) EXCEPTION_EXIT(env,model,state);
  85.  
  86.   /* Write model to 'sos.lp' */
  87.  
  88.   error = GRBwrite(model, "sos.lp");
  89.   if (error) EXCEPTION_EXIT(env,model,state);
  90.  
  91.   /* Capture solution information */
  92.  
  93.   error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
  94.   if (error) EXCEPTION_EXIT(env,model,state);
  95.  
  96.   error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
  97.   if (error) EXCEPTION_EXIT(env,model,state);
  98.  
  99.   error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, x);
  100.   if (error) EXCEPTION_EXIT(env,model,state);
  101.  
  102.   printf("\nOptimization complete\n");
  103.   if (optimstatus == GRB_OPTIMAL) {
  104.     printf("Optimal objective: %.4e\n", objval);
  105.  
  106.     printf("  x=%.4f, y=%.4f, z=%.4f\n", x[0], x[1], x[2]);
  107.   } else if (optimstatus == GRB_INF_OR_UNBD) {
  108.     printf("Model is infeasible or unbounded\n");
  109.     ret = GRP_EXIT_SOLVE_UNFEASABLE;
  110.   } else {
  111.     printf("Optimization was stopped early\n");
  112.     ret = GRP_EXIT_ABORT;
  113.   }
  114.  
  115.   GRBfreemodel(model);   /* Free model */                
  116.   GRBfreeenv(env);       /* Free environment */            
  117.  
  118.   return ret;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement