-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlvq_pak.h
More file actions
299 lines (250 loc) · 9.72 KB
/
lvq_pak.h
File metadata and controls
299 lines (250 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/************************************************************************
* *
* Program packages 'lvq_pak' and 'som_pak' : *
* *
* lvq_pak.h *
* -definitions needed in programs *
* *
* Version 3.2 *
* Date: 21 Aug 1995 *
* *
* NOTE: This program package is copyrighted in the sense that it *
* may be used for scientific purposes. The package as a whole, or *
* parts thereof, cannot be included or used in any commercial *
* application without written permission granted by its producents. *
* No programs contained in this package may be copied for commercial *
* distribution. *
* *
* All comments concerning this program package may be sent to the *
* e-mail address 'lvq@cochlea.hut.fi'. *
* *
************************************************************************/
#ifndef LVQ_PAK_H
#define LVQ_PAK_H
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include "config.h"
#ifndef NO_BACKGROUND_SNAP
#include <sys/wait.h>
#include <sys/types.h>
#endif /* NO_BACKGROUND_SNAP */
/* parameters */
#define ALWAYS 1 /* required */
#define OPTION 0 /* optional */
#define OPTION2 2 /* optional, doesn't require an argument */
#define IN_DATA_FILE "-din"
#define OUT_DATA_FILE "-dout"
#define IN_CODE_FILE "-cin"
#define OUT_CODE_FILE "-cout"
#define NUMBER_OF_CODES "-noc"
#define RUNNING_LENGTH "-rlen"
#define TRAINING_ALPHA "-alpha"
#define TRAINING_EPSILON "-epsilon"
#define TRAINING_RADIUS "-radius"
#define WINDOW_WIDTH "-win"
#define KNN_NEIGHBORS "-knn"
#define LABEL "-label"
#define OUT_CLASSIFICATION_FILE "-cfout"
#define VERBOSE "-v"
#define RANDOM "-rand"
#define SILENT "-silent"
#define XDIM "-xdim"
#define YDIM "-ydim"
#define TOPOLOGY "-topol"
#define NEIGHBORHOOD "-neigh"
#define PLANE "-plane"
#define FIXPOINTS "-fixed"
#define WEIGHTS "-weights"
struct fixpoint {
short xfix;
short yfix;
};
/* every entry (either input data or code vector) is stored
in linked lists consisting of following objects */
struct data_entry {
float *points;
/* index to label data base */
union {
int *label_array;
int label;
} lab;
short num_labs;
short weight;
/* pointer to next entry in list */
struct data_entry *next;
char *mask; /* if mask is present, ignore vector components marked
with nonzero */
struct fixpoint *fixed;
};
struct entries {
short dimension; /* dimension of the entry */
short topol; /* topology type */
short neigh; /* neighbourhood */
short xdim, ydim; /* dimensions of the map */
struct data_entry *current; /* current entry */
struct data_entry *entries; /* pointer to entries */
long num_loaded; /* number of lines loaded in entries list */
long num_entries; /* number of entries in the data set if known */
struct {
unsigned int loadmode : 1; /* read whole file into memory or read entries
from file when needed */
unsigned int totlen_known : 1; /* true when total length of file is
known */
unsigned int random_order : 1; /* use data vectors in random order.
only available in LOADMODE_ALL */
unsigned int skip_empty : 1; /* Ignore vectors that have all components
masked off (default) */
unsigned int labels_needed : 1; /* Set if labels are required */
} flags;
int lap; /* how many times have all samples been used */
struct file_info *fi; /* file info for file if needed */
long buffer; /* how many lines to read from file at one time */
void *userdata;
};
#define labels_needed(codes) ((codes)->flags.labels_needed = 1)
/* structure used to get information about the winning entries. Also
with k-nns */
struct winner_info {
long index;
struct data_entry *winner;
float diff;
};
/* adaptation function type */
struct teach_params;
/* function type for adapting a neighboruhood */
typedef void NEIGH_ADAPT(struct teach_params *teach,
struct data_entry *sample,
int bx, int by,
float radius, float alpha);
/* function type for adapting a vector. */
typedef void VECTOR_ADAPT(struct data_entry *c, struct data_entry *s,
int d, float a);
/* function to return the distance between two units on the 2D map */
typedef float MAPDIST_FUNCTION(int bx, int by, int tx, int ty);
/* general distance between two vectors */
typedef float DIST_FUNCTION(struct data_entry *v1, struct data_entry *v2, int dim);
typedef int WINNER_FUNCTION(struct entries *codes, struct data_entry *sample, struct winner_info *w, int knn);
typedef float ALPHA_FUNC(long iter, long length, float alpha);
struct snapshot_info {
long interval; /* save codebook every 'interval' iterations */
char *filename; /* filename of snapshot file */
int type; /* type of action */
int flags;
struct file_info *fi;
#ifndef NO_BACKGROUND_SNAP
pid_t pid;
#endif
int counter;
void *data;
};
/* Snapshot types. Not actually used right now because first two are
determined from the filename and the last one is not yet
implemented */
#define SNAPSHOT_SAVEFILE 1 /* Save codebook in file */
#define SNAPSHOT_EXEC_CMD 2 /* Pipe codebook file to a command. Filename
is the command line to execute. */
#ifndef NO_BACKGROUND_SNAP
#define SNAPSHOT_ASYNC 3 /* same as above but fork a new process
before saving */
#endif
#define SNAPSHOT_KEEPOPEN 4 /* save all snapshots consecutively in one file */
#define SNAPSHOT_ASYNC_NOWAIT 5 /* when starting a new backround snap don't
wait for the previous one to complete */
#define SNAPFLAG_KEEPOPEN 1
#ifndef NO_BACKGROUND_SNAP
#define SNAPFLAG_BACKGROUND 2 /* save file on background */
#define SNAPFLAG_NOWAIT 4 /* do not wait for previous save to complete */
#endif
struct teach_params {
short topol;
short neigh;
short alpha_type;
MAPDIST_FUNCTION *mapdist; /* calculates distance between two units */
DIST_FUNCTION *dist; /* calculates distance between two vectors */
NEIGH_ADAPT *neigh_adapt; /* adapts weights */
VECTOR_ADAPT *vector_adapt; /* adapt one vector */
WINNER_FUNCTION *winner; /* function to find winner */
ALPHA_FUNC *alpha_func;
float radius; /* initial radius (for SOM) */
float alpha; /* initial alpha value */
long length; /* length of training */
int knn; /* nearest neighbours */
struct entries *codes;
struct entries *data;
struct snapshot_info *snapshot;
time_t start_time, end_time;
};
#define LOADMODE_ALL 0
#define LOADMODE_BUFFER 1
/* topology types */
#define TOPOL_UNKNOWN 0
#define TOPOL_DATA 1
#define TOPOL_LVQ 2
#define TOPOL_HEXA 3
#define TOPOL_RECT 4
/* neighborhood types */
#define NEIGH_UNKNOWN 0
#define NEIGH_BUBBLE 1
#define NEIGH_GAUSSIAN 2
/* alpha function types */
#define ALPHA_UNKNOWN 0
#define ALPHA_LINEAR 1
#define ALPHA_INVERSE_T 2
struct typelist {
int id; /* numeric id */
char *str;
void *data; /* data pointer */
};
extern struct typelist alpha_list[];
struct entry_ptr {
struct data_entry *current;
struct entries *parent;
long index;
};
typedef struct entry_ptr eptr;
extern int lvq_errno;
/* labels */
#include "labels.h"
WINNER_FUNCTION find_winner_euc, find_winner_knn;
DIST_FUNCTION vector_dist_euc;
VECTOR_ADAPT adapt_vector;
/* useful general routines */
void errormsg(char *msg);
void debugmsg(char *msg);
void *oalloc(unsigned int len);
void *orealloc(void *po, unsigned int len);
void ofree(void *ptr);
struct fixpoint *get_fixed(char *str);
void mprint(long rlen);
int verbose(int level);
int silent(int level);
extern int verbose_level;
#define ifverbose(lvl) if (verbose_level >= lvl)
void osrand(int i);
long orand();
void init_random(int i);
char *ostrdup(char *str);
long oatoi(char *str, long def);
float oatof(char *str, float def);
char *extract_parameter(int argc, char **argv, char *param, int when);
int parameters_left(void);
int global_options(int argc, char **argv);
/* Saving snapshots */
int save_snapshot(struct teach_params *teach, long iter);
struct snapshot_info *get_snapshot(char *filename, long interval, int type);
void free_snapshot(struct snapshot_info *shot);
/* typelist searches */
struct typelist *get_type_by_id(struct typelist *types, int id);
struct typelist *get_type_by_str(struct typelist *types, char *str);
#define get_str_by_id(l,i) (get_type_by_id((l),(i))->str)
#define get_id_by_str(l,s) (get_type_by_str((l),(s))->id)
#define get_data_by_id(l,i) (get_type_by_id((l),(i))->data)
#define get_data_by_str(l,s) (get_type_by_str((l),(s))->data)
/* alpha functions */
ALPHA_FUNC linear_alpha, inverse_t_alpha;
int print_lines(FILE *fp, char **);
#define printhelp() print_lines(stdout,usage)
char *get_version(void);
#endif /* LVQ_PAK_H */