📄 options.c
字号:
/* LOQO options being read for general nonlinear optimization *//* Copyright Hande Y. Benson 2002 */#include <math.h>#include <stdio.h>#include <stdlib.h>#include "loqo.h"#include "myalloc.h"int rd_opns(char *s);int rd_specs(char *Spec_name);void *binsearch(char **sp);void set_opns(LOQO *lp);static LOQO *lp=NULL;static char *opnstr=NULL;static int bad_opns;static int spec=0;typedef struct keyword keyword;struct keyword { char *name; int type; int ivalue; double dvalue; };#define KW(a,b,c,d) {a,b,c,d}static keyword keywds[] = { KW("bndpush", 2, 0, -1.0), KW("convex", 0, 0, 0.0), KW("dense", 1, -1, 0.0), KW("dual", 0, 0, 0.0), KW("epsdiag", 2, 0, 1.0e-10), KW("epsnum", 2, 0, 0.0), KW("epssol", 2, 0, 1.0e-6), KW("honor_bnds", 0, 0, 0.0), KW("honor_bnds_init", 0, 0, 0.0), KW("inftol", 2, 0, 1e-6), KW("inftol2", 2, 0, 1e+5), KW("iterlim", 1, 500, 0.0), KW("lincons", 1, 0, 0.0), KW("max", 0, 0, 0.0), KW("maximize", 0, 0, 0.0), KW("maxit", 1, 200, 0.0), KW("min", 0, 0, 0.0), KW("mindeg", 0, 0, 0.0), KW("minimize", 0, 0, 0.0), KW("minlocfil", 0, 0, 0.0), KW("mufactor", 2, 0, -1.0), KW("noreord", 0, 0, 0.0), KW("outlev", 1, 0, 0.0), KW("pred_corr", 0, 0, 0.0), KW("primal", 0, 0, 0.0), KW("quadratic", 0, 0, 0.0), KW("sdp", 0, 0, 0.0), KW("sigfig", 1, 8, 0.0), KW("stablty", 2, 0, 1.0), KW("steplen", 2, 0, 0.95), KW("timlim", 2, 0, -1.0), KW("verbose", 1, 0, 0.0)}; /* if (opnstr != NULL) { bad_opns = 0; if (!rd_opns()) { printf("loqo_options not set correctly\n"); return 0; } } set_opns();*/int rd_opns(char *s) { char *s1; keyword *kw; while (*s) { while (*s && *s==' ') s++; if (!*s) return 1; if ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z')) { kw = (keyword *)binsearch(&s); if (bad_opns) return 0; while (*s && *s == ' ') s++; switch (kw->type) { case 0: kw->ivalue = 1; /* depending on keyword, this may need to change */ break; case 1: if (*s == '=') { s++; while (*s && *s == ' ') s++; if (*s <= '9' && *s >= '0') { kw->ivalue = (int)strtol(s1 = s, &s, 10); } else { return 0; } } else { return 0; } break; case 2: if (*s == '=') { s++; while (*s && *s == ' ') s++; if (*s <= '9' && *s >= '0') { kw->dvalue = strtod(s1 = s, &s); } else { return 0; } } else { return 0; } break; } } else { return 0; } } return 1;}void *binsearch(char **sp) { keyword *kw = keywds; keyword *kw1; int n = 32; /* number of keywords */ int n1; int c1, c2; char *s, *s1, *s2; s = *sp; while (n > 0) { kw1 = kw + (n1 = n >> 1); s2 = *(char **)kw1; for (s1 = s;; s1++) { c1 = tolower(*(unsigned char *)s1); if (!(c2 = *s2++)) { if (c1 <= ' ' || c1 == '=') { *sp = s1; return kw1; } break; } if (c1 != c2) break; } if (c1 == '=' || c1 < c2) n = n1; else { n -= n1 + 1; kw = kw1 + 1; } } bad_opns++; return 0;}void set_opns(LOQO *lp) { keyword *kw = keywds; int verbose, itnlim; int noreord, md, mlf; int primal, dual; int max, maximize, min, minimize; lp->bndpush = kw->dvalue; kw++; lp->convex = kw->ivalue; kw++; lp->dense = kw->ivalue; kw++; dual = kw->ivalue; kw++; lp->epsdiag = kw->dvalue; kw++; lp->epsnum = kw->dvalue; kw++; lp->epssol = kw->dvalue; kw++; lp->honor_bnds = kw->ivalue; kw++; lp->honor_bnds_init = kw->ivalue; kw++; lp->inftol = kw->dvalue; kw++; lp->inftol2 = kw->dvalue; kw++; itnlim = kw->ivalue; kw++; lp->lincons = kw->ivalue; kw++; max = kw->ivalue; kw++; maximize = kw->ivalue; kw++; if (itnlim == 200) lp->itnlim = kw->ivalue; else lp->itnlim = itnlim; kw++; min = kw->ivalue; kw++; md = kw->ivalue; kw++; minimize = kw->ivalue; kw++; mlf = kw->ivalue; kw++; lp->mufactor = kw->dvalue; kw++; noreord = kw->ivalue; kw++; verbose = kw->ivalue; kw++; lp->pred_corr = kw->ivalue; kw++; primal = kw->ivalue; kw++; lp->quadratic = kw->ivalue; kw++; lp->sdp = kw->ivalue; kw++; lp->sf_req = kw->ivalue; kw++; lp->stablty = kw->dvalue; kw++; lp->steplen = kw->dvalue; kw++; if (kw->dvalue != -1.0) lp->timlim = kw->dvalue; kw++; if (verbose == 0) lp->verbose = kw->ivalue; else lp->verbose = verbose; kw++; lp->method=1; if (noreord) lp->method = 0; else if (mlf) lp->method = 2; else if (md) lp->method = 1; lp->pdf=0; if (primal) lp->pdf = 1; if (dual) lp->pdf = 2; lp->max=1; if (max || maximize) lp->max = -1; if (min || minimize) lp->max = 1; if (lp->quadratic) lp->lincons=1;} int rd_specs(char *Spec_name) { char *s1; keyword *kw; char *CUTEr_loc; char *Spec_loc; char specline[80]; char option[15], val[15], comment[50]; char *optionptr, *valptr; int i, j; FILE *specfile; int s; spec = 1; /* open the specs file */ specfile = fopen(Spec_name, "r"); fgets(specline, 80, specfile); s = sscanf(specline, "%s%s%s", option, val, comment); i = 0; while (s && !feof(specfile)) { i++; if ((option[0] >= 'a' && option[0] <= 'z') || (option[0] >= 'A' && option[0] <= 'Z')) { optionptr = &option[0]; kw = (keyword *)binsearch(&optionptr); if (bad_opns) { fclose(specfile); return i; } switch (kw->type) { case 0: if (val[0] == 'T') kw->ivalue = 1; else if (val[0] == 'F') kw->ivalue = 0; else { fclose(specfile); return i; } break; case 1: if ((val[0] <= '9' && val[0] >= '0') || (val[0] == '-' && val[1] <= '9' && val[1] >= 0)) { valptr = &val[0]; kw->ivalue = (int)strtol(s1 = valptr, &valptr, 10); } else { fclose(specfile); return i; } break; case 2: if (val[0] <= '9' && val[0] >= '0') { valptr = &val[0]; kw->dvalue = strtod(s1 = valptr, &valptr); } else { fclose(specfile); return i; } break; } } else { if (option[0] != '*') { /* Comment */ fclose(specfile); return i; } } fgets(specline, 80, specfile); s = sscanf(specline, "%s%s%s", option, val, comment); } fclose(specfile); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -