📄 lvq_pak.cpp
字号:
next = i;
}
int orand()
{
return((int) ((next = (next * 23) % 100000001) % RND_MAX));
}
/* init_random - initialize own random number generator with seed.
If seed is 0, uses current time as seed. */
void init_random(int seed)
{
if (!seed)
osrand((int) time(NULL));
else
osrand(seed);
}
int verbose_level = 1;
int verbose(int level)
{
if (level >= 0) {
verbose_level = level;
}
return(verbose_level);
}
int silent(int level)
{
static int silent_level = 0;
if (level >= 0) {
silent_level = level;
}
return(silent_level);
}
int use_fixed(int level)
{
static int fixed_level = 0;
if (level >= 0) {
fixed_level = level;
}
return(fixed_level);
}
int use_weights(int level)
{
static int weights_level = 0;
if (level >= 0) {
weights_level = level;
}
return(weights_level);
}
/* ostrdup - return a pointer to a duplicate of string str. If no memory
for new string cannot be allocated, return NULL. */
char *ostrdup(char *str)
{
char *tmp;
int len;
/* allocate mem for duplicate */
len = strlen(str);
if (len < 0)
return NULL;
tmp = (char*)malloc(sizeof(char) * (len + 1));
if (tmp == NULL)
{
Msg(0, "ostrdup: Can't allocate mem.");
return NULL;
}
/* copy string */
strcpy(tmp, str);
return(tmp);
}
/*******************************************************************
* Routines to get the parameter values *
*******************************************************************/
static int no_parameters = -1;
int parameters_left(void)
{
return(no_parameters);
}
long oatoi(char *str, long def)
{
if (str == (char *) NULL)
return(def);
else
return(atoi(str));
}
float oatof(char *str, float def)
{
if (str == (char *) NULL)
return(def);
else
return((float) atof(str));
}
char *extract_parameter(int argc, char **argv, char *param, int when)
{
int i = 0;
if (no_parameters == -1)
no_parameters = argc - 1;
while ((i < argc) && (strcmp(param, argv[i]))) {
i++;
}
if ((i <= argc - 1) && (when == OPTION2))
{
no_parameters -= 1;
return "";
}
if (i < argc-1) {
no_parameters -= 2;
return(argv[i+1]);
}
else {
if (when == ALWAYS)
{
Msg(0, "Can't find asked option %s", param);
Fail(-1);
}
}
return((char *) NULL);
}
/* global_options - handle some options that are common to all
programs. Also read some environment variables. */
int global_options(int argc, char **argv)
{
char *s;
/* set program name */
/*setprogname(argv[0]);*/
#ifndef NO_PIPED_COMMANDS
/* command for compressing */
s = getenv("LVQSOM_COMPRESS_COMMAND");
if (s)
compress_command = s;
s = extract_parameter(argc, argv, "-compress_cmd", OPTION);
if (s)
compress_command = s;
/* command for uncompressing */
s = getenv("LVQSOM_UNCOMPRESS_COMMAND");
if (s)
uncompress_command = s;
s = extract_parameter(argc, argv, "-uncompress_cmd", OPTION);
if (s)
uncompress_command = s;
#endif /* NO_PIPED_COMMANDS */
if (extract_parameter(argc, argv, "-version", OPTION2))
Msg(0, "Version: %s", get_version());
verbose(oatoi(extract_parameter(argc, argv, VERBOSE, OPTION), 1));
return 0;
}
/* save_snapshot - save a snapshot of the codebook */
int save_snapshot(struct teach_params *teach, long iter)
{
struct entries *codes = teach->codes;
struct snapshot_info *shot = teach->snapshot;
char filename[1024]; /* hope this is enough */
char comments[1024];
/* make filename */
sprintf(filename, shot->filename, iter);
/* open file for writing */
Msg(0, "saving snapshot: file '%s', type '%s'\n", filename,
get_str_by_id(snapshot_list, shot->type));
sprintf(comments, "#SNAPSHOT FILE\n#iterations: %ld (%ld total)\n",
iter, teach->length);
#if 0
if (save_entries_wcomments(codes, filename, comments))
return 1; /* error */
#endif
return 0;
}
/* get_snapshot - allocate and initialize snapshot info */
struct snapshot_info *get_snapshot(char *filename, int interval, int type)
{
struct snapshot_info *shot;
shot = (struct snapshot_info *)malloc(sizeof(struct snapshot_info));
if (shot == NULL)
{
Msg(0, "get_snapshot: Can't allocate structure");
Msg(0, "get_snapshot");
return NULL;
}
/* allocate room for string */
shot->filename = NULL;
if (filename)
if ((shot->filename = (char*)malloc(strlen(filename) + 1)) == NULL)
{
Msg(0, "get_snapshot: Can't allocate mem for string");
free(shot);
return NULL;
}
else
strcpy(shot->filename, filename);
shot->interval = interval;
shot->type = type;
Msg(0, "snapshot: filename: '%s', interval: %d, type: %s",
shot->filename, shot->interval, get_str_by_id(snapshot_list, type));
return shot;
}
/* free_snapshot - deallocate snapshot info */
void free_snapshot(struct snapshot_info *shot)
{
if (shot)
{
if (shot->filename)
free(shot->filename);
free(shot);
}
}
/* get_type_by_id - search typelist for id */
struct typelist *get_type_by_id(struct typelist *types, int id)
{
for (;types->str; types++)
if (types->id == id)
break;
return types;
}
/* get_type_by_str - search typelist for string */
struct typelist *get_type_by_str(struct typelist *types, char *str)
{
for (;types->str; types++)
if (str) /* NULL str gets the last item */
if (strcasecmp(types->str, str) == 0)
break;
return types;
}
/* ------- different alpha functions -------------- */
/* alpha functions */
struct typelist alpha_list[] = {
{ALPHA_LINEAR, "linear", linear_alpha},
{ALPHA_INVERSE_T, "inverse_t", inverse_t_alpha},
{ALPHA_UNKNOWN, NULL, NULL}}; /* default */
/* linearly decreasing alpha */
float linear_alpha(long iter, long length, float alpha)
{
return (alpha * (float) (length - iter) / (float) length);
}
#define INV_ALPHA_CONSTANT 100.0
float inverse_t_alpha(long iter, long length, float alpha)
{
float c;
c = length / INV_ALPHA_CONSTANT;
return (alpha * c / (c + iter));
}
/* print_lines */
int print_lines(FILE *fp, char **lines)
{
char *line;
while ((line = *lines++))
fputs(line, fp);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -