Exemple #1
0
/* Set the upperbound of a variable */
long __declspec(dllexport) WINAPI _set_upbo(lprec *lp, long column, double value)
 {
  long ret;

  if (lp != NULL) {
   freebuferror();
   ret = set_upbo(lp, column, value);
  }
  else
   ret = 0;
  return(ret);
 }
int CLPLpsolve::changeBds(int varIndex, const char* lowOrUpper, double bound)
{
	if (stricmp(lowOrUpper, "L") == 0)
		m_status = set_lowbo(m_env, varIndex + 1, bound);

	else if (stricmp(lowOrUpper, "U") == 0)
		m_status = set_upbo(m_env, varIndex + 1, bound);

	else
		assert(false);

	return getStatus();
}
/*
	Solve the LP subproblem by calling lp_solve API 
*/
int LLW_solve_lp(double **gradient, const struct TrainingCache *cache, const struct Model *model)
{
	long i,k,l,ind_pattern,y_i;
	const long Q = model->Q;
	const double Qd = (double)Q;
	const long chunk_size = cache->chunk_size;
	const double *C = model->C;
	
	const int nRows = Q-1; 
	const int nCols = chunk_size * Q;

	double *obj = (double*)malloc(sizeof(double) * (1+nCols));
	double *row = (double*)malloc(sizeof(double) * (1+nCols));
	double *rhs = (double*)malloc(sizeof(double) * Q);
	long **lp_sol_table = matrix_l(nCols, 2);
	long **lp_sol_table_inv = matrix_l(chunk_size, Q);
	double *sol = (double*)malloc(sizeof(double) * (1+nRows+nCols));
	double epsel;
		
	// Make LP
	lprec *lp = make_lp(0, nCols);
	set_add_rowmode(lp, TRUE);

	// Make objective function
	int col = 1;
	for(i=1; i<=chunk_size; i++)
	  {
	  ind_pattern = cache->table_chunk[i];
	  for(k=1; k<=Q; k++)
	      {
	      	obj[col] = gradient[ind_pattern][k];
		lp_sol_table[col][1] = i;	// keep a table of correspondance between
		lp_sol_table[col][2] = k;	// LPSOLVE vector of variables and lp_sol matrix
		lp_sol_table_inv[i][k] = col++; // lp_sol[i][k] = the 'lp_solve_table_inv[i][k]'-th variable for LPSOLVE
	      }
	  }
	set_obj_fn(lp, obj);


/*	// Make RHS of constraints
	// -- complete computation --
	for(k=1; k<Q; k++)
	  {
	  rhs[k] = 0.0;
	  for(i=1; i<=nb_data; i++)
	    if(cache->in_chunk[i] == 0)
	      {
	      for(l=1; l<=Q; l++)
		 rhs[k] += model->alpha[i][l];
	      rhs[k] -= Qd * model->alpha[i][k];
		
	      }
	  }
*/	  

	// Make RHS of constraints
	// -- updates to cache->rhs are made in compute_new_alpha()
	//    to keep track of rhs
	//    we only need to remove the contribution of the examples in the chunk
	for(k=1; k<Q; k++)
	  {
	  rhs[k] = cache->lp_rhs[k];
	  for(i=1; i<=chunk_size; i++)
	      {
	      ind_pattern = cache->table_chunk[i];
	      for(l=1; l<=Q; l++)
		 rhs[k] -= model->alpha[ind_pattern][l];
	      rhs[k] += Qd * model->alpha[ind_pattern][k];		
	      }
	  }
	
	
	// Make constraints
	for(k=1; k<Q; k++)
	  {
	  for(col = 1;col <=nCols; col++)
	     row[col] = 0.0;

	  for(i=1; i<=chunk_size; i++)
	    {
	    ind_pattern = cache->table_chunk[i];
	    y_i = model->y[ind_pattern];

            for(l=1; l<=Q; l++)
              if(l != y_i)
        	{
        	row[lp_sol_table_inv[i][l]] = -1.0;
        	if(l == k)
        	     row[lp_sol_table_inv[i][l]] += Qd;
		}
	    }

	  add_constraint(lp, row, EQ, rhs[k]);
	  }

	// Upper bound constraints: alpha <= Cy_i
	for(col=1;col<=nCols;col++)
		set_upbo(lp, col, C[model->y[cache->table_chunk[lp_sol_table[col][1]]]]);

	/*
	for(i=1; i<=chunk_size; i++) {
	  for(k=1; k<=Q; k++)
	    if(k != model->y[cache->table_chunk[i]]) {
	    	col = (int)lp_sol_table_inv[i][k];
		set_upbo(lp, col, C);
	    }
	}
	*/
	
	// End of LP making
	set_add_rowmode(lp, FALSE);	
	//print_lp(lp);
	
	// Solve LP
	int jump = false;
	set_outputfile(lp,"");

	if(solve(lp)) {
		printf("Problem with the LP... \n");
		jump = true;
	}
	else {
		// Recover solution in the matrix lp_sol
		get_primal_solution(lp, sol);	// sol: template for lp_solve solution format
						// 	sol=[obj, constraints, variables] 

		epsel = get_epsel(lp);	// tolerance in lp_solve
		
		// Put solution into lp_sol
		for(col=1; col<= nCols; col++) {

			// Check feasibility of the col-th variable
			if((sol[nRows+col] < -epsel) || (sol[nRows+col] > C[model->y[cache->table_chunk[lp_sol_table[col][1]]]] + epsel)) {
				jump = true;
				break;
			}
			// Round off tolerance
			if(fabs(sol[nRows+col]) < epsel) 
				sol[nRows+col] = 0.0;
				
			else if(fabs(sol[nRows+col] - C[model->y[cache->table_chunk[lp_sol_table[col][1]]]]) < epsel)
				sol[nRows+col] = C[model->y[cache->table_chunk[lp_sol_table[col][1]]]];
				
			// Set the value in lp_sol matrix
			cache->lp_sol[lp_sol_table[col][1]][lp_sol_table[col][2]] = sol[nRows+col];
		}
		
	}

	delete_lp(lp);

	free(obj);
	free(row);
	free(rhs);
	free(lp_sol_table[1]);free(lp_sol_table);
	free(lp_sol_table_inv[1]);free(lp_sol_table_inv);
	free(sol);

	return jump;	
}
Exemple #4
0
int demoImplicit(void)
{
# if defined ERROR
#  undef ERROR
# endif
# define ERROR() { fprintf(stderr, "Error\n"); return(1); }
  lprec *lp;
  int majorversion, minorversion, release, build;
  char buf[1024];

  if ((lp = make_lp(0,4)) == NULL)
    ERROR();

  lp_solve_version(&majorversion, &minorversion, &release, &build);

  /* let's first demonstrate the logfunc callback feature */
  put_logfunc(lp, Mylogfunc, 0);
  sprintf(buf, "lp_solve %d.%d.%d.%d demo\n\n", majorversion, minorversion, release, build);
  print_str(lp, buf);
  solve(lp); /* just to see that a message is send via the logfunc routine ... */
  /* ok, that is enough, no more callback */
  put_logfunc(lp, NULL, 0);

  /* Now redirect all output to a file */
  /* set_outputfile(lp, "result.txt"); */

  /* set an abort function. Again optional */
  put_abortfunc(lp, Myctrlcfunc, 0);

  /* set a message function. Again optional */
  put_msgfunc(lp, Mymsgfunc, 0, MSG_PRESOLVE | MSG_LPFEASIBLE | MSG_LPOPTIMAL | MSG_MILPEQUAL | MSG_MILPFEASIBLE | MSG_MILPBETTER);

  printf("lp_solve %d.%d.%d.%d demo\n\n", majorversion, minorversion, release, build);
  printf("This demo will show most of the features of lp_solve %d.%d.%d.%d\n", majorversion, minorversion, release, build);
  press_ret();
  printf("\nWe start by creating a new problem with 4 variables and 0 constraints\n");
  printf("We use: lp=make_lp(0,4);\n");
  press_ret();

  printf("We can show the current problem with print_lp(lp)\n");
  print_lp(lp);
  press_ret();
  printf("Now we add some constraints\n");
  printf("str_add_constraint(lp, \"3 2 2 1\" ,LE,4)\n");
  printf("This is the string version of add_constraint. For the normal version\n");
  printf("of add_constraint see the help file.\n");
  if (!str_add_constraint(lp, "3 2 2 1", LE, 4))
    ERROR();
  print_lp(lp);
  press_ret();
  printf("str_add_constraint(lp, \"0 4 3 1\" ,GE,3)\n");
  if (!str_add_constraint(lp, "0 4 3 1", GE, 3))
    ERROR();
  print_lp(lp);
  press_ret();
  printf("Set the objective function\n");
  printf("str_set_obj_fn(lp, \"2 3 -2 3\")\n");
  if (!str_set_obj_fn(lp, "2 3 -2 3"))
    ERROR();
  print_lp(lp);
  press_ret();
  printf("Now solve the problem with printf(solve(lp));\n");
  printf("%d",solve(lp));
  press_ret();
  printf("The value is 0, this means we found an optimal solution\n");
  printf("We can display this solution with print_objective(lp) and print_solution(lp)\n");
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);

  press_ret();
  printf("The dual variables of the solution are printed with\n");
  printf("print_duals(lp);\n");
  print_duals(lp);
  press_ret();
  printf("We can change a single element in the matrix with\n");
  printf("set_mat(lp,2,1,0.5)\n");
  if (!set_mat(lp,2,1,0.5))
    ERROR();
  print_lp(lp);
  press_ret();
  printf("If we want to maximize the objective function use set_maxim(lp);\n");
  set_maxim(lp);
  print_lp(lp);
  press_ret();
  printf("after solving this gives us:\n");
  solve(lp);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  print_duals(lp);
  press_ret();
  printf("Change the value of a rhs element with set_rh(lp,1,7.45)\n");
  set_rh(lp,1,7.45);
  print_lp(lp);
  solve(lp);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("We change %s to the integer type with\n", get_col_name(lp, 4));
  printf("set_int(lp, 4, TRUE)\n");
  set_int(lp, 4, TRUE);
  print_lp(lp);
  printf("We set branch & bound debugging on with set_debug(lp, TRUE)\n");
  set_debug(lp, TRUE);
  printf("and solve...\n");
  press_ret();
  solve(lp);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("We can set bounds on the variables with\n");
  printf("set_lowbo(lp,2,2); & set_upbo(lp,4,5.3)\n");
  set_lowbo(lp,2,2);
  set_upbo(lp,4,5.3);
  print_lp(lp);
  press_ret();
  solve(lp);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("Now remove a constraint with del_constraint(lp, 1)\n");
  del_constraint(lp,1);
  print_lp(lp);
  printf("Add an equality constraint\n");
  if (!str_add_constraint(lp, "1 2 1 4", EQ, 8))
    ERROR();
  print_lp(lp);
  press_ret();
  printf("A column can be added with:\n");
  printf("str_add_column(lp,\"3 2 2\");\n");
  if (!str_add_column(lp,"3 2 2"))
    ERROR();
  print_lp(lp);
  press_ret();
  printf("A column can be removed with:\n");
  printf("del_column(lp,3);\n");
  del_column(lp,3);
  print_lp(lp);
  press_ret();
  printf("We can use automatic scaling with:\n");
  printf("set_scaling(lp, SCALE_MEAN);\n");
  set_scaling(lp, SCALE_MEAN);
  print_lp(lp);
  press_ret();
  printf("The function get_mat(lprec *lp, int row, int column) returns a single\n");
  printf("matrix element\n");
  printf("%s get_mat(lp,2,3), get_mat(lp,1,1); gives\n","printf(\"%f %f\\n\",");
  printf("%f %f\n", (double)get_mat(lp,2,3), (double)get_mat(lp,1,1));
  printf("Notice that get_mat returns the value of the original unscaled problem\n");
  press_ret();
  printf("If there are any integer type variables, then only the rows are scaled\n");
  printf("set_scaling(lp, SCALE_MEAN);\n");
  set_scaling(lp, SCALE_MEAN);
  printf("set_int(lp,3,FALSE);\n");
  set_int(lp,3,FALSE);
  print_lp(lp);
  press_ret();
  solve(lp);
  printf("print_objective, print_solution gives the solution to the original problem\n");
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("Scaling is turned off with unscale(lp);\n");
  unscale(lp);
  print_lp(lp);
  press_ret();
  printf("Now turn B&B debugging off and simplex tracing on with\n");
  printf("set_debug(lp, FALSE), set_trace(lp, TRUE) and solve(lp)\n");
  set_debug(lp, FALSE);
  set_trace(lp, TRUE);
  press_ret();
  solve(lp);
  printf("Where possible, lp_solve will start at the last found basis\n");
  printf("We can reset the problem to the initial basis with\n");
  printf("default_basis(lp). Now solve it again...\n");
  press_ret();
  default_basis(lp);
  solve(lp);

  printf("It is possible to give variables and constraints names\n");
  printf("set_row_name(lp,1,\"speed\"); & set_col_name(lp,2,\"money\")\n");
  if (!set_row_name(lp,1,"speed"))
    ERROR();
  if (!set_col_name(lp,2,"money"))
    ERROR();
  print_lp(lp);
  printf("As you can see, all column and rows are assigned default names\n");
  printf("If a column or constraint is deleted, the names shift place also:\n");
  press_ret();
  printf("del_column(lp,1);\n");
  del_column(lp,1);
  print_lp(lp);
  press_ret();

  delete_lp(lp);

/*
  printf("A lp structure can be created and read from a .lp file\n");
  printf("lp = read_LP(\"lp_examples/demo_lag.lp\", TRUE);\n");
  printf("The verbose option is used\n");
  if ((lp = read_LP("lp_examples/demo_lag.lp", TRUE, "test")) == NULL)
    ERROR();
  press_ret();
  printf("lp is now:\n");
  print_lp(lp);

  press_ret();
  printf("solution:\n");
  set_debug(lp, TRUE);
  solve(lp);
  set_debug(lp, FALSE);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("You can see that branch & bound was used in this problem\n");

  printf("Now remove the last constraint and use lagrangian relaxation\n");
  printf("del_constraint(lp,6);\n");
  printf("str_add_lag_con(lp, \"1 1 1 0 0 0\", LE, 2);\n");
  del_constraint(lp,6);
  if (!str_add_lag_con(lp, "1 1 1 0 0 0", LE, 2))
    ERROR();
  print_lp(lp);
*/

/*
  printf("Lagrangian relaxation is used in some heuristics. It is now possible\n");
  printf("to get a feasible integer solution without usage of branch & bound.\n");
  printf("Use lag_solve(lp, 0, 30); 0 is the initial bound, 30 the maximum\n");
  printf("number of iterations, the last variable turns the verbose mode on.\n");
  press_ret();
  set_lag_trace(lp, TRUE);
  printf("%d\n",lag_solve(lp, 0, 30));
  printf("The returncode of lag_solve is 6 or FEAS_FOUND. this means that a feasible\n");
  printf("solution has been found. For a list of other possible return values\n");
  printf("see the help file. Print this solution with print_objective, print_solution\n");
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);

  delete_lp(lp);
*/

  press_ret();

  return(0);
}
Exemple #5
0
int main(void)
{
  lprec *lp1,*lp2;
  FILE  *input_file;

  printf("lp_solve 2.0 demo by Jeroen J. Dirks ([email protected])\n\n");
  printf("This demo will show most of the features of lp_solve 2.0\n");
  press_ret();
  printf("\nWe start by creating a new problem with 4 variables and 0 constraints\n");
  printf("We use: lp1=make_lp(0,4);\n");
  lp1=make_lp(0,4);
  press_ret();
  printf("We can show the current problem with print_lp(lp1)\n");
  print_lp(lp1);
  press_ret();
  printf("Now we add some constraints\n");
  printf("str_add_constraint(lp1, \"3 2 2 1\" ,LE,4)\n");
  printf("This is the string version of add_constraint. For the normal version\n");
  printf("of add_constraint see the file lpkit.h\n");
  str_add_constraint(lp1, "3 2 2 1", LE, 4);
  print_lp(lp1);
  press_ret();
  printf("str_add_constraint(lp1, \"0 4 3 1\" ,GE,3)\n");
  str_add_constraint(lp1, "0 4 3 1", GE, 3);
  print_lp(lp1);
  press_ret();
  printf("Set the objective function\n");
  printf("str_set_obj_fn(lp1, \"2 3 -2 3\")\n");
  str_set_obj_fn(lp1, "2 3 -2 3");
  print_lp(lp1);
  press_ret();
  printf("Now solve the problem with printf(solve(lp1));\n");
  printf("%d",solve(lp1));
  press_ret();
  printf("The value is 0, this means we found an optimal solution\n");
  printf("We can display this solution with print_solution(lp1)\n");
  print_solution(lp1);
  press_ret();
  printf("The dual variables of the solution are printed with\n");
  printf("print_duals(lp1);\n");
  print_duals(lp1);
  press_ret();
  printf("We can change a single element in the matrix with\n");
  printf("set_mat(lp1,2,1,0.5)\n");
  set_mat(lp1,2,1,0.5);
  print_lp(lp1);
  press_ret();  
  printf("It we want to maximise the objective function use set_maxim(lp1);\n");
  set_maxim(lp1);
  print_lp(lp1);
  press_ret();
  printf("after solving this gives us:\n");
  solve(lp1);
  print_solution(lp1);
  print_duals(lp1);
  press_ret();
  printf("Change the value of a rhs element with set_rh(lp1,1,7.45)\n");
  set_rh(lp1,1,7.45);
  print_lp(lp1);
  solve(lp1);
  print_solution(lp1);
  press_ret();
  printf("We change Var[4] to the integer type with\n");
  printf("set_int(lp1, 4, TRUE)\n");
  set_int(lp1, 4, TRUE);
  print_lp(lp1);
  printf("We set branch & bound debugging on with lp1->debug=TRUE\n");
  lp1->debug=TRUE;
  printf("and solve...\n");
  press_ret();
  solve(lp1);
  print_solution(lp1);	
  press_ret();
  printf("We can set bounds on the variables with\n");
  printf("set_lowbo(lp1,2,2); & set_upbo(lp1,4,5.3)\n");
  set_lowbo(lp1,2,2);
  set_upbo(lp1,4,5.3);
  print_lp(lp1);
  press_ret();
  solve(lp1);
  print_solution(lp1);
  press_ret();
  printf("Now remove a constraint with del_constraint(lp1, 1)\n");
  del_constraint(lp1,1);
  print_lp(lp1);
  printf("Add an equality constraint\n");
  str_add_constraint(lp1, "1 2 1 4", EQ, 8);
  print_lp(lp1);
  press_ret();
  printf("A column can be added with:\n");
  printf("str_add_column(lp1,\"3 2 2\");\n");
  str_add_column(lp1,"3 2 2");
  print_lp(lp1);
  press_ret();
  printf("A column can be removed with:\n");
  printf("del_column(lp1,3);\n");
  del_column(lp1,3);
  print_lp(lp1);
  press_ret();
  printf("We can use automatic scaling with:\n");
  printf("auto_scale(lp1);\n");
  auto_scale(lp1);
  print_lp(lp1);
   press_ret();
  printf("The function mat_elm(lprec *lp, int row, int column) returns a single\n");
  printf("matrix element\n");
  printf("%s mat_elm(lp1,2,3), mat_elm(lp1,1,1); gives\n","printf(\"%f %f\\n\",");
  printf("%f %f\n",mat_elm(lp1,2,3), mat_elm(lp1,1,1));
  printf("Notice that mat_elm returns the value of the original unscaled problem\n");
  press_ret();
  printf("It there are any integer type variables, then only the rows are scaled\n");
  printf("set_int(lp1,3,FALSE);\n");
  printf("auto_scale(lp1);\n");
  set_int(lp1,3,FALSE);
  auto_scale(lp1);
  print_lp(lp1);
  press_ret();
  solve(lp1); 
  printf("print_solution gives the solution to the original problem\n");
  print_solution(lp1);
  press_ret();
  printf("Scaling is turned off with unscale(lp1);\n");
  unscale(lp1);
  print_lp(lp1);
  press_ret();
  printf("Now turn B&B debugging of and simplex tracing on with\n");
  printf("lp1->debug=FALSE, lp1->trace=TRUE and solve(lp1)\n");
  lp1->debug=FALSE;
  lp1->trace=TRUE;
  press_ret();
  solve(lp1);
  printf("Where possible, lp_solve will start at the last found basis\n");
  printf("We can reset the problem to the initial basis with\n");
  printf("reset_basis(lp1). Now solve it again...\n");
  press_ret();
  reset_basis(lp1);
  solve(lp1);
  press_ret();
  printf("It is possible to give variables and constraints names\n");
  printf("set_row_name(lp1,1,\"speed\"); & set_col_name(lp1,2,\"money\")\n");
  set_row_name(lp1,1,"speed");
  set_col_name(lp1,2,"money");
  print_lp(lp1);
  printf("As you can see, all column and rows are assigned default names\n");
  printf("If a column or constraint is deleted, the names shift place also:\n");
  press_ret(); 
  printf("del_column(lp1,1);\n");
  del_column(lp1,1);
  print_lp(lp1);
  press_ret();
  printf("A lp structure can be created and read from a .lp file\n");
  printf("input_file=fopen(\"lp_examples/demo_lag.lp\",\"r\");\n");
  printf("lp2 = read_lp_file(input_file, TRUE);\n");
  printf("The verbose option is used\n");
  input_file = fopen("lp_examples/demo_lag.lp", "r");
  if (input_file == NULL)
    {
      printf("Can't find demo_lag.lp, stopping\n");
      exit(EXIT_FAILURE);
    }
  lp2 = read_lp_file(input_file, TRUE, "test");
  press_ret();
  printf("lp2 is now:\n");
  print_lp(lp2);
  press_ret();
  printf("solution:\n");
  lp2->debug=TRUE; 
  solve(lp2);
  lp2->debug=FALSE;
  print_solution(lp2);
  press_ret();
  printf("You can see that branch & bound was used in this problem\n");
  printf("Now remove the last constraint and use lagrangian relaxation\n");
  printf("del_constraint(lp2,6);\n");
  printf("str_add_lag_con(lp2, \"1 1 1 0 0 0\", LE, 2);\n");
  del_constraint(lp2,6);
  str_add_lag_con(lp2, "1 1 1 0 0 0", LE, 2);
  print_lp(lp2);
  printf("Lagrangian relaxation is used in some heuristics. It is now possible\n");
  printf("to get a feasible integer soltution without usage of branch & bound.\n");
  printf("Use lag_solve(lp2, 0, 40, TRUE); 0 is the initial bound, 30 the maximum\n");
  printf("number of iterations, the last variable turns the verbose mode on.\n");
  press_ret();
  printf("%d\n",lag_solve(lp2, 0, 30, TRUE));
  printf("The returncode of lag_solve is 6 or FEAS_FOUND. this means that a feasible\n");
  printf("solution has been found. For a list of other possible return values\n");
  printf("see \"lpkit.h\". Print this solution with print_solution\n");
  print_solution(lp2);
  press_ret();

  return(0);
}
Exemple #6
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;
	}
Exemple #7
0
int main ( int argv, char * argc[] )
{

# if defined ERROR
#  undef ERROR
# endif
# define ERROR() { fprintf(stderr, "Error\n"); exit(1); }
  lprec *lp;
  int majorversion, minorversion, release, build;

#if defined FORTIFY
  Fortify_EnterScope();
#endif

  lp_solve_version(&majorversion, &minorversion, &release, &build);
  printf("lp_solve %d.%d.%d.%d demo\n\n", majorversion, minorversion, release, build);
  printf("This demo will show most of the features of lp_solve %d.%d.%d.%d\n", majorversion, minorversion, release, build);
  press_ret();
  printf("\nWe start by creating a new problem with 4 variables and 0 constraints\n");
  printf("We use: lp=make_lp(0,4);\n");
  if ((lp = make_lp(0,4)) == NULL)
    ERROR();
  press_ret();

  printf("We can show the current problem with print_lp(lp)\n");
  print_lp(lp);
  press_ret();
  printf("Now we add some constraints\n");
  printf("add_constraint(lp, {0, 3, 2, 2, 1}, LE, 4)\n");
  {
    double row[] = {0, 3, 2, 2, 1};
    if (!add_constraint(lp, row, LE, 4))
      ERROR();
  }
  print_lp(lp);
  press_ret();
  printf("add_constraintex is now used to add a row. Only the npn-zero values must be specfied with this call.\n");
  printf("add_constraintex(lp, 3, {4, 3, 1}, {2, 3, 4}, GE, 3)\n");
  {
    int colno[] = {2, 3, 4};
    double row[] = {4, 3, 1};
    if (!add_constraintex(lp, sizeof(colno) / sizeof(*colno), row, colno, GE, 3))
      ERROR();
  }
  print_lp(lp);
  press_ret();
  printf("Set the objective function\n");
  printf("set_obj_fn(lp, {0, 2, 3, -2, 3})\n");
  {
    double row[] = {0, 2, 3, -2, 3};
    if (!set_obj_fn(lp, row))
      ERROR();
  }
  print_lp(lp);
  press_ret();
  printf("Now solve the problem with printf(solve(lp));\n");
  printf("%d",solve(lp));
  press_ret();
  printf("The value is 0, this means we found an optimal solution\n");
  printf("We can display this solution with print_objective(lp) and print_solution(lp)\n");
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);

  press_ret();
  printf("The dual variables of the solution are printed with\n");
  printf("print_duals(lp);\n");
  print_duals(lp);
  press_ret();
  printf("We can change a single element in the matrix with\n");
  printf("set_mat(lp,2,1,0.5)\n");
  if (!set_mat(lp,2,1,0.5))
    ERROR();
  print_lp(lp);
  press_ret();
  printf("If we want to maximize the objective function use set_maxim(lp);\n");
  set_maxim(lp);
  print_lp(lp);
  press_ret();
  printf("after solving this gives us:\n");
  solve(lp);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  print_duals(lp);
  press_ret();
  printf("Change the value of a rhs element with set_rh(lp,1,7.45)\n");
  set_rh(lp,1,7.45);
  print_lp(lp);
  solve(lp);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("We change %s to the integer type with\n", get_col_name(lp, 4));
  printf("set_int(lp, 4, TRUE)\n");
  set_int(lp, 4, TRUE);
  print_lp(lp);
  printf("We set branch & bound debugging on with set_debug(lp, TRUE)\n");
  set_debug(lp, TRUE);
  printf("and solve...\n");
  press_ret();
  solve(lp);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("We can set bounds on the variables with\n");
  printf("set_lowbo(lp,2,2); & set_upbo(lp,4,5.3)\n");
  set_lowbo(lp,2,2);
  set_upbo(lp,4,5.3);
  print_lp(lp);
  press_ret();
  solve(lp);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("Now remove a constraint with del_constraint(lp, 1)\n");
  del_constraint(lp,1);
  print_lp(lp);
  printf("Add an equality constraint\n");
  {
    double row[] = {0, 1, 2, 1, 4};
    if (!add_constraint(lp, row, EQ, 8))
      ERROR();
  }
  print_lp(lp);
  press_ret();
  printf("A column can be added with:\n");
  printf("add_column(lp,{3, 2, 2});\n");
  {
    double col[] = {3, 2, 2};
    if (!add_column(lp, col))
      ERROR();
  }
  print_lp(lp);
  press_ret();
  printf("A column can be removed with:\n");
  printf("del_column(lp,3);\n");
  del_column(lp,3);
  print_lp(lp);
  press_ret();
  printf("We can use automatic scaling with:\n");
  printf("set_scaling(lp, SCALE_MEAN);\n");
  set_scaling(lp, SCALE_MEAN);
  print_lp(lp);
  press_ret();
  printf("The function get_mat(lprec *lp, int row, int column) returns a single\n");
  printf("matrix element\n");
  printf("%s get_mat(lp,2,3), get_mat(lp,1,1); gives\n","printf(\"%f %f\\n\",");
  printf("%f %f\n", (double)get_mat(lp,2,3), (double)get_mat(lp,1,1));
  printf("Notice that get_mat returns the value of the original unscaled problem\n");
  press_ret();
  printf("If there are any integer type variables, then only the rows are scaled\n");
  printf("set_scaling(lp, SCALE_MEAN);\n");
  set_scaling(lp, SCALE_MEAN);
  printf("set_int(lp,3,FALSE);\n");
  set_int(lp,3,FALSE);
  print_lp(lp);
  press_ret();
  solve(lp);
  printf("print_objective, print_solution gives the solution to the original problem\n");
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();
  printf("Scaling is turned off with unscale(lp);\n");
  unscale(lp);
  print_lp(lp);
  press_ret();
  printf("Now turn B&B debugging off and simplex tracing on with\n");
  printf("set_debug(lp, FALSE), set_trace(lp, TRUE) and solve(lp)\n");
  set_debug(lp, FALSE);
  set_trace(lp, TRUE);
  press_ret();
  solve(lp);
  printf("Where possible, lp_solve will start at the last found basis\n");
  printf("We can reset the problem to the initial basis with\n");
  printf("default_basis(lp). Now solve it again...\n");
  press_ret();
  default_basis(lp);
  solve(lp);

  printf("It is possible to give variables and constraints names\n");
  printf("set_row_name(lp,1,\"speed\"); & set_col_name(lp,2,\"money\")\n");
  if (!set_row_name(lp,1,"speed"))
    ERROR();
  if (!set_col_name(lp,2,"money"))
    ERROR();
  print_lp(lp);
  printf("As you can see, all column and rows are assigned default names\n");
  printf("If a column or constraint is deleted, the names shift place also:\n");
  press_ret();
  printf("del_column(lp,1);\n");
  del_column(lp,1);
  print_lp(lp);
  press_ret();

  write_lp(lp, "lp.lp");

  delete_lp(lp);

  printf("An lp structure can be created and read from a .lp file\n");
  printf("lp = read_lp(\"lp.lp\", TRUE);\n");
  printf("The verbose option is used\n");
  if ((lp = read_LP("lp.lp", TRUE, "test")) == NULL)
    ERROR();
  press_ret();
  printf("lp is now:\n");
  print_lp(lp);

  press_ret();
  printf("solution:\n");
  set_debug(lp, TRUE);
  solve(lp);
  set_debug(lp, FALSE);
  print_objective(lp);
  print_solution(lp, 1);
  print_constraints(lp, 1);
  press_ret();

  delete_lp(lp);

#if defined FORTIFY
  Fortify_LeaveScope();
#endif

    return 0;
}
void CLPLpsolve::setFunction(CLPFunction* function)
{
	m_status = 0;

	m_lpFunction = function;
	const int nVars = function->getNumCoefficients();

	// Creates an empty problem with getNumCoefficients variables
	m_env = make_lp(0, nVars);

	if(m_env == NULL)
	{	
		m_status = 1;
	}

	//if(!set_add_rowmode(m_env, FALSE))
	//{
	//	m_status = 1;
	//}
	
	//Allowing memory for rows
	//int * colno = new int[nVars];
    REAL * row= new REAL[nVars + 1];

	//set variables names
	for (int i = 0; i < nVars; ++i)
	{
		int lpIndex = i + 1;
		row[lpIndex] = function->getCoefficients().at(i);
		//colno[i] = lpIndex;

		

		//Determines the type
		switch(function->getIntegers().at(i)) {
			case 'C' :
				m_status = set_unbounded(m_env, lpIndex);
				break;

			case 'B' :
				m_status = set_binary(m_env, lpIndex, TRUE);
				break;

			case 'I' :
				m_status = set_int(m_env, lpIndex, TRUE);
				break;

			case 'S' :
				m_status = set_semicont(m_env, lpIndex, TRUE);
				break;

			default:
				assert(false);
				break;
		}
		
		//Sets upper bound and lower bound
		set_upbo(m_env, lpIndex, function->getUpperBounds().at(i));
		set_lowbo(m_env, lpIndex, function->getLowerBounds().at(i));
		set_col_name(m_env, lpIndex, const_cast<char*>(function->getVarNames().at(i).c_str()));

	}

	//set_obj_fnex(m_env, nVars, row, colno); 
	set_obj_fn(m_env, row);

	// Set the type of the problem (Min or Max)
	switch (function->getType()) {
	case lpMinFunction:
		set_minim(m_env);
		break;

	case lpMaxFunction:
		set_maxim(m_env);
		break;
	}

	if(!set_add_rowmode(m_env, TRUE))
	{
		m_status = 1;
	}
	
	delete [] row;

}