⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glpapi17.c

📁 著名的大规模线性规划求解器源码GLPK.C语言版本,可以修剪.内有详细帮助文档.
💻 C
📖 第 1 页 / 共 3 页
字号:
         }      }      /*--------------------------------------------------------------*/      /* read problem data from the input file */      if (csa->in_file == NULL)      {  xprintf("No input problem file specified; try %s --help\n",            argv[0]);         ret = EXIT_FAILURE;         goto done;      }      if (csa->format == FMT_MPS_DECK)      {  ret = glp_read_mps(csa->prob, GLP_MPS_DECK, NULL,            csa->in_file);         if (ret != 0)err1:    {  xprintf("MPS file processing error\n");            ret = EXIT_FAILURE;            goto done;         }      }      else if (csa->format == FMT_MPS_FILE)      {  ret = glp_read_mps(csa->prob, GLP_MPS_FILE, NULL,            csa->in_file);         if (ret != 0) goto err1;      }      else if (csa->format == FMT_CPLEX_LP)      {  ret = glp_read_lp(csa->prob, NULL, csa->in_file);         if (ret != 0)         {  xprintf("CPLEX LP file processing error\n");            ret = EXIT_FAILURE;            goto done;         }      }      else if (csa->format == FMT_MATHPROG)      {  int k;         /* allocate the translator workspace */         csa->tran = glp_mpl_alloc_wksp();         /* read model section and optional data section */         if (glp_mpl_read_model(csa->tran, csa->in_file, csa->ndf > 0))err2:    {  xprintf("MathProg model processing error\n");            ret = EXIT_FAILURE;            goto done;         }         /* read optional data section(s), if necessary */         for (k = 1; k <= csa->ndf; k++)         {  if (glp_mpl_read_data(csa->tran, csa->in_data[k]))               goto err2;         }         /* generate the model */         if (glp_mpl_generate(csa->tran, csa->out_dpy)) goto err2;         /* build the problem instance from the model */         glp_mpl_build_prob(csa->tran, csa->prob);      }      else if (csa->format == FMT_MIN_COST)      {  csa->graph = glp_create_graph(sizeof(v_data), sizeof(a_data));         ret = glp_read_mincost(csa->graph, offsetof(v_data, rhs),            offsetof(a_data, low), offsetof(a_data, cap),            offsetof(a_data, cost), csa->in_file);         if (ret != 0)         {  xprintf("DIMACS file processing error\n");            ret = EXIT_FAILURE;            goto done;         }         glp_mincost_lp(csa->prob, csa->graph, GLP_ON,            offsetof(v_data, rhs), offsetof(a_data, low),            offsetof(a_data, cap), offsetof(a_data, cost));         glp_set_prob_name(csa->prob, csa->in_file);      }      else if (csa->format == FMT_MAX_FLOW)      {  int s, t;         csa->graph = glp_create_graph(sizeof(v_data), sizeof(a_data));         ret = glp_read_maxflow(csa->graph, &s, &t,            offsetof(a_data, cap), csa->in_file);         if (ret != 0)         {  xprintf("DIMACS file processing error\n");            ret = EXIT_FAILURE;            goto done;         }         glp_maxflow_lp(csa->prob, csa->graph, GLP_ON, s, t,            offsetof(a_data, cap));         glp_set_prob_name(csa->prob, csa->in_file);      }      else         xassert(csa != csa);      /*--------------------------------------------------------------*/      /* change problem name, if required */      if (csa->new_name != NULL)         glp_set_prob_name(csa->prob, csa->new_name);      /* change optimization direction, if required */      if (csa->dir != 0)         glp_set_obj_dir(csa->prob, csa->dir);      /* order rows and columns of the constraint matrix */      lpx_order_matrix(csa->prob);      /*--------------------------------------------------------------*/      /* write problem data in fixed MPS format, if required */      if (csa->out_mps != NULL)      {  ret = glp_write_mps(csa->prob, GLP_MPS_DECK, NULL,            csa->out_mps);         if (ret != 0)         {  xprintf("Unable to write problem in fixed MPS format\n");            ret = EXIT_FAILURE;            goto done;         }      }      /* write problem data in free MPS format, if required */      if (csa->out_freemps != NULL)      {  ret = glp_write_mps(csa->prob, GLP_MPS_FILE, NULL,            csa->out_freemps);         if (ret != 0)         {  xprintf("Unable to write problem in free MPS format\n");            ret = EXIT_FAILURE;            goto done;         }      }      /* write problem data in CPLEX LP format, if required */      if (csa->out_cpxlp != NULL)      {  ret = glp_write_lp(csa->prob, NULL, csa->out_cpxlp);         if (ret != 0)         {  xprintf("Unable to write problem in CPLEX LP format\n");            ret = EXIT_FAILURE;            goto done;         }      }      /* write problem data in OPB format, if required */      if (csa->out_pb != NULL)      {  ret = lpx_write_pb(csa->prob, csa->out_pb, 0, 0);         if (ret != 0)         {  xprintf("Unable to write problem in OPB format\n");            ret = EXIT_FAILURE;            goto done;         }      }      /* write problem data in normalized OPB format, if required */      if (csa->out_npb != NULL)      {  ret = lpx_write_pb(csa->prob, csa->out_npb, 1, 1);         if (ret != 0)         {  xprintf(               "Unable to write problem in normalized OPB format\n");            ret = EXIT_FAILURE;            goto done;         }      }      /*--------------------------------------------------------------*/      /* if only problem data check is required, skip computations */      if (csa->check)      {  ret = EXIT_SUCCESS;         goto done;      }      /*--------------------------------------------------------------*/      /* determine the solution type */      if (!csa->nomip &&          glp_get_num_int(csa->prob) + glp_get_num_bin(csa->prob) > 0)      {  if (csa->solution == SOL_INTERIOR)         {  xprintf("Interior-point method is not able to solve MIP pro"               "blem; use --simplex\n");            ret = EXIT_FAILURE;            goto done;         }         csa->solution = SOL_INTEGER;      }      /*--------------------------------------------------------------*/      /* if solution is provided, read it and skip computations */      if (csa->in_res != NULL)      {  if (csa->solution == SOL_BASIC)            ret = glp_read_sol(csa->prob, csa->in_res);         else if (csa->solution == SOL_INTERIOR)            ret = glp_read_ipt(csa->prob, csa->in_res);         else if (csa->solution == SOL_INTEGER)            ret = glp_read_mip(csa->prob, csa->in_res);         else            xassert(csa != csa);         if (ret != 0)         {  xprintf("Unable to read problem solution\n");            ret = EXIT_FAILURE;            goto done;         }         goto skip;      }      /*--------------------------------------------------------------*/      /* scale the problem data, if required */      if (csa->scale)      {  if (csa->solution == SOL_BASIC && !csa->smcp.presolve ||             csa->solution == SOL_INTERIOR ||             csa->solution == SOL_INTEGER && !csa->iocp.presolve)            glp_scale_prob(csa->prob, GLP_SF_AUTO);      }      /* construct starting LP basis */      if (csa->solution == SOL_BASIC && !csa->smcp.presolve ||          csa->solution == SOL_INTEGER && !csa->iocp.presolve)      {  if (csa->crash == USE_STD_BASIS)            glp_std_basis(csa->prob);         else if (csa->crash == USE_ADV_BASIS)            glp_adv_basis(csa->prob, 0);         else if (csa->crash == USE_CPX_BASIS)            glp_cpx_basis(csa->prob);         else            xassert(csa != csa);      }      /*--------------------------------------------------------------*/      /* solve the problem */      start = xtime();      if (csa->solution == SOL_BASIC)      {  if (!csa->exact)         {  glp_set_bfcp(csa->prob, &csa->bfcp);            glp_simplex(csa->prob, &csa->smcp);            if (csa->xcheck)            {  if (csa->smcp.presolve &&                   glp_get_status(csa->prob) != GLP_OPT)                  xprintf("If you need to check final basis for non-opt"                     "imal solution, use --nopresol\n");               else                  glp_exact(csa->prob, &csa->smcp);            }            if (csa->out_sol != NULL || csa->out_res != NULL)            {  if (csa->smcp.presolve &&                   glp_get_status(csa->prob) != GLP_OPT)               xprintf("If you need actual output for non-optimal solut"                  "ion, use --nopresol\n");            }         }         else            glp_exact(csa->prob, &csa->smcp);      }      else if (csa->solution == SOL_INTERIOR)         glp_interior(csa->prob, NULL);      else if (csa->solution == SOL_INTEGER)      {  if (!csa->iocp.presolve)         {  glp_set_bfcp(csa->prob, &csa->bfcp);            glp_simplex(csa->prob, &csa->smcp);         }         glp_intopt(csa->prob, &csa->iocp);      }      else         xassert(csa != csa);      /*--------------------------------------------------------------*/      /* display statistics */      xprintf("Time used:   %.1f secs\n", xdifftime(xtime(), start));      {  xlong_t tpeak;         char buf[50];         lib_mem_usage(NULL, NULL, NULL, &tpeak);         xprintf("Memory used: %.1f Mb (%s bytes)\n",            xltod(tpeak) / 1048576.0, xltoa(tpeak, buf));      }      /*--------------------------------------------------------------*/skip: /* postsolve the model, if necessary */      if (csa->tran != NULL)      {  if (csa->solution == SOL_BASIC)            ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_SOL);         else if (csa->solution == SOL_INTERIOR)            ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_IPT);         else if (csa->solution == SOL_INTEGER)            ret = glp_mpl_postsolve(csa->tran, csa->prob, GLP_MIP);         else            xassert(csa != csa);         if (ret != 0)         {  xprintf("Model postsolving error\n");            ret = EXIT_FAILURE;            goto done;         }      }      /*--------------------------------------------------------------*/      /* write problem solution in printable format, if required */      if (csa->out_sol != NULL)      {  if (csa->solution == SOL_BASIC)            ret = lpx_print_sol(csa->prob, csa->out_sol);         else if (csa->solution == SOL_INTERIOR)            ret = lpx_print_ips(csa->prob, csa->out_sol);         else if (csa->solution == SOL_INTEGER)            ret = lpx_print_mip(csa->prob, csa->out_sol);         else            xassert(csa != csa);         if (ret != 0)         {  xprintf("Unable to write problem solution\n");            ret = EXIT_FAILURE;            goto done;         }      }      /* write problem solution in printable format, if required */      if (csa->out_res != NULL)      {  if (csa->solution == SOL_BASIC)            ret = glp_write_sol(csa->prob, csa->out_res);         else if (csa->solution == SOL_INTERIOR)            ret = glp_write_ipt(csa->prob, csa->out_res);         else if (csa->solution == SOL_INTEGER)            ret = glp_write_mip(csa->prob, csa->out_res);         else            xassert(csa != csa);         if (ret != 0)         {  xprintf("Unable to write problem solution\n");            ret = EXIT_FAILURE;            goto done;         }      }      /* write sensitivity bounds information, if required */      if (csa->out_bnds != NULL)      {  if (csa->solution == SOL_BASIC)         {  ret = lpx_print_sens_bnds(csa->prob, csa->out_bnds);            if (ret != 0)            {  xprintf("Unable to write sensitivity bounds information "                  "\n");               ret = EXIT_FAILURE;               goto done;            }         }         else            xprintf("Cannot write sensitivity bounds information for in"               "terior-point or MIP solution\n");      }      /*--------------------------------------------------------------*/      /* all seems to be ok */      ret = EXIT_SUCCESS;      /*--------------------------------------------------------------*/done: /* delete the LP/MIP problem object */      if (csa->prob != NULL)         glp_delete_prob(csa->prob);      /* free the translator workspace, if necessary */      if (csa->tran != NULL)         glp_mpl_free_wksp(csa->tran);      /* delete the network problem object, if necessary */      if (csa->graph != NULL)         glp_delete_graph(csa->graph);      xassert(gmp_pool_count() == 0);      gmp_free_mem();      /* close log file, if necessary */      if (csa->log_file != NULL) lib_close_log();      /* check that no memory blocks are still allocated */      {  int count;         xlong_t total;         lib_mem_usage(&count, NULL, &total, NULL);         if (count != 0)            xerror("Error: %d memory block(s) were lost\n", count);         xassert(count == 0);         xassert(total.lo == 0 && total.hi == 0);      }      /* free the library environment */      lib_free_env();      /* return to the control program */      return ret;}/* eof */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -