示例#1
0
文件: readASLfg.cpp 项目: imzye/coin
ASL *readASLfg (char **argv) {

  // read .nl file from first argument //////////////////////////

  char *stub;

  // Create the ASL structure
  ASL* asl = (ASL*) ASL_alloc (ASL_read_fg);
  FILE *nl = NULL;
  stub = getstub (&argv, &Oinfo);

  // Although very intuitive, we shall explain why the second argument
  // is passed with a minus sign: it is to tell the ASL to retrieve
  // the nonlinear information too.
  nl = jac0dim (stub, - (fint) strlen (stub));

  // Set options in the asl structure
  want_xpi0 = 1 | 2;  // allocate initial values for primal and dual if available
  obj_no = 0;         // always want to work with the first (and only?) objective

  // allocate space for initial values
  X0      = new real [n_var];
  havex0  = new char [n_var];
  pi0     = new real [n_con];
  havepi0 = new char [n_con];

  // read the rest of the nl file
  fg_read (nl, ASL_return_read_err | ASL_findgroups);

  return asl;
}
示例#2
0
/*--------------------------------------------------------------------------
 Searches for stub and if it exists, notifies it of pending exit and if it
  wants NetShell relaunched (yes if status is zero) then exits application.
--------------------------------------------------------------------------*/
DP_API void dpExitFromApp(int status)
{
	OSErr err;
	ProcessSerialNumber stubPSN;

	if (!status) {
		err = getstub(&stubPSN);
		if (err == dp_RES_OK ) {
			int done = 0;
			TargetID sender;
			unsigned long msgRefcon = REFCON;
			unsigned long sendRefcon, myOpts, msglen;
			EventRecord event;
			event.what = kHighLevelEvent;
			event.message = STUB_EVENTID;
			event.where = longtopt(STUB_OBITCLASS);
			myOpts = receiverIDisPSN  + nAttnMsg;
			err = PostHighLevelEvent(&event, &stubPSN, msgRefcon, nil, 0, myOpts);
			if (err != noErr) {
				DPRINT(("post to stub about die error %d\n", err));
			}
		}
	}
	ExitToShell();
}
示例#3
0
文件: bb.c 项目: TheLoutre/nomad
int main ( int argc, char **argv ) {

  FILE * nl;
  char * stub;
  FILE * point_file;
  char * point_file_name;
  int    point_file_name_size;
  fint   nerror = (fint)0;
  int    n_badvals = 0;
  int    n_con_tmp = 0;
  int    i;
  real   f;
  real * R;

  if( argc < 2 ) {
    fprintf ( stderr , "Usage: %s x.txt\n" , argv[0] );
    return 1;
  }

  // get the point file name:
  point_file_name_size = strlen(argv[1]) + 1;
  point_file_name      = (char*)Malloc(point_file_name_size * sizeof(char));
  strcpy ( point_file_name , argv[1] );
  strcpy ( argv[1] , MODEL_NAME );

  // Read objectives and first derivative information.
  if( !(asl = ASL_alloc(ASL_read_fg)) ) exit(1);
  stub = getstub(&argv, &Oinfo);
  nl   = jac0dim(stub, (fint)strlen(stub));

  // Get command-line options.
  if (getopts(argv, &Oinfo)) exit(1);

  // Check command-line options.
  if( showgrad < 0 || showgrad > 1 ) {
    Printf("Invalid value for showgrad: %d\n", showgrad);
    n_badvals++;
  }
  if( showname < 0 || showname > 1 ) {
    Printf("Invalid value for showgrad: %d\n", showgrad);
    n_badvals++;
  }

  if(n_badvals) {
    Printf("Found %d errors in command-line options.\n", n_badvals);
    exit(1);
  }

  // Allocate memory for problem data.
  // The variables below must have precisely THESE names.
  X0    = (real*)Malloc(n_var * sizeof(real));  // Initial guess
  pi0   = (real*)Malloc(n_con * sizeof(real));  // Initial multipliers
  LUv   = (real*)Malloc(n_var * sizeof(real));  // Lower bounds on variables
  Uvx   = (real*)Malloc(n_var * sizeof(real));  // Upper bounds on variables
  LUrhs = (real*)Malloc(n_con * sizeof(real));  // Lower bounds on constraints
  Urhsx = (real*)Malloc(n_con * sizeof(real));  // Upper bounds on constraints
  R     = (real*)Malloc(n_con * sizeof(real));  // constraints

  want_xpi0 = 3;

  // Read in ASL structure - trap read errors
  if( fg_read(nl, 0) ) {
    fprintf(stderr, "Error fg-reading nl file\n");
    goto bailout;
  }

#ifdef DISPLAY

  n_con_tmp = 0;
  for ( i = 0 ; i < n_con ; ++i ) {
    if ( LUrhs[i] > -Infinity )
      ++n_con_tmp;
    if ( Urhsx[i] < Infinity )
      ++n_con_tmp;
  }

  printf ( "n_obj=%i\nn_var=%i\nn_con=%i\nx0=[" , n_obj , n_var , n_con_tmp );
  for ( i = 0 ; i < n_var ; ++i )
    printf ( "%g " , X0[i] );
  printf ( "]\n" );
#endif

  // read x:
  if ((point_file = fopen(point_file_name,"r")) == NULL) {
    fprintf(stderr, "Cannot open file %s.\n",point_file_name);
    goto bailout;
  }

  for ( i = 0 ; i < n_var ; ++i )
    fscanf ( point_file , "%lf" , &X0[i] );

  fclose(point_file);
  free ( point_file_name );


#ifdef DISPLAY
  printf ( "x =[" );
  for ( i = 0 ; i < n_var ; ++i )
    printf ( "%g " , X0[i] );
  printf ( "]\n" );
#endif

  // objective functions:
  for ( i = 0 ; i < n_obj ; ++i ) {
    f = objval ( i , X0 , &nerror ); 

    if ( nerror ) {
      fprintf(stderr, "Error while evaluating objective.\n");
      goto bailout;
    }

#ifdef DISPLAY
    Printf("f%i(x) = %21.15e\n", i , f );
#else
    Printf("%21.15e\n", f );
#endif
  }

  // constraints:
  conval ( X0 , R , &nerror );

  for ( i = 0 ; i < n_con ; ++i ) {

#ifdef DISPLAY
    printf ("%g <= %g <= %g\n" ,  LUrhs[i] , R[i] , Urhsx[i] );
#else
    if ( LUrhs[i] > -Infinity )
      Printf("%21.15e\n", LUrhs[i]-R[i] );
    if ( Urhsx[i] < Infinity )
      Printf("%21.15e\n", R[i]-Urhsx[i] );
#endif
  }

 bailout:
  // Free data structure. DO NOT use free() on X0, pi0, etc.
  ASL_free((ASL**)(&asl));

  return 0;
}
示例#4
0
main(int argc, char **argv)
#endif
{
	char *stub;
	ASL *asl;
	FILE *nl;
	lprec *lp;
	ograd *og;
	int ct, i, intmin, *is, j, j0, j1, k, nalt, rc;
	short *basis, *lower;
	real *LU, *c, lb, objadj, *rshift, *shift, t, ub, *x, *x0, *x1;
	char buf[256];
	typedef struct { char *msg; int code; } Sol_info;
	static Sol_info solinfo[] = {
		{ "optimal", 0 },
		{ "integer programming failure", 502 },
		{ "infeasible", 200 },
		{ "unbounded", 300 },
		{ "failure", 501 },
		{ "bug", 500 }
		};

	sprintf(lp_solve_version+9, "%.*s", (int)sizeof(lp_solve_version)-10,
		PATCHLEVEL);
	sprintf(lp_solve_vversion, "%s, driver(20001002)", lp_solve_version);
	asl = ASL_alloc(ASL_read_f);
	stub = getstub(&argv, &Oinfo);
	nl = jac0dim(stub, (fint)strlen(stub));
	suf_declare(suftab, sizeof(suftab)/sizeof(SufDecl));

	/* set A_vals to get the constraints column-wise */
	A_vals = (real *)M1alloc(nzc*sizeof(real));

	f_read(nl,0);

	lp = make_lp(n_con, 0);

	Oinfo.uinfo = (char *)lp;
	if (getopts(argv, &Oinfo))
		return 1;

	i = n_var + n_con + 1;
	x = (real*)M1alloc(i*sizeof(real));	/* scratch vector */
	memset(x, 0, i*sizeof(real));
	x0 = x++;
	c = x + n_con;

	/* supply objective */

	objadj = 0;
	if (--nobj >= 0 && nobj < n_obj) {
		for(og = Ograd[nobj]; og; og = og->next)
			c[og->varno] = og->coef;
		if (objtype[nobj])
			set_maxim(lp);
		objadj = objconst(nobj);
		}

	/* supply columns and variable bounds */

	LU = LUv;
	intmin = n_var - (nbv + niv);
	j1 = nalt = 0;
	rshift = shift = 0;
	for(i = 1; i <= n_var; i++, LU += 2) {
		lb = LU[0];
		ub = LU[1];
		j0 = j1;
		j1 = A_colstarts[i];
		*x0 = *c++;	/* cost coefficient */
		if (lb <= negInfinity && ub < Infinity) {
			/* negate this variable */
			nalt++;
			lb = -ub;
			ub = -LU[0];
			for(j = j0; j < j1; j++)
				x[A_rownos[j]] = -A_vals[j];
			*x0 = -*x0;
			add_column(lp, x0);
			if (lb)
				goto shift_check;
			}
		else {
			for(j = j0; j < j1; j++)
				x[A_rownos[j]] = A_vals[j];
			add_column(lp, x0);
			if (lb <= negInfinity) {
				nalt++;
				if (i > intmin)
					set_int(lp, lp->columns, TRUE);
				/* split free variable */
				*x0 = -*x0;
				for(j = j0; j < j1; j++)
					x[A_rownos[j]] *= -1.;
				add_column(lp,x0);
				}
			else if (lb) {
 shift_check:
				if (lb > 0)
					set_lowbo(lp, lp->columns, lb);
				else {
					if (!rshift) {
						rshift = (real*)M1zapalloc(
						  (n_var+n_con)*sizeof(real));
						shift = rshift + n_con - 1;
						}
					shift[i] = lb;
					for(j = j0; j < j1; j++) {
						k = A_rownos[j];
						rshift[k] += lb*x[k];
						}
					if (ub < Infinity)
						ub -= lb;
					objadj += lb**x0;
					}
				}
			if (ub < Infinity)
				set_upbo(lp, lp->columns, ub);
			}
		for(j = j0; j < j1; j++)
			x[A_rownos[j]] = 0;
		if (i > intmin)
			set_int(lp, lp->columns, TRUE);
		}

	if (objadj) {
		/* add a fixed variable to adjust the objective value */
		*x0 = objadj;
		add_column(lp, x0);
		set_lowbo(lp, i, 1.);
		set_upbo(lp, i, 1.);
		}

	/* supply constraint rhs */

	LU = LUrhs;
	for(i = 1; i <= n_con; i++, LU += 2) {
		t = LU[0];
		if (t == LU[1])
			ct = EQ;
		else if (t <= negInfinity) {
			t = LU[1];
			if (t >= Infinity) {
				/* This is possible only with effort: */
				/* one must turn presolve off and */
				/* explicitly specify a constraint */
				/* with infinite bounds. */
				fprintf(Stderr,
					"Sorry, can't handle free rows.\n");
				exit(1);
				}
			ct = LE;
			}
		else
			ct = GE;
		set_constr_type(lp, i, ct);
		set_rh(lp, i, rshift ? t - *rshift++ : t);
		if (ct == GE && LU[1] < Infinity)
			lp->orig_upbo[i] = LU[1] - t;
		}

	if (prlp)
		print_lp(lp);
	if (scaling)
		auto_scale(lp);

	/* Unfortunately, there seems to be no way to suggest */
	/* a starting basis to lp_solve; thus we must ignore  */
	/* any incoming .sstatus values. */

	rc = solve(lp);
	if (rc < 0 || rc > 5)
		rc = 5;
	solve_result_num = solinfo[rc].code;
	i = sprintf(buf, "%s: %s", Oinfo.bsname, solinfo[rc].msg);
	if (rc == OPTIMAL)
		i += sprintf(buf+i, ", objective %.*g", obj_prec(),
			lp->best_solution[0]);
	i += sprintf(buf+i,"\n%d simplex iterations", lp->total_iter);
	if (lp->max_level > 1 || lp->total_nodes > 1)
		sprintf(buf+i, "\n%d branch & bound nodes: depth %d",
			lp->total_nodes, lp->max_level);

	/* Prepare to report solution: deal with split free variables. */

	x1 = lp->best_solution+lp->rows+1;
	if (nalt || shift) {
		x = x0;
		LU = LUv;
		for(i = 0; i < n_var; i++, LU += 2) {
			if (LU[0] > negInfinity)
				x[i] = *x1++;
			else if (LU[1] < Infinity)
				x[i] = -*x1++;
			else {
				x[i] = x1[0] - x1[1];
				x1 += 2;
				}
			if (shift)
				x[i] += *++shift;
			}
		}
	else
		x = x1;

	if (solinfo[rc].code < 500 && !(nbv + niv)) {

		/* return .sstatus values */

		basis = lp->basis;
		lower = lp->lower;
		is = M1alloc((n_var + n_con)*sizeof(int));
		suf_iput("sstatus", ASL_Sufkind_con, is);
		for(i = 0; i < n_con; i++) {
			j = *++lower;
			*is++ = *++basis ? 1 : j ? 3 : 4;
			}
		suf_iput("sstatus", ASL_Sufkind_var, is);
		LU = LUv;
		for(i = 0; i < n_var; i++, LU += 2) {
			j0 = *++basis;
			j1 = *++lower;
			if (LU[0] > negInfinity)
				j = j0 ? 1 : j1 ? 3 : 4;
			else if (LU[1] < Infinity)
				j = j0 ? 1 : j1 ? 4 : 3;
			else {
				++lower;
				j = *++basis || j0;
				}
			*is++ = j;
			}
		}

	write_sol(buf, x, lp->duals+1, &Oinfo);
	/* The following calls would only be needed */
	/* if execution were to continue... */
	delete_lp(lp);
	ASL_free(&asl);
	return 0;
	}
示例#5
0
文件: miniampl.c 项目: dpo/miniampl
int main(int argc, char **argv) {

  FILE *nl;
  char *stub;
  fint nerror = (fint)0;
  int n_badvals = 0;
  real f;

  if( argc < 2 ) {
    fprintf(stderr, "Usage: %s stub\n", argv[0]);
    return 1;
  }

  // Read objectives and first derivative information.
  if( !(asl = ASL_alloc(ASL_read_fg)) ) exit(1);
  stub = getstub(&argv, &Oinfo);
  nl   = jac0dim(stub, (fint)strlen(stub));

  // Get command-line options.
  if (getopts(argv, &Oinfo)) exit(1);

  // Check command-line options.
  if( showgrad < 0 || showgrad > 1 ) {
    Printf("Invalid value for showgrad: %d\n", showgrad);
    n_badvals++;
  }
  if( showname < 0 || showname > 1 ) {
    Printf("Invalid value for showname: %d\n", showname);
    n_badvals++;
  }

  if(n_badvals) {
    Printf("Found %d errors in command-line options.\n", n_badvals);
    exit(1);
  }

  // Allocate memory for problem data.
  // The variables below must have precisely THESE names.
  X0    = (real*)Malloc(n_var * sizeof(real));  // Initial guess
  pi0   = (real*)Malloc(n_con * sizeof(real));  // Initial multipliers
  LUv   = (real*)Malloc(n_var * sizeof(real));  // Lower bounds on variables
  Uvx   = (real*)Malloc(n_var * sizeof(real));  // Upper bounds on variables
  LUrhs = (real*)Malloc(n_con * sizeof(real));  // Lower bounds on constraints
  Urhsx = (real*)Malloc(n_con * sizeof(real));  // Upper bounds on constraints
  want_xpi0 = 3;

  // Read in ASL structure - trap read errors
  if( fg_read(nl, 0) ) {
    fprintf(stderr, "Error fg-reading nl file\n");
    goto bailout;
  }

  if(showname) { // Display objective name if requested.
    Printf("Objective name: %s\n", obj_name(0));
  }

  // This "solver" outputs the objective function value at X0.
  f = objval(0, X0, &nerror);
  if(nerror) {
    fprintf(stderr, "Error while evaluating objective.\n");
    goto bailout;
  }
  Printf("f(x0) = %21.15e\n", f);

  // Optionally also output objective gradient at X0.
  if(showgrad) {
    real *g = (real*)malloc(n_var * sizeof(real));
    objgrd(0, X0, g, &nerror);
    Printf("g(x0) = [ ");
    for(int i=0; i<n_var; i++) Printf("%8.1e ", g[i]);
    Printf("]\n");
    free(g);
  }

  // Write solution to file. Here we just write the initial guess.
  Oinfo.wantsol = 9;  // Suppress message echo. Force .sol writing
  write_sol(CHR"And the winner is", X0, pi0, &Oinfo);

 bailout:
  // Free data structure. DO NOT use free() on X0, pi0, etc.
  ASL_free((ASL**)(&asl));

  return 0;
}