📄 lp2mat.c
字号:
/* This file contains a function to interface LP_SOLVE to MATLAB. This file is not part of the LP_SOLVE package. Marian V. Iordache (miordach@nd.edu)*/#include "lpkit.h"#include <stdlib.h>#include <stdio.h>void lp2mat(REAL* f, REAL* L, REAL* b, int m, int n, REAL* intlist, REAL* ub, REAL* lb, REAL* ctype, REAL* kind, REAL* solution) {/* In principle the following type of program is considered: min f'x such that Lx <= b, ub >= x >= lb, x >= 0, and x[i] is integer for all i such that intlist[i] != 0 However each row may specify a different type of constraint: <=, =, or >=. Thus we have: L(i,:)x <= b(i) for ctype[i] = -1 L(i,:)x = b(i) for ctype[i] = 0 L(i,:)x >= b(i) for ctype[i] = 1 m is the number of rows of L and n the number of columns. In reality L is represnted as a vector, not a matrix. It has m*n elements taken column by column from the corresponding m x n matrix. Use ub[i] < 0 if x[i] has an infinite upper bound. 'solution' is the optimal x; 'kind' is initialized to the kind of termination: OPTIMAL, INFEASIBLE, UNBOUNDED or FAILURE. */lprec* lp;int i,j;REAL** mat, *tf;lp = make_lp(0, n);set_minim(lp);mat = calloc(m, sizeof(REAL*));tf = calloc(n+1, sizeof(REAL)); for (i=0; i < n; i++) tf[i+1] = f[i];set_obj_fn(lp, tf); for (i=0; i < m; i++) { mat[i] = calloc(n+1, sizeof(REAL)); for (j=0; j < n; j++) { mat[i][j+1] = L[i+j*m]; } if (ctype[i] < 0) add_constraint(lp, mat[i], LE, b[i]); else if (ctype[i] == 0) add_constraint(lp, mat[i], EQ, b[i]); else add_constraint(lp, mat[i], GE, b[i]); } for (j = 0; j < n; j++) { set_lowbo(lp, j+1, lb[j]); if (ub[j] >= 0) set_upbo(lp, j+1, ub[j]); if (intlist[j]) set_int(lp, j+1, TRUE); } /* lp->debug = TRUE; lp->trace = TRUE;*/ /* print_lp(lp); */*kind = (REAL)solve(lp); for(i = 1; i <= lp->columns; i++) { solution[i-1] = lp->best_solution[lp->rows + i]; /* printf("%g and %g\n",lp->best_solution[i], lp->best_solution[lp->rows + i]);*/ }delete_lp(lp); for(i = 0; i < m; i++) free(mat[i]);free(mat); free(tf);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -