⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lpkit.c

📁 linux下的源码分类器SVM
💻 C
📖 第 1 页 / 共 4 页
字号:
	addtoo[i] = TRUE;	lp->non_zeros++;      }      else	addtoo[i] = FALSE;  MALLOC(newmat, lp->non_zeros);  inc_mat_space(lp, 0);  lp->rows++;  lp->sum++;  inc_row_space(lp);  if(lp->scaling_used) {    /* shift scale */    for(i = lp->sum; i > lp->rows; i--)      lp->scale[i] = lp->scale[i - 1];    lp->scale[lp->rows] = 1;  }  if(lp->names_used)    sprintf(lp->row_name[lp->rows], "r_%d", lp->rows);  if(lp->scaling_used && lp->columns_scaled)    for(i = 1; i <= lp->columns; i++)      row[i] *= lp->scale[lp->rows + i];       if(constr_type == GE)    lp->ch_sign[lp->rows] = TRUE;  else    lp->ch_sign[lp->rows] = FALSE;  elmnr = 0;  stcol = 0;  for(i = 1; i <= lp->columns; i++) {    for(j = stcol; j < lp->col_end[i]; j++) {        newmat[elmnr].row_nr = lp->mat[j].row_nr;      newmat[elmnr].value = lp->mat[j].value;      elmnr++;    }    if(addtoo[i]) {      if(lp->ch_sign[lp->rows])	newmat[elmnr].value = -row[i];      else	newmat[elmnr].value = row[i];      newmat[elmnr].row_nr = lp->rows;      elmnr++;    }    stcol = lp->col_end[i];    lp->col_end[i] = elmnr;  }        memcpy(lp->mat, newmat, lp->non_zeros * sizeof(matrec));   free(newmat);  free(addtoo);  for(i = lp->sum; i > lp->rows; i--) {    lp->orig_upbo[i]   = lp->orig_upbo[i - 1];    lp->orig_lowbo[i]  = lp->orig_lowbo[i - 1];    lp->basis[i]       = lp->basis[i - 1];    lp->lower[i]       = lp->lower[i - 1];    lp->must_be_int[i] = lp->must_be_int[i - 1];  }  /* changed from i <= lp->rows to i < lp->rows, MB */  for(i = 1 ; i < lp->rows; i++)    if(lp->bas[i] >= lp->rows)      lp->bas[i]++;  if(constr_type == LE || constr_type == GE) {    lp->orig_upbo[lp->rows] = lp->infinite;  }  else if(constr_type == EQ) {    lp->orig_upbo[lp->rows] = 0;  }  else {    fprintf(stderr, "Wrong constraint type\n");    exit(EXIT_FAILURE);  }  lp->orig_lowbo[lp->rows] = 0;  if(constr_type == GE && rh != 0)    lp->orig_rh[lp->rows] = -rh;  else    lp->orig_rh[lp->rows] = rh;    lp->row_end_valid = FALSE;   lp->bas[lp->rows] = lp->rows;  lp->basis[lp->rows] = TRUE;  lp->lower[lp->rows] = TRUE;     lp->eta_valid = FALSE;}void str_add_constraint(lprec *lp,			char *row_string,			short constr_type,			REAL rh){  int  i;  REAL *aRow;  char *p, *newp;  CALLOC(aRow, lp->columns + 1);  p = row_string;   for(i = 1; i <= lp->columns; i++) {    aRow[i] = (REAL) strtod(p, &newp);    if(p == newp)      error("Bad string in str_add_constr");    else      p = newp;   }  add_constraint(lp, aRow, constr_type, rh);  free(aRow);}void del_constraint(lprec *lp, int del_row){  int i, j;  unsigned elmnr;  int startcol;  if(del_row<1 || del_row>lp->rows)    {      fprintf(stderr, "There is no constraint nr. %d\n", del_row);      exit(EXIT_FAILURE);    }  elmnr = 0;  startcol = 0;  for(i = 1; i <= lp->columns; i++) {    for(j = startcol; j < lp->col_end[i]; j++) {      if(lp->mat[j].row_nr != del_row) {	lp->mat[elmnr] = lp->mat[j];	if(lp->mat[elmnr].row_nr > del_row)	  lp->mat[elmnr].row_nr--;	elmnr++;      }      else	lp->non_zeros--;    }    startcol = lp->col_end[i];    lp->col_end[i] = elmnr;  }  for(i = del_row; i < lp->rows; i++) {    lp->orig_rh[i] = lp->orig_rh[i + 1];    lp->ch_sign[i] = lp->ch_sign[i + 1];    lp->bas[i] = lp->bas[i + 1];    if(lp->names_used)      strcpy(lp->row_name[i], lp->row_name[i + 1]);  }  for(i = 1; i < lp->rows; i++)    if(lp->bas[i] >  del_row)      lp->bas[i]--;  for(i = del_row; i < lp->sum; i++) {    lp->lower[i] = lp->lower[i + 1];    lp->basis[i] = lp->basis[i + 1];    lp->orig_upbo[i] = lp->orig_upbo[i + 1];    lp->orig_lowbo[i] = lp->orig_lowbo[i + 1];    lp->must_be_int[i] = lp->must_be_int[i + 1];    if(lp->scaling_used)      lp->scale[i] = lp->scale[i + 1];  }  lp->rows--;  lp->sum--;  lp->row_end_valid = FALSE;  lp->eta_valid     = FALSE;  lp->basis_valid   = FALSE; }void add_lag_con(lprec *lp, REAL *row, short con_type, REAL rhs){  int i;  REAL sign;  if(con_type == LE || con_type == EQ)    sign = 1;  else if(con_type == GE)    sign = -1;  else    error("con_type not implemented\n");  lp->nr_lagrange++;  if(lp->nr_lagrange == 1) {    CALLOC(lp->lag_row, lp->nr_lagrange);    CALLOC(lp->lag_rhs, lp->nr_lagrange);    CALLOC(lp->lambda, lp->nr_lagrange);    CALLOC(lp->lag_con_type, lp->nr_lagrange);  }  else {    REALLOC(lp->lag_row, lp->nr_lagrange);    REALLOC(lp->lag_rhs, lp->nr_lagrange);    REALLOC(lp->lambda, lp->nr_lagrange);    REALLOC(lp->lag_con_type, lp->nr_lagrange);  }  CALLOC(lp->lag_row[lp->nr_lagrange-1], lp->columns+1);  lp->lag_rhs[lp->nr_lagrange-1] = rhs * sign;  for( i = 1; i <= lp->columns; i++)    lp->lag_row[lp->nr_lagrange-1][i] = row[i] * sign;  lp->lambda[lp->nr_lagrange-1] = 0;  lp->lag_con_type[lp->nr_lagrange-1]=(con_type == EQ);}void str_add_lag_con(lprec *lp, char *row, short con_type, REAL rhs){  int  i;  REAL *a_row;  char *p, *new_p;  CALLOC(a_row, lp->columns + 1);  p = row;   for(i = 1; i <= lp->columns; i++) {    a_row[i] = (REAL) strtod(p, &new_p);    if(p == new_p)      error("Bad string in str_add_lag_con");    else      p = new_p;   }  add_lag_con(lp, a_row, con_type, rhs);  free(a_row);}void add_column(lprec *lp, REAL *column){  int i, elmnr;  /* if the column has only one entry, this should be handled as a bound, but     this currently is not the case */  lp->columns++;  lp->sum++;  inc_col_space(lp);  inc_mat_space(lp, lp->rows + 1);  if(lp->scaling_used) {    for(i = 0; i <= lp->rows; i++)      column[i] *= lp->scale[i];    lp->scale[lp->sum] = 1;  }  elmnr = lp->col_end[lp->columns - 1];  for(i = 0 ; i <= lp->rows ; i++)    if(column[i] != 0) {      lp->mat[elmnr].row_nr = i;      if(lp->ch_sign[i])	lp->mat[elmnr].value = -column[i];      else	lp->mat[elmnr].value = column[i];      lp->non_zeros++;      elmnr++;    }  lp->col_end[lp->columns] = elmnr;  lp->orig_lowbo[lp->sum] = 0;  lp->orig_upbo[lp->sum] = lp->infinite;  lp->lower[lp->sum] = TRUE;  lp->basis[lp->sum] = FALSE;  lp->must_be_int[lp->sum] = FALSE;  if(lp->names_used)    sprintf(lp->col_name[lp->columns], "var_%d", lp->columns);   lp->row_end_valid = FALSE;}void str_add_column(lprec *lp, char *col_string){  int  i;  REAL *aCol;  char *p, *newp;  CALLOC(aCol, lp->rows + 1);  p = col_string;   for(i = 0; i <= lp->rows; i++) {    aCol[i] = (REAL) strtod(p, &newp);    if(p == newp)      error("Bad string in str_add_column");    else      p = newp;   }  add_column(lp, aCol);  free(aCol);}void del_column(lprec *lp, int column){  int i, j, from_elm, to_elm, elm_in_col;  if(column > lp->columns || column < 1)    error("Column out of range in del_column");  for(i = 1; i <= lp->rows; i++) {    if(lp->bas[i] == lp->rows + column)      lp->basis_valid = FALSE;    else if(lp->bas[i] > lp->rows + column)      lp->bas[i]--;  }  for(i = lp->rows + column; i < lp->sum; i++) {    if(lp->names_used)      strcpy(lp->col_name[i - lp->rows], lp->col_name[i - lp->rows + 1]);    lp->must_be_int[i] = lp->must_be_int[i + 1];    lp->orig_upbo[i] = lp->orig_upbo[i + 1];    lp->orig_lowbo[i] = lp->orig_lowbo[i + 1];    lp->upbo[i] = lp->upbo[i + 1];    lp->lowbo[i] = lp->lowbo[i + 1];    lp->basis[i] = lp->basis[i + 1];    lp->lower[i] = lp->lower[i + 1];    if(lp->scaling_used)      lp->scale[i] = lp->scale[i + 1];  }  for(i = 0; i < lp->nr_lagrange; i++)    for(j = column; j <= lp->columns; j++)      lp->lag_row[i][j] = lp->lag_row[i][j+1];  to_elm = lp->col_end[column-1];  from_elm = lp->col_end[column];  elm_in_col = from_elm-to_elm;  for(i = from_elm; i < lp->non_zeros; i++) {    lp->mat[to_elm] = lp->mat[i];    to_elm++;  }  for(i = column; i < lp->columns; i++)    lp->col_end[i] = lp->col_end[i + 1] - elm_in_col;  lp->non_zeros -= elm_in_col;  lp->row_end_valid = FALSE;  lp->eta_valid = FALSE;  lp->sum--;  lp->columns--;}void set_upbo(lprec *lp, int column, REAL value){  if(column > lp->columns || column < 1)    error("Column out of range");  if(lp->scaling_used)    value /= lp->scale[lp->rows + column];  if(value < lp->orig_lowbo[lp->rows + column])    error("Upperbound must be >= lowerbound");   lp->eta_valid = FALSE;  lp->orig_upbo[lp->rows + column] = value;}void set_lowbo(lprec *lp, int column, REAL value){  if(column > lp->columns || column < 1)    error("Column out of range");  if(lp->scaling_used)    value /= lp->scale[lp->rows + column];  if(value > lp->orig_upbo[lp->rows + column])    error("Upperbound must be >= lowerbound");   /*    if(value < 0)    error("Lower bound cannot be < 0");    */  lp->eta_valid = FALSE;  lp->orig_lowbo[lp->rows + column] = value;}void set_int(lprec *lp, int column, short must_be_int){  if(column > lp->columns || column < 1)    error("Column out of range");  lp->must_be_int[lp->rows + column] = must_be_int;  if(must_be_int == TRUE)    if(lp->columns_scaled)      unscale_columns(lp);}void set_rh(lprec *lp, int row, REAL value){  if(row > lp->rows || row < 0)    error("Row out of Range");  if ((row == 0) && (!lp->maximise))  /* setting of RHS of OF IS meaningful */    value = -value;  if(lp->scaling_used) {    if(lp->ch_sign[row])      lp->orig_rh[row] = -value * lp->scale[row];    else      lp->orig_rh[row] = value * lp->scale[row];  }  else    if(lp->ch_sign[row])      lp->orig_rh[row] = -value;    else      lp->orig_rh[row] = value;  lp->eta_valid = FALSE;} void set_rh_vec(lprec *lp, REAL *rh){  int i;  if(lp->scaling_used) {    for(i = 1; i <= lp->rows; i++)      if(lp->ch_sign[i])        lp->orig_rh[i] = -rh[i]*lp->scale[i];      else        lp->orig_rh[i] = rh[i]*lp->scale[i];  }  else    for(i = 1; i <= lp->rows; i++)      if(lp->ch_sign[i])        lp->orig_rh[i] = -rh[i];      else        lp->orig_rh[i] = rh[i];  lp->eta_valid = FALSE;}void str_set_rh_vec(lprec *lp, char *rh_string){  int  i;  REAL *newrh;  char *p, *newp;  CALLOC(newrh, lp->rows + 1);  p = rh_string;   for(i = 1; i <= lp->rows; i++) {    newrh[i] = (REAL) strtod(p, &newp);    if(p == newp)      error("Bad string in str_set_rh_vec");    else      p = newp;   }  set_rh_vec(lp, newrh);  free(newrh);}void set_maxim(lprec *lp){  int i;  if(lp->maximise == FALSE) {    for(i = 0; i < lp->non_zeros; i++)      if(lp->mat[i].row_nr == 0)	lp->mat[i].value *= -1;    lp->eta_valid = FALSE;    lp->orig_rh[0] *= -1;  }   lp->maximise = TRUE;  lp->ch_sign[0] = TRUE;}void set_minim(lprec *lp){  int i;  if(lp->maximise == TRUE) {    for(i = 0; i < lp->non_zeros; i++)      if(lp->mat[i].row_nr == 0)	lp->mat[i].value = -lp->mat[i].value;    lp->eta_valid = FALSE;    lp->orig_rh[0] *= -1;  }   lp->maximise = FALSE;  lp->ch_sign[0] = FALSE;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -