Exemple #1
0
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;
}
Exemple #2
0
 void
MAIN__(void)
{
	FILE *nl;
	fint *iv, liv, lv;
	fint N, NZ, P;
	real *rhsLU, *v;
	int i, j;
	extern int xargc;
	extern char **xargv;
	char *stub;
	static fint L1 = 1;
	static char *rvmsg[9] = {
		"X-Convergence", /* 3 */
		"Relative Function Convergence",
		"X- and Relative Function Convergence",
		"Absolute Function Convergence",
		"Singular Convergence",
		"False Convergence",
		"Function Evaluation Limit",
		"Iteration Limit",
		"Unexpected return code"
		};
	char buf[256];

	if (xargc < 2) {
		fprintf(Stderr, "usage: %s stub\n", xargv[0]);
		exit(1);
		}
	stub = xargv[1];

	ASL_alloc(ASL_read_fg);

	amplflag = xargc >= 3 && !strncmp(xargv[2], "-AMPL", 5);

	nl = jac0dim(stub, (fint)strlen(stub));
	if (n_obj) {
		fprintf(Stderr, "Ignoring %d objectives.\n", n_obj);
		fflush(Stderr);
		}

	N = n_con;
	P = n_var;
	NZ = nzc;
	liv = 82 + 4*P;
	lv = 105 + P*(N + 2*P + 21) + 2*N;
	v = (real *)Malloc((lv + P)*sizeof(real) + liv*sizeof(fint));
	X0 = v + lv;
	iv = (fint *)(X0 + P);

	fg_read(nl,0);

	/* Check for valid problem: all equality constraints. */

	for(i = j = 0, rhsLU = LUrhs; i++ < N; rhsLU += 2)
		if (rhsLU[0] != rhsLU[1]) {
			if (j++ > 4) {
				/* Stop chattering if > 4 errors. */
				fprintf(Stderr, "...\n");
				exit(2);
				}
			fprintf(Stderr, "Lrhs(%d) = %g < Urhs(%d) = %g\n",
				i, rhsLU[0], i, rhsLU[1]);
			}
	if (j)
		exit(2);

	dense_j();	/* Tell jacval_ we want a dense Jacobian. */

	divset_(&L1, iv, &liv, &lv, v);	/* set default iv and v values */
	if (amplflag)
		iv[prunit] = 0; /* Turn off printing . */

	dn2gb_(&N, &P, X0, LUv, (U_fp)calcr, (U_fp)calcj,
		iv, &liv, &lv, v, &NZ, LUrhs, (U_fp)calcr);

	j = iv[0] >= 3 && iv[0] <= 10 ? (int)iv[0] - 3 : 8;
	i = Sprintf(buf, "nl21: %s", rvmsg[j]);
	if (j == 8)
		i += Sprintf(buf+i, " %ld", iv[0]);
	i += Sprintf(buf+i,
		"\n%ld function, %ld gradient evaluations",
		iv[nfcall], iv[ngcall]);
	i += Sprintf(buf+i, "\nFinal sum of squares = ");
	g_fmtop(buf+i, 2*v[f]);
	write_sol(buf, X0, 0, 0);
	}
Exemple #3
0
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;
}
Exemple #4
0
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;
}
Exemple #5
0
int main(int argc, char **argv)
{
	FILE *nl;
	char *stub;
	ASL *asl;
	fint nerror = 0;
	real *X;
	real objVal;
	int i, j, k, je;
	real* J;
	cgrad *cg;
	char *fmt;

	if (argc < 2) {
		printf("Usage: %s stub\n", argv[0]);
		return 1;
	}
	
	double t0 = clock_now();
	asl = ASL_alloc(ASL_read_fg);
	stub = argv[1];
	nl = jac0dim(stub, (fint)strlen(stub));
	fg_read(nl,0);
	
	J = (real *)Malloc(nzc*sizeof(real));
	X = (real *)Malloc(n_var*sizeof(real));
	for (i = 0; i < n_var; i++) X[i] = 1.0;

	// include one jacobian call for consistency
	jacval(X, J, &nerror);

	t0 = clock_now() - t0;
	
	//objVal = objval(0, X, &nerror);
	//printf("Objective %.5f\n", objVal);
	
	double jactime = 1e30;
	for (k = 0; k < 10; k++) {
		double t1 = clock_now();
		jacval(X, J, &nerror);
		t1 = clock_now() - t1;
		jactime = (t1 < jactime) ? t1 : jactime;
	}

	double norm = 0;
	for (i = 0; i < nzc; i++) {
		norm += J[i]*J[i];
	}
	norm = sqrt(norm);


	char *bname = basename(argv[1]);
	// Initialization time, 1 jacobian evaluation
	printf("### %s %f %g\n",bname,t0,jactime);
	printf("## %s Jacobian norm: %.10g (nnz = %d)\n",bname,norm,nzc);


	
	/*printf("nzc %d\n",nzc);
	for(i = 0; i < nzc; i++) {
		printf("%d %g\n",i,J[i]);
	}*/
	/*
	for(i = 0; i < n_con; i++) {
		printf("\nRow %d:", i+1);
		//fmt = " J[%d]=%g*X%d";
		//for(cg = Cgrad[i]; cg; cg = cg->next, fmt = " + J[%d]=%g*X%d") {
		//	printf(fmt, cg->goff, J[cg->goff], cg->varno+1);
		//}
		fmt = " %g*X%d";
		for(cg = Cgrad[i]; cg; cg = cg->next, fmt = " + %g*X%d") {
			printf(fmt, J[cg->goff], cg->varno+1);
		}
		printf("\n");
	}*/

	return 0;
}