Exemple #1
0
void mexFunction(int nlhs, mxArray *plhs[], int	nrhs, const mxArray *prhs[])
{
  FILE *log;
  Options_Interface *o;
  Void *timer;
  MCP *m;
  MCP_Termination t;
  Information info;

  double *tempZ;
  double *status, *tot_time;
  double dnnz;
  int i, len;

  mat_install_error();
  mat_install_memory();

  log = fopen("logfile.tmp", "w");
  Output_SetLog(log);

  /* Options.
     1.  Get options associated with the algorithm
     2.  Set to default values
     3.  Read in an options file
     4.  Print out the options */

  o = Options_Create();
  Path_AddOptions(o);
  Options_Default(o);

  if (nrhs < 7) {
    Error("Not enough input arguments.\n");
  }

  if (nlhs < 2) {
    Error("Not enough output arguments.\n");
  }

  nMax = (int) mxGetScalar(prhs[0]);
  nnzMax = (int) mxGetScalar(prhs[1]);

  plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
  plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);

  if (nlhs > 2) {
    plhs[2] = mxCreateDoubleMatrix(nMax,1,mxREAL);
  }

  if (nlhs > 3) {
    plhs[3] = mxCreateDoubleMatrix(nMax,1,mxREAL);
  }

  /* Create a timer and start it. */
  timer = Timer_Create();
  Timer_Start(timer);

  /* Derefencing to get the status argument. */
  status = mxGetPr(plhs[0]);
  tot_time = mxGetPr(plhs[1]);

  /* First print out some version information.  */
  Output_Printf(Output_Log | Output_Status | Output_Listing,
		"%s: Matlab Link\n", Path_Version());

  /* Now check the arguments.

     If the dimension of the problem is 0, there is nothing to do.
  */

  if (nMax == 0) {
    Output_Printf(Output_Log | Output_Status,
		  "\n ** EXIT - solution found (degenerate model).\n");
    (*status) = 1.0;
    (*tot_time) = Timer_Query(timer);

    Timer_Destroy(timer);
    Options_Destroy(o);
    fclose(log);
    return;
  }

  /* Check size of the jacobian.  nnz < n*n.  Need to convert to double
     so we do not run out of integers.

     dnnz - max number of nonzeros as a double
  */

  dnnz = MIN(1.0*nnzMax, 1.0*nMax*nMax);
  if (dnnz > INT_MAX) {
    Output_Printf(Output_Log | Output_Status,
		  "\n ** EXIT - model too large.\n");
    (*status) = 0.0;
    (*tot_time) = Timer_Query(timer);

    Timer_Destroy(timer);
    Options_Destroy(o);
    fclose(log);
    return;
  }
  nnzMax = (int) dnnz;

  /* Have correct number of nonzeros.  Print some statistics.
     Print out the density.  */

  Output_Printf(Output_Log | Output_Status | Output_Listing,
		"%d row/cols, %d non-zeros, %3.2f%% dense.\n\n",
		nMax, nnzMax, 100.0*nnzMax/(1.0*nMax*nMax));

  if (nnzMax == 0) {
    nnzMax++;
  }

  zp = mxGetPr(prhs[2]);
  lp = mxGetPr(prhs[3]);
  up = mxGetPr(prhs[4]);

  m_start = mxGetJc(prhs[5]);
  m_row = mxGetIr(prhs[5]);
  m_data = mxGetPr(prhs[5]);
  m_len = (int *)Memory_Allocate(nMax*sizeof(int));

  for (i = 0; i < nMax; i++) {
    m_len[i] = m_start[i+1] - m_start[i];
    m_start[i]++;
  }
  actualNnz = m_start[nMax];

  for (i = 0; i < actualNnz; i++) {
    m_row[i]++;
  }

  q = mxGetPr(prhs[6]);
     
  /* Create a structure for the problem. */
  m = MCP_Create(nMax, nnzMax);
  MCP_Jacobian_Structure_Constant(m, 1);
  MCP_Jacobian_Data_Contiguous(m, 1);
  install_interface(m);

  Options_Read(o, "path.opt");
  Options_Display(o);

  info.generate_output = Output_Log | Output_Status | Output_Listing;
  info.use_start = True;
  info.use_basics = True;

  /* Solve the problem.  
     m - MCP to solve, info - information, t - termination */

  t = Path_Solve(m, &info);

  /* Read in the results.  First get the solution vector. The report. */

  tempZ = MCP_GetX(m);
  for (i = 0; i < nMax; i++) {
    zp[i] = tempZ[i];
  }

  /* If the final function evaluation is wanted, report it. */
  if (nlhs > 2) {
    double *fval = mxGetPr(plhs[2]);

    tempZ = MCP_GetF(m);
    for (i = 0; i < nMax; i++) {
      fval[i] = tempZ[i];
    }
  }

  /* If the final basis is wanted, report it. */
  if (nlhs > 3) {
    double *bval = mxGetPr(plhs[3]);
    int *tempB;

    tempB = MCP_GetB(m);
    for (i = 0; i < nMax; i++) {
      switch(tempB[i]) {
      case Basis_LowerBound:
	bval[i] = -1;
	break;

      case Basis_UpperBound:
	bval[i] = 1;
	break;

      default:
	bval[i] = 0;
	break;
      }
    }
  }

  switch(t) {
  case MCP_Solved:
    *status = 1.0;
    break;

  default:
    *status = 0.0;
    break;
  }
     
  /* Stop the timer and report the results. */
  (*tot_time) = Timer_Query(timer);
  Timer_Destroy(timer);

  /* Clean up.  Deallocate memory used and close the log. */
  MCP_Destroy(m);
  Memory_Free(m_len);
  Options_Destroy(o);

  fclose(log);
  return;
}
Exemple #2
0
int path_main( int n, int nnz,
                double *z, double *f, double *lb, double *ub,
                char **var_name, char **con_name,
                void *f_eval, void *j_eval) {


  // solve status: return value
  int status;

  // creating a Problem instance
  problem.n = n;
  problem.nnz = nnz;
  problem.z = z;
  problem.f = f;
  problem.lb = lb;
  problem.ub = ub;
  problem.var_name = var_name;
  problem.con_name = con_name;
  problem.f_eval = f_eval;
  problem.j_eval = j_eval;

  if (var_name == NULL) {
    mcp_interface.variable_name = NULL;
  } else {
    mcp_interface.variable_name = variable_name;
  }

  if (con_name == NULL) {
    mcp_interface.constraint_name = NULL;
  } else {
    mcp_interface.constraint_name = constraint_name;
  }




  // Output Interface
  #if defined(USE_OUTPUT_INTERFACE)
    Output_SetInterface(&outputInterface);
  #else
    /* N.B.: the Output_SetLog call does not work when using a DLL:
     * the IO systems of a .exe and .dll do not automatically interoperate */
    Output_SetLog(stdout);
  #endif

  // Information instance
  Information info;
  info.generate_output = Output_Log | Output_Status | Output_Listing;
  info.use_start = True;
  info.use_basics = True;

  // Options Interface
  Options_Interface *o;
  o = Options_Create();
  Path_AddOptions(o);
  Options_Default(o);
  Options_Read(o, "path.opt");
  Options_Display(o);

  // Preprocessing
  if (n == 0) {
    fprintf(stdout, "\n ** EXIT - solution found (degenerate model).\n");
    (status) = MCP_Solved;
    Options_Destroy(o);
    return status;
  }

  double dnnz;
  dnnz = MIN(1.0*nnz, 1.0*n*n);
  if (dnnz > INT_MAX) {
    fprintf(stdout, "\n ** EXIT - model too large.\n");
    (status) = MCP_Error;
    Options_Destroy(o);
    return status;
  }
  nnz = (int) dnnz;

  fprintf(stdout, "%d row/cols, %d non-zeros, %3.2f%% dense.\n\n",
          n, nnz, 100.0*nnz/(1.0*n*n));
  nnz++;




  // preparing an MCP instance and interfaces
  MCP *m;
  m = MCP_Create(n, nnz);
  MCP_SetInterface(m, &mcp_interface);
  Output_SetLog(stdout);




  // SOLVE
  status = Path_Solve(m, &info);


  // Preparing return values
  double *tempZ;
  double *tempF;
  tempZ = MCP_GetX(m);
  tempF = MCP_GetF(m);

  int i;
  for (i = 0; i < n; i++) {
    z[i] = tempZ[i];
    f[i] = tempF[i];
  }



  // destroy
  MCP_Destroy(m);
  Options_Destroy(o);

  return status;

}
void F_CALLCONV MYMAIN(int *n, int *nnz, int *status,
	               double *z, double *f, double *lb, double *ub)
{
  Options_Interface *o;
  MCP *m;
  MCP_Termination t;
  Information info;

  double *tempZ;
  double *tempF;
  double dnnz;
  int i;

  for_install_output();

  o = Options_Create();
  Path_AddOptions(o);
  Options_Default(o);

  Output_Printf(Output_Log | Output_Status | Output_Listing,
		"%s: Standalone-F Link\n", Path_Version());

  problem.n = *n;
  if (problem.n == 0) {
    Output_Printf(Output_Log | Output_Status,
		  "\n ** EXIT - solution found (degenerate model).\n");
    (*status) = MCP_Solved;
    Options_Destroy(o);
    return;
  }

  dnnz = MIN(1.0*(*nnz), 1.0*problem.n*problem.n);
  if (dnnz > INT_MAX) {
    Output_Printf(Output_Log | Output_Status,
		  "\n ** EXIT - model too large.\n");
    (*status) = MCP_Error;
    Options_Destroy(o);
    return;
  }
  problem.nnz = (int) dnnz;

  Output_Printf(Output_Log | Output_Status | Output_Listing,
		"%d row/cols, %d non-zeros, %3.2f%% dense.\n\n",
		problem.n, problem.nnz, 
		100.0*problem.nnz/(1.0*problem.n*problem.n));
  problem.nnz++;

  problem.z = z;
  problem.f = f;
  problem.lb = lb;
  problem.ub = ub;

  m = MCP_Create(problem.n, problem.nnz);
  install_interface(m);

  Options_Read(o, "path.opt");
  Options_Display(o);

  info.generate_output = Output_Log | Output_Status | Output_Listing;
  info.use_start = True;
  info.use_basics = True;

  t = Path_Solve(m, &info);

  tempZ = MCP_GetX(m);
  tempF = MCP_GetF(m);

  for (i = 0; i < problem.n; i++) {
    z[i] = tempZ[i];
    f[i] = tempF[i];
  }
  *status = t;

  MCP_Destroy(m);
  Options_Destroy(o);
  return;
}
void SimpleQP(int variables, int constraints,
              int q_nnz, int *q_i, int *q_j, double *q_ij, double *c,
              int a_nnz, int *a_i, int *a_j, double *a_ij, double *b, 
	      int *c_type, double *lb, double *ub,
              QP_Termination *status, double *z, double *pi, double *obj)
{
  Options_Interface *o;
  MCP *m;
  MCP_Termination t;
  Information info;

  double *x;
  double dnnz;
  int element, i;
  int colStart, colEnd;

  o = Options_Create();
  Path_AddOptions(o);
  Options_Default(o);

  Output_Printf(Output_Log | Output_Status | Output_Listing,
		"%s: QP Link\n", Path_Version());

  create(variables, constraints, q_nnz, q_i, q_j, q_ij, c,
	 a_nnz, a_i, a_j, a_ij, b, c_type, z, pi, lb, ub);
     
  if (problem.n == 0) {
    Output_Printf(Output_Log | Output_Status,
		  "\n ** EXIT - solution found (degenerate model).\n");
    (*status) = QP_Solved;
    Options_Destroy(o);
    return;
  }

  dnnz = MIN(1.0*problem.nnz, 1.0*problem.n*problem.n);
  if (dnnz > INT_MAX) {
    Output_Printf(Output_Log | Output_Status,
		  "\n ** EXIT - model too large.\n");
    (*status) = QP_Error;
    Options_Destroy(o);
    return;
  }
  problem.nnz = (int) dnnz;

  Output_Printf(Output_Log | Output_Status | Output_Listing,
		"%d row/cols, %d non-zeros, %3.2f%% dense.\n\n",
		problem.n, problem.nnz, 
		100.0*problem.nnz/(1.0*problem.n*problem.n));

  m = MCP_Create(problem.n, problem.nnz+1);
  MCP_Jacobian_Structure_Constant(m, 1);
  install_interface(m);

  Options_Read(o, "path.opt");
  Options_Display(o);

  info.generate_output = Output_Log | Output_Status | Output_Listing;
  info.use_start = True;
  info.use_basics = True;

  t = Path_Solve(m, &info);

  if (t == MCP_Solved) {
    *status = QP_Solved;
  } 
  else {
    Options_Default(o);
  
    Options_Read(o, "path.op2");
    Options_Display(o);

    for (i = 0; i < variables; i++) {
      problem.q[i] = 0;

      colStart = problem.m_start[i] - 1;
      colEnd = colStart + problem.m_len[i];

      while(colStart < colEnd) {
	if (problem.m_row[colStart] <= variables) {
	  problem.m_data[colStart] = 0.0;
	}
	colStart++;
      }
    }

    info.generate_output = Output_Log | Output_Status | Output_Listing;
    info.use_start = True;
    info.use_basics = True;

    t = Path_Solve(m, &info);
    if (t == MCP_Solved) {
      *status = QP_Unbounded;
    }
    else {
      *status = QP_Infeasible;
    }
  }

  x = MCP_GetX(m);
     
  for (i = 0; i < variables; i++) {
    z[i] = x[i];
  }

  for (i = 0; i < constraints; i++) {
    pi[i] = x[i + variables];
  }

  *obj = 0;
  for (i = 0; i < variables; i++) {
    for (element = problem.m_start[i] - 1;
	 element < problem.m_start[i] + problem.m_len[i] - 1;
	 element++) {
      if (problem.m_row[element] <= variables) {
	*obj += problem.m_data[element] * 
	  z[i] * z[problem.m_row[element] - 1];
      }
    }
  }
     
  *obj /= 2.0;
  for (i = 0; i < variables; i++) {
    *obj += z[i] * problem.q[i];
  }
     
  MCP_Destroy(m);
  destroy();

  Options_Destroy(o);
  return;
}