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); } }
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); }
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); } } }
void ssd(Vid* vid, Mat* out) { int nf = vid->nf; IplImage** imgs = vid->imgs; printf("Computing SSDs\n"); for (int j = 0; j < nf; j++) { for (int i = j + 1; i < nf; i++) { double v = ssd(imgs[i], imgs[j]); set_mat(out, i, j, v); set_mat(out, j, i, v); } set_mat(out, j, j, 0.0); } }
pyne::Material pyne::Material::set_mat (std::set<std::string> nucset, double value, std::string n) { // Sets a substream from this stream based on a set of strings. // Strings can be of any form. std::set<int> iset; for (std::set<std::string>::iterator i = nucset.begin(); i != nucset.end(); i++) { iset.insert(pyne::nucname::zzaaam(*i)); }; return set_mat(iset, value, n); };
/* fill in element (Row,Column) of the matrix Row in [0..Rows] and Column in [1..Columns] */ long __declspec(dllexport) WINAPI _set_mat(lprec *lp, long row, long column, double value) { long ret; if (lp != NULL) { freebuferror(); ret = set_mat(lp, row, column, value); } else ret = 0; return(ret); }
void null_matrix(matrix_t *mat, int r) { int i = 0, j = 0; mat->m = r; mat->n = r; alloc_matrix(mat); for (i = 0; i<mat->m; i++) { for (j = 0; j<mat->n; j++) { set_mat(*mat, i, j, (float)0); } } }
void random_matrix(matrix_t *mat, int r) { int i = 0, j = 0; mat->m = r; mat->n = r; alloc_matrix(mat); for (i = 0; i<mat->m; i++) { for (j = 0; j<mat->n; j++) { set_mat(*mat, i, j, ((float)(((float)(rand()%100))/(float)(rand()%50))) ); } } }
matrix_t set_zero(matrix_t mat) { //Assuming Square matrix will add the check latter int row_len = mat.row_end - mat.row_start + 1; int column_len = mat.column_end - mat.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, i, j, 0); } } return mat; }
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; }
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; }
int py_shader_param_set(PyShaderParamObject *self, PyObject *val) { const struct ShaderParam *p = self->param; int ok = 0; if (PyObject_TypeCheck(val, &py_mat_type)) { ok = set_mat(p, val); } else if (PyObject_TypeCheck(val, &py_vec_type)) { ok = set_vec(p, val); } else if (PyFloat_Check(val)) { ok = set_float(p, val); } else if (PyLong_Check(val)) { ok = set_int(p, val); } else if (PyObject_TypeCheck(val, &py_array_type)) { ok = set_array(p, (PyArrayObject*)val); } else { PyErr_Format( PyExc_TypeError, "unsupported type '%s' for shader param '%s'", Py_TYPE(val)->tp_name, p->name ); return 0; } if (!ok) { PyErr_Format( PyExc_RuntimeError, "failed to set shader param '%s'", p->name ); error_print_tb(); error_clear(); return 0; } return 1; }
int read_nblist(FILE *in,FILE *fp,int **mat,int natoms,bool bSymm) { bool bNL; char buf[256],b1[32],b2[32],solv[256],il_code[256]; int i,ii,j,nnbl,full,icmp,nri,isolv; int iatom,nrj,nj,shift,gid,nargs,njtot=0; do { if (fgets2(buf,255,in) == NULL) gmx_fatal(FARGS,"EOF when looking for '%s' in logfile",header); } while (strstr(buf,header) == NULL); do { do { if (fgets2(buf,255,in) == NULL) return njtot; } while (strstr(buf,"nri:") == NULL); if (0) { if ((nargs = sscanf(buf,"%*s%s%*s%s",il_code,solv)) != 2) { fprintf(stderr,"Can not find the right il_code\n"); return njtot; } for(isolv=0; (isolv<esolNR); isolv++) if (strstr(esol_names[isolv],solv) != NULL) break; if (isolv == esolNR) { fprintf(stderr,"Can not read il_code or solv (nargs=%d)\n",nargs); return njtot; } } else isolv = enlistATOM; /* gmx_fatal(FARGS,"Can not read il_code or solv (nargs=%d)",nargs);*/ if ((nargs = sscanf(buf,"%*s%d%*s%d",&nri,&nrj)) != 2) gmx_fatal(FARGS,"Can not read nri or nrj (nargs=%d)",nargs); for(ii=0; (ii<nri); ii++) { if ((nargs = fscanf(in,"%*s%d%*s%d%*s%d%*s%d", &iatom,&shift,&gid,&nj)) != 4) gmx_fatal(FARGS,"Can not read iatom, shift gid or nj (nargs=%d)",nargs); /* Number shifts from 1 to 27 iso 0 to 26 to distinguish uninitialized * matrix elements. */ range_check(iatom,0,natoms); for(i=0; (i<nj); i++) { if ((nargs = fscanf(in,"%*s%d",&j)) != 1) gmx_fatal(FARGS,"Can not read j"); range_check(j,0,natoms); switch (isolv) { case enlistATOM: set_mat(fp,mat,iatom,1,j,1,bSymm,shift); njtot++; break; case enlistWATER: set_mat(fp,mat,iatom,3,j,1,bSymm,shift); njtot+=3; break; case enlistWATERWATER: set_mat(fp,mat,iatom,3,j,3,bSymm,shift); njtot+=9; break; default: gmx_incons("non-existing solvent type"); } } } fprintf(fp,"nri = %d nrj = %d\n",nri,nrj); } while (TRUE); return -1; }
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; }
int CLPLpsolve::changeCoefs(int rowindex, int varIndex, double coef) { set_mat(m_env, rowindex + 1, varIndex + 1, coef); return 1; }
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); }
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); }
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)); }