void prob_map(Mat* in, Mat* out, double factor)
{
    printf("Computing probability map\n");
    double vmean = 0;
    double vmin = -1;
    double vmax = -1;
    for (int j = 0; j < in->m; j++)
    {
        for (int i = 0; i < in->n; i++)
        {
            if (i == j) continue;

            double v = get_mat(in, i, j);
            vmean += v;

            if (vmin == -1)
                vmin = vmax = v;

            vmin = fmin(vmin, v);
            vmax = fmax(vmax, v);
        }
    }

    vmean /= in->m * in->n;
    printf("...Mean non-zero ssd = %f\n", vmean);
    printf("...Min non-zero ssd = %f\n", vmin);
    printf("...Max non-zero ssd = %f\n", vmax);

    double sigma = factor * vmean;
    printf("...Sigma = %f\n", sigma);

    // Compute probabilities
    for (int i = 0; i < in->n - 1; i++)
    {
        for (int j = 0; j < in->m; j++)
        {
            double v = get_mat(in, i + 1, j);
            double p = exp(-v / sigma);
            set_mat(out, i, j, p);
        }
    }

    // Avoid transitions to the last frame
    for (int j = 0; j < in->m; j++)
        set_mat(out, in->n - 1, j, 0);

    for (int i = 0; i < in->n; i++)
        set_mat(out, i, in->m - 1, 0);

    // Normalize
    for (int i = 0; i < in->n; i++)
    {
        double sum = 0;
        for (int j = 0; j < in->m; j++)
            sum += get_mat(out, i, j);

        for (int j = 0; j < in->m; j++)
            set_mat(out, i, j, get_mat(out, i, j) / sum);
    }
}
Exemple #2
0
PyObject *Bar_GetMatrix(BarObject *self) {
  int nnz, i;
  PyObject *retval;
  int (*get_mat)(glp_prob*,int,int[],double[]);

  if (!Bar_Valid(self, 1)) return NULL;

  get_mat = Bar_Row(self) ? glp_get_mat_row : glp_get_mat_col;
  i = Bar_Index(self)+1;
  nnz = get_mat(LP, i, NULL, NULL);
  retval = PyList_New(nnz);
  if (nnz==0 || retval==NULL) return retval;

  int*ind = (int*)calloc(nnz,sizeof(int));
  double*val = (double*)calloc(nnz,sizeof(double));
  nnz = get_mat(LP, i, ind-1, val-1);

  for (i=0; i<nnz; ++i) {
    PyList_SET_ITEM(retval, i, Py_BuildValue("id", ind[i]-1, val[i]));
  }
  free(ind);
  free(val);
  if (PyList_Sort(retval)) {
    Py_DECREF(retval);
    return NULL;
  }
  return retval;
}
int test(Mat* m)
{
    int n = m->n;
    int j0 = m->n / 2;

    Elem* elems = (Elem*) malloc(n * sizeof(Elem));
    for (int i = 0; i < n; i++)
    {
        double v = get_mat(m, i, j0);
        elems[i].key = v;
        elems[i].id = i;
    }

    printf("index map:\n");
    for (int j = 0; j < 10; j++)
    {
        for (int i = 0; i <= j; i++)
        {
            int ii = fmax(i, j);
            int jj = fmin(i, j);
            int idx = jj + ii * (ii + 1) / 2;
            printf("(i, j) = (%d, %d), idx = %d\n", i, j, idx);
        }
    }

    printf("prob map:\n");
    for (int j = 0; j < 10; j++)
    {
        for (int i = 0; i <= j; i++)
        {
            printf("%.2f ", get_mat(m, i, j));
        }
        printf("\n");
    }

    printf("original distribution:\n");
    for (int i = 0; i < n; i++)
        printf("key = %f, id = %d:\n", elems[i].key, elems[i].id);

    quicksort(elems, 0, n-1);
    printf("sorted distribution:\n");
    for (int i = 0; i < n; i++)
        printf("key = %f, id = %d:\n", elems[i].key, elems[i].id);

    reverse(elems, n);
    printf("reverse-sorted distribution:\n");
    for (int i = 0; i < n; i++)
        printf("key = %f, id = %d:\n", elems[i].key, elems[i].id);

    ncumsum(elems, n);
    printf("cumulative distribution:\n");
    for (int i = 0; i < n; i++)
        printf("key = %f, id = %d:\n", elems[i].key, elems[i].id);

    free(elems);
}
Exemple #4
0
/* return the id of a new population */
int disperse(struct pathogen * pathogen, struct dispmat *disp, struct param *par){
	int i=1, k=disp->n, popid = get_popid(pathogen);
	double cumprob=get_mat(disp)[popid][0];
	double x=gsl_rng_uniform(par->rng); /* nb between 0 and 1 */

	while(x > cumprob && i<k){
		cumprob += get_mat(disp)[popid][i++];
	}
	return i-1;
}
void deadend_filt(Mat* in, Mat* out, double alpha, int p, double thresh)
{
    printf("Applying dead-end filter\n");
    printf("...Alpha = %f\n", alpha);
    printf("...p = %d\n", p);
    printf("...Threshold = %f\n", thresh);
    int n = in->n;
    int m = in->m;
    double* ms = (double*) malloc(n * sizeof(double));

    // Initialize
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            set_mat(out, i, j, pow(get_mat(in, i, j), p));
    }

    // Iterate
    double cmax = -1;
    int iters = 0;
    while(cmax == -1 || cmax > thresh)
    {
        for (int i = 0; i < n; i++)
        {
            double dmin = get_mat(out, i, 0);
            for (int j = 0; j < m; j++)
            {
                if (j != i)
                    dmin = fmin(dmin, get_mat(out, i, j));
            }
            ms[i] = dmin;
        }

        for (int i = n - 1; i >= 0; i--)
        {
            cmax = 0;
            for (int j = 0; j < m; j++)
            {
                double ta = pow(get_mat(in, i, j), p);
                double tb = alpha * ms[j];
                double next = ta + tb;
                double prev = get_mat(out, i, j);
                double diff = abs(next - prev);
                cmax = fmax(cmax, diff);
                set_mat(out, i, j, next);
            }
        }
        iters++;
    }
    printf("...Corrected for dead-ends after %d iterations\n", iters);
}
Exemple #6
0
matrix_t matrix_subtraction(matrix_t mat_a, matrix_t mat_b,
			    matrix_t mat_c)
{
    //Assuming Square matrix will add the check latter
    int row_len = mat_c.row_end - mat_c.row_start + 1;
    int column_len = mat_c.column_end - mat_c.column_start + 1;
    int i = 0, j = 0;
    for (i = 0; i<row_len; i++) {
	for (j = 0; j<column_len; j++) {
	    set_mat(mat_c, i, j,
		    get_mat(mat_a, i, j)-get_mat(mat_b, i, j));
	}
    }
    return mat_c;
}
void diag_filt(Mat* in, Mat* out)
{
    printf("Applying diagonal filter\n");
    int n = in->n;
    int m = in->m;
    int d = 1;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            double c = 0;
            double v = 0;
            for (int k = -d; k < d; k++)
            {
                int di = i + k; 
                int dj = j + k;
                if (di >= 0 && di < n && dj >= 0 && dj < m)
                {
                    v += get_mat(in, i + k, j + k); 
                    c++;
                }
            }
            set_mat(out, i, j, v / c);
        }
    }
}
Exemple #8
0
matrix_t matrix_multiplication(matrix_t mat_a, matrix_t mat_b,
			       matrix_t mat_c)
{
    int i = 0, j = 0, k = 0;
    int limit = mat_c.row_end - mat_c.row_start;
    /* if (validate_matrix(mat_a, mat_b, mat_c) == FALSE) { */
    /* 	return mat_c; */
    /* } */
    for(i=0; i<=limit; i++) {
	for(j=0; j<=limit; j++) {
	    for (k = 0; k<=limit; k++) {
		set_mat(mat_c, i, j,
			get_mat(mat_c, i, j)
			+ (get_mat(mat_a, i, k)
			   *get_mat(mat_b, k, j)));
	    }
	}
    }
    return mat_c;
}
Exemple #9
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	float  *y0=0,  *y1=0,  *y2=0, *iy0=0, *iy1=0, *iy2=0;
	int dim_g[3], dim_f[3];
	REAL U[4][3], V[4][3];

        if (nrhs != 6 || nlhs > 3) mexErrMsgTxt("Incorrect usage.");

	y0 = get_volume(prhs[0], dim_g);
	y1 = get_volume(prhs[1], dim_f);
	if (dim_g[0] != dim_f[0] || dim_g[1] != dim_f[1] || dim_g[2] != dim_f[2])
		mexErrMsgTxt("Incompatible dimensions.");
	y2 = get_volume(prhs[2], dim_f);
	if (dim_g[0] != dim_f[0] || dim_g[1] != dim_f[1] || dim_g[2] != dim_f[2])
		mexErrMsgTxt("Incompatible dimensions.");

	if (!mxIsNumeric(prhs[3]) || mxIsComplex(prhs[3]) ||
		mxIsComplex(prhs[3]) || !mxIsDouble(prhs[3]) || mxGetM(prhs[3]) * mxGetN(prhs[3]) != 3)
		mexErrMsgTxt("Output dimensions must be numeric, real, full, double and contain three elements.");

	dim_f[0] = mxGetPr(prhs[3])[0];
	dim_f[1] = mxGetPr(prhs[3])[1];
	dim_f[2] = mxGetPr(prhs[3])[2];

	get_mat(prhs[4],U);
	get_mat(prhs[5],V);

	plhs[0] = mxCreateNumericArray(3, dim_f,mxSINGLE_CLASS,mxREAL);
	plhs[1] = mxCreateNumericArray(3, dim_f,mxSINGLE_CLASS,mxREAL);
	plhs[2] = mxCreateNumericArray(3, dim_f,mxSINGLE_CLASS,mxREAL);

	iy0 = (float *)mxGetPr(plhs[0]);
	iy1 = (float *)mxGetPr(plhs[1]);
	iy2 = (float *)mxGetPr(plhs[2]);

	setnan(iy0, dim_f[0]*dim_f[1]*dim_f[2]);
	setnan(iy1, dim_f[0]*dim_f[1]*dim_f[2]);
	setnan(iy2, dim_f[0]*dim_f[1]*dim_f[2]);

	invert_field(dim_g, y0, y1, y2, dim_f, iy0, iy1, iy2, U, V);
}
STATIC int write_lprow(lprec *lp, int rowno, void *userhandle, write_modeldata_func write_modeldata, int maxlen)
{
  int     i, ie, j, nchars;
  REAL    a;
  MATrec  *mat = lp->matA;
  MYBOOL  first = TRUE, elements;

  if(rowno == 0) {
    i = 1;
    ie = lp->columns+1;
  }
  else {
    i = mat->row_end[rowno-1];
    ie = mat->row_end[rowno];
  }
  elements = ie - i;
  if(write_modeldata != NULL) {
    nchars = 0;
    for(; i < ie; i++) {
      if(rowno == 0) {
	j = i;
	a = get_mat(lp, 0, i);
	if(a == 0)
	  continue;
      }
      else {
	j = ROW_MAT_COLNR(i);
	a = ROW_MAT_VALUE(i);
	a = my_chsign(is_chsign(lp, rowno), a);
	a = unscaled_mat(lp, a, rowno, j);
      }
      if(is_splitvar(lp, j))
	continue;
      if(!first)
	nchars += write_data(userhandle, write_modeldata, " ");
      else
	first = FALSE;
      if(a == -1)
	nchars += write_data(userhandle, write_modeldata, "-");
      else if(a == 1)
	nchars += write_data(userhandle, write_modeldata, "+");
      else
	nchars += write_data(userhandle, write_modeldata, "%+.12g ", (double)a);
      nchars += write_data(userhandle, write_modeldata, "%s", get_col_name(lp, j));
      /* Check if we should add a linefeed */
      if((maxlen > 0) && (nchars >= maxlen) && (i < ie-1)) {
	write_data(userhandle, write_modeldata, "%s", "\n");
	nchars = 0;
      }
    }
  }
  return(elements);
}
Exemple #11
0
/* Printing of sensitivity analysis reports */
void REPORT_extended(lprec *lp)
{
  int  i, j;
  LPSREAL hold;
  LPSREAL *duals, *dualsfrom, *dualstill, *objfrom, *objtill;
  MYBOOL ret;

  ret = get_ptr_sensitivity_obj(lp, &objfrom, &objtill);
  report(lp, NORMAL, " \n");
  report(lp, NORMAL, "Primal objective:\n");
  report(lp, NORMAL, " \n");
  report(lp, NORMAL, "  Column name                      Value   Objective         Min         Max\n");
  report(lp, NORMAL, "  --------------------------------------------------------------------------\n");
  for(j = 1; j <= lp->columns; j++) {
    hold = get_mat(lp,0,j);
    report(lp, NORMAL, "  %-25s " MPSVALUEMASK MPSVALUEMASK MPSVALUEMASK MPSVALUEMASK "\n",
           get_col_name(lp,j),
           my_precision(hold,lp->epsprimal),
           my_precision(hold*lp->best_solution[lp->rows+j],lp->epsprimal),
           my_precision((ret) ? objfrom[j - 1] : 0.0,lp->epsprimal),
           my_precision((ret) ? objtill[j - 1] : 0.0,lp->epsprimal));
  }
  report(lp, NORMAL, " \n");

  ret = get_ptr_sensitivity_rhs(lp, &duals, &dualsfrom, &dualstill);
  report(lp, NORMAL, "Primal variables:\n");
  report(lp, NORMAL, " \n");
  report(lp, NORMAL, "  Column name                      Value       Slack         Min         Max\n");
  report(lp, NORMAL, "  --------------------------------------------------------------------------\n");
  for(j = 1; j <= lp->columns; j++)
    report(lp, NORMAL, "  %-25s " MPSVALUEMASK MPSVALUEMASK MPSVALUEMASK MPSVALUEMASK "\n",
           get_col_name(lp,j),
           my_precision(lp->best_solution[lp->rows+j],lp->epsprimal),
           my_precision(my_inflimit(lp, (ret) ? duals[lp->rows+j-1] : 0.0),lp->epsprimal),
           my_precision((ret) ? dualsfrom[lp->rows+j-1] : 0.0,lp->epsprimal),
           my_precision((ret) ? dualstill[lp->rows+j-1] : 0.0,lp->epsprimal));

  report(lp, NORMAL, " \n");
  report(lp, NORMAL, "Dual variables:\n");
  report(lp, NORMAL, " \n");
  report(lp, NORMAL, "  Row name                         Value       Slack         Min         Max\n");
  report(lp, NORMAL, "  --------------------------------------------------------------------------\n");
  for(i = 1; i <= lp->rows; i++)
    report(lp, NORMAL, "  %-25s " MPSVALUEMASK MPSVALUEMASK MPSVALUEMASK MPSVALUEMASK "\n",
           get_row_name(lp,i),
           my_precision((ret) ? duals[i - 1] : 0.0, lp->epsprimal),
           my_precision(lp->best_solution[i], lp->epsprimal),
           my_precision((ret) ? dualsfrom[i - 1] : 0.0,lp->epsprimal),
           my_precision((ret) ? dualstill[i - 1] : 0.0,lp->epsprimal));

  report(lp, NORMAL, " \n");
}
Exemple #12
0
void print_matrix(matrix_t mat)
{
    int i = 0, j = 0;
    int m = mat.row_end-mat.row_start;
    int n = mat.column_end-mat.column_start;

    for (i = 0; i<=m; i++) {
	for (j = 0; j<=n; j++) {
	    printf("%-14f ", get_mat(mat, i, j));
	}
	printf("\n");
    }
}
void write_mat(Mat* m, const char* fn)
{
    printf("Writing matrix to: %s\n", fn);
    FILE *fp = fopen(fn, "w");
    for (int i = 0; i < m->n; i++)
    {
        for (int j = 0; j < m->m; j++)
        {
            fprintf(fp, "%f ", get_mat(m, i, j));
        }
        fprintf(fp, "\n");
    }
    fclose(fp);
}
Exemple #14
0
/* List the current basis matrix columns over the selected row range */
void blockWriteBMAT(FILE *output, const char *label, lprec* lp, int first, int last)
{
  int    i, j, jb, k = 0;
  double hold;

  if(first < 0)
    first = 0;
  if(last < 0)
    last = lp->rows;

  fprintf(output, "%s", label);
  fprintf(output, "\n");

  for(i = first; i <= last; i++) {
    for(j = 1; j <= lp->rows; j++) {
      jb = lp->var_basic[j];
      if(jb <= lp->rows) {
        if(jb == i)
          hold = 1;
        else
          hold = 0;
      }
      else
        hold = get_mat(lp, i, j);
      if(i == 0)
        modifyOF1(lp, jb, &hold, 1);
      hold = unscaled_mat(lp, hold, i, jb);
      fprintf(output, " %18g", hold);
      k++;
      if(my_mod(k, 4) == 0) {
        fprintf(output, "\n");
        k = 0;
      }
    }
    if(my_mod(k, 4) != 0) {
      fprintf(output, "\n");
      k = 0;
    }
  }
  if(my_mod(k, 4) != 0)
    fprintf(output, "\n");
}
Exemple #15
0
void rs_dec(unsigned char rx[],unsigned char decode[],unsigned char err_pos[],int num_err)
{
	int i;
	int j;
	char error[]="too many error postions!\n";
	if(num_err>PARA_N_K)
	{
		memcpy(decode,error,sizeof(error));
	}
	get_mat(Ainv,err_pos,num_err);
	for (i=0;i<PARA_FRAMESIZE;i++)
	{
		
		for (j=0;j<PARA_N1;j++)
		{
			d0[j]=rx[j*PARA_FRAMESIZE+i];
		}
		for (j=0;j<num_err;j++)
		{
			d0[err_pos[j]]=0;
		}
		for (j=0;j<num_err;j++)
		{
			s[j]=gf_sub(d0,( unsigned char )(j+1));
		}
		gf_mat_mult(Ainv,s,err_value,num_err);
		for (j=0;j<num_err;j++)
		{
			d0[err_pos[j]]=err_value[j];
		}
		for (j=0;j<PARA_N1;j++)
		{
			decode[j*PARA_FRAMESIZE+i]=d0[j];
		}
		
	}
}
Exemple #16
0
/* A more readable lp-format report of the model; antiquated and not updated */
void REPORT_lp(lprec *lp)
{
  int  i, j;

  if(lp->outstream == NULL)
    return;

  fprintf(lp->outstream, "Model name: %s\n", get_lp_name(lp));
  fprintf(lp->outstream, "          ");

  for(j = 1; j <= lp->columns; j++)
    fprintf(lp->outstream, "%8s ", get_col_name(lp,j));

  fprintf(lp->outstream, "\n%simize  ", (is_maxim(lp) ? "Max" : "Min"));
  for(j = 1; j <= lp->columns; j++)
      fprintf(lp->outstream, "%8g ", get_mat(lp, 0, j));
  fprintf(lp->outstream, "\n");

  for(i = 1; i <= lp->rows; i++) {
    fprintf(lp->outstream, "%-9s ", get_row_name(lp, i));
    for(j = 1; j <= lp->columns; j++)
      fprintf(lp->outstream, "%8g ", get_mat(lp, i, j));
    if(is_constr_type(lp, i, GE))
      fprintf(lp->outstream, ">= ");
    else if(is_constr_type(lp, i, LE))
      fprintf(lp->outstream, "<= ");
    else
      fprintf(lp->outstream, " = ");
    fprintf(lp->outstream, "%8g", get_rh(lp, i));

    if(is_constr_type(lp, i, GE)) {
      if(get_rh_upper(lp, i) < lp->infinite)
        fprintf(lp->outstream, "  %s = %8g", "upbo", get_rh_upper(lp, i));
    }
    else if(is_constr_type(lp, i, LE)) {
      if(get_rh_lower(lp, i) > -lp->infinite)
        fprintf(lp->outstream, "  %s = %8g", "lowbo", get_rh_lower(lp, i));
    }
    fprintf(lp->outstream, "\n");
  }

  fprintf(lp->outstream, "Type      ");
  for(i = 1; i <= lp->columns; i++) {
    if(is_int(lp,i))
      fprintf(lp->outstream, "     Int ");
    else
      fprintf(lp->outstream, "    Real ");
  }

  fprintf(lp->outstream, "\nupbo      ");
  for(i = 1; i <= lp->columns; i++)
    if(get_upbo(lp, i) >= lp->infinite)
      fprintf(lp->outstream, "     Inf ");
    else
      fprintf(lp->outstream, "%8g ", get_upbo(lp, i));
  fprintf(lp->outstream, "\nlowbo     ");
  for(i = 1; i <= lp->columns; i++)
    if(get_lowbo(lp, i) <= -lp->infinite)
      fprintf(lp->outstream, "    -Inf ");
    else
      fprintf(lp->outstream, "%8g ", get_lowbo(lp, i));
  fprintf(lp->outstream, "\n");

  fflush(lp->outstream);
}
Exemple #17
0
/* List the current user data matrix columns over the selected row range */
void blockWriteAMAT(FILE *output, const char *label, lprec* lp, int first, int last)
{
  int    i, j, k = 0;
  int    nzb, nze, jb;
  double hold;
  MATrec *mat = lp->matA;

  if(!mat_validate(mat))
    return;
  if(first < 0)
    first = 0;
  if(last < 0)
    last = lp->rows;

  fprintf(output, "%s", label);
  fprintf(output, "\n");

  if(first == 0) {
    for(j = 1; j <= lp->columns; j++) {
      hold = get_mat(lp, 0, j);
      fprintf(output, " %18g", hold);
      k++;
      if(my_mod(k, 4) == 0) {
        fprintf(output, "\n");
        k = 0;
      }
    }
    if(my_mod(k, 4) != 0) {
      fprintf(output, "\n");
      k = 0;
    }
    first++;
  }
  nze = mat->row_end[first-1];
  for(i = first; i <= last; i++) {
    nzb = nze;
    nze = mat->row_end[i];
    if(nzb >= nze)
      jb = lp->columns+1;
    else
      jb = ROW_MAT_COLNR(nzb);
    for(j = 1; j <= lp->columns; j++) {
      if(j < jb)
        hold = 0;
      else {
        hold = get_mat(lp, i, j);
        nzb++;
        if(nzb < nze)
          jb = ROW_MAT_COLNR(nzb);
        else
          jb = lp->columns+1;
      }
      fprintf(output, " %18g", hold);
      k++;
      if(my_mod(k, 4) == 0) {
        fprintf(output, "\n");
        k = 0;
      }
    }
    if(my_mod(k, 4) != 0) {
      fprintf(output, "\n");
      k = 0;
    }
  }
  if(my_mod(k, 4) != 0)
    fprintf(output, "\n");
}
Exemple #18
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 #19
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 copy_mat(Mat* in, Mat* out)
{
    for (int i = 0; i < in->n; i++)
        for (int j = 0; j < in->m; j++)
            set_mat(out, i, j, get_mat(in, i, j));
}