File: cle_examplerep.c

package info (click to toggle)
eprover 2.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,288 kB
  • sloc: ansic: 331,111; csh: 12,026; python: 10,178; awk: 5,825; makefile: 461; sh: 389
file content (473 lines) | stat: -rw-r--r-- 11,888 bytes parent folder | download
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*-----------------------------------------------------------------------

File  : cle_examplerep.c

Author: Stephan Schulz

Contents

  Functions for dealing with (sets of) example representations.

  Copyright 1998, 1999 by the author.
  This code is released under the GNU General Public Licence and
  the GNU Lesser General Public License.
  See the file COPYING in the main E directory for details..
  Run "eprover -h" for contact information.

Changes

<1> Tue Jul 27 11:46:29 MET DST 1999
    New

-----------------------------------------------------------------------*/

#include "cle_examplerep.h"



/*---------------------------------------------------------------------*/
/*                        Global Variables                             */
/*---------------------------------------------------------------------*/


/*---------------------------------------------------------------------*/
/*                      Forward Declarations                           */
/*---------------------------------------------------------------------*/


/*---------------------------------------------------------------------*/
/*                         Internal Functions                          */
/*---------------------------------------------------------------------*/



/*---------------------------------------------------------------------*/
/*                         Exported Functions                          */
/*---------------------------------------------------------------------*/

/*-----------------------------------------------------------------------
//
// Function: ExampleRepFree()
//
//   Free an example represenation.
//
// Global Variables: -
//
// Side Effects    : Memory operations
//
/----------------------------------------------------------------------*/

void ExampleRepFree(ExampleRep_p junk)
{
   FeaturesFree(junk->features);
   FREE(junk->name);
   ExampleRepCellFree(junk);
}


/*-----------------------------------------------------------------------
//
// Function: ExampleRepPrint()
//
//   Print an example representation.
//
// Global Variables: -
//
// Side Effects    : Output
//
/----------------------------------------------------------------------*/

void ExampleRepPrint(FILE* out, ExampleRep_p rep)
{
   fprintf(out, "%ld: \"%s\"\n", rep->ident, rep->name);
   NumFeaturesPrint(out, rep->features);
   fputc('\n', out);
}


/*-----------------------------------------------------------------------
//
// Function: ExampleRepParse()
//
//   Parse an example representation and return a pointer to it.
//
// Global Variables: -
//
// Side Effects    : Input, memory operations.
//
/----------------------------------------------------------------------*/

ExampleRep_p  ExampleRepParse(Scanner_p in)
{
   ExampleRep_p handle = ExampleRepCellAlloc();

   handle->ident = AktToken(in)->numval;
   AcceptInpTok(in, PosInt);
   AcceptInpTok(in, Colon);
   CheckInpTok(in, Name);
   if(TestInpTok(in, String))
   {
      char *tmp;
      int l;

      tmp = DStrCopy(AktToken(in)->literal);
      l = strlen(tmp);
      tmp[l-1] = '\0';
      handle->name = SecureStrdup(tmp+1);
      FREE(tmp);
   }
   else
   {
      handle->name = DStrCopy(AktToken(in)->literal);
   }
   NextToken(in);
   handle->features = NumFeaturesParse(in);

   return handle;
}

/*-----------------------------------------------------------------------
//
// Function: ExampleSetAlloc()
//
//   Allocate an empty example set and return a pointer to it.
//
// Global Variables: -
//
// Side Effects    : Memory operations
//
/----------------------------------------------------------------------*/

ExampleSet_p ExampleSetAlloc(void)
{
   ExampleSet_p handle = ExampleSetCellAlloc();

   handle->count = 0;
   handle->ident_index = NULL;
   handle->name_index = NULL;

   return handle;
}


/*-----------------------------------------------------------------------
//
// Function: ExampleSetFree()
//
//   Free an exampel set.
//
// Global Variables: -
//
// Side Effects    : Memory operations
//
/----------------------------------------------------------------------*/

void ExampleSetFree(ExampleSet_p junk)
{
   PStack_p stack;
   NumTree_p handle;

   stack = NumTreeTraverseInit(junk->ident_index);
   while((handle = NumTreeTraverseNext(stack)))
   {
      ExampleRepFree(handle->val1.p_val);
   }
   PStackFree(stack);
   NumTreeFree(junk->ident_index);
   StrTreeFree(junk->name_index);
   ExampleSetCellFree(junk);
}


/*-----------------------------------------------------------------------
//
// Function: ExampleSetFindName()
//
//   Find an entry by name, return NULL if non-existant.
//
// Global Variables: -
//
// Side Effects    : - (well, reorganizes tree)
//
/----------------------------------------------------------------------*/

ExampleRep_p  ExampleSetFindName(ExampleSet_p set, char* name)
{
   StrTree_p handle;

   handle = StrTreeFind(&(set->name_index), name);
   if(!handle)
   {
      return NULL;
   }
   return handle->val1.p_val;
}

/*-----------------------------------------------------------------------
//
// Function: ExampleSetInsert()
//
//   Insert rep into set. Return true if it works, false otherwise.
//
// Global Variables: -
//
// Side Effects    : Changes set.
//
/----------------------------------------------------------------------*/

bool ExampleSetInsert(ExampleSet_p set, ExampleRep_p rep)
{
   IntOrP tmp;
   StrTree_p res;
   bool res1;

   tmp.p_val = rep;

   res1 = NumTreeStore(&(set->ident_index), rep->ident, tmp, tmp);
   if(!res1)
   {
      return false;
   }
   res = StrTreeStore(&(set->name_index), rep->name, tmp, tmp);
   if(!res)
   {
      return false;
   }
   set->count = MAX(set->count, rep->ident);
   return true;
}


/*-----------------------------------------------------------------------
//
// Function: ExampleSetExtract()
//
//   Extract rep from set and return it. Return NULL is rep does not
//   exist in set.
//
// Global Variables: -
//
// Side Effects    : Changes trees.
//
/----------------------------------------------------------------------*/

ExampleRep_p ExampleSetExtract(ExampleSet_p set, ExampleRep_p rep)
{
   ExampleRep_p handle;
   NumTree_p    cell;
   bool         res;

   cell = NumTreeExtractEntry(&(set->ident_index), rep->ident);
   if(!cell)
   {
      return NULL;
   }
   handle = cell->val1.p_val;
   NumTreeCellFree(cell);
   res = StrTreeDeleteEntry(&(set->name_index), rep->name);
   UNUSED(res); assert(res);

   return handle;
}


/*-----------------------------------------------------------------------
//
// Function: ExampleSetDeleteId()
//
//   Delete the example with ident id. Returns success.
//
// Global Variables: -
//
// Side Effects    : Changes set, memory operations
//
/----------------------------------------------------------------------*/

bool ExampleSetDeleteId(ExampleSet_p set, long ident)
{
   ExampleRep_p handle;
   NumTree_p    cell;

   cell = NumTreeFind(&(set->ident_index), ident);
   if(!cell)
   {
      return false;
   }
   handle = ExampleSetExtract(set, cell->val1.p_val);
   assert(handle);
   ExampleRepFree(handle);
   return true;
}

/*-----------------------------------------------------------------------
//
// Function: ExampleSetDeleteName()
//
//   Delete the example with name name. Returns success.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

bool ExampleSetDeleteName(ExampleSet_p set, char* name)
{
   ExampleRep_p handle;
   StrTree_p    cell;

   cell = StrTreeFind(&(set->name_index), name);
   if(!cell)
   {
      return false;
   }
   handle = ExampleSetExtract(set, cell->val1.p_val);
   assert(handle);
   ExampleRepFree(handle);
   return true;
}

/*-----------------------------------------------------------------------
//
// Function: ExampleSetPrint()
//
//   Print a set of example representations.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

void ExampleSetPrint(FILE* out, ExampleSet_p set)
{
   PStack_p  stack;
   NumTree_p handle;

   stack = NumTreeTraverseInit(set->ident_index);
   while((handle = NumTreeTraverseNext(stack)))
   {
      ExampleRepPrint(out, handle->val1.p_val);
   }
   NumTreeTraverseExit(stack);
}


/*-----------------------------------------------------------------------
//
// Function: ExampleSetParse()
//
//   Parse a list of examples into set. Return number of items parsed.
//
// Global Variables: -
//
// Side Effects    : Reads input, memory, may give error, changes
//                   set->count.
//
/----------------------------------------------------------------------*/

long ExampleSetParse(Scanner_p in, ExampleSet_p set)
{
   long         count = 0;
   ExampleRep_p handle;
   DStr_p       source_name, errpos;
   long         line, column;
   StreamType   type;
   bool         res;

   while(TestInpTok(in, PosInt))
   {
      line = AktToken(in)->line;
      column = AktToken(in)->column;
      source_name = DStrGetRef(AktToken(in)->source);
      type = AktToken(in)->stream_type;
      handle = ExampleRepParse(in);
      res =  ExampleSetInsert(set, handle);
      if(!res)
      {
    errpos = DStrAlloc();

    DStrAppendStr(errpos, PosRep(type, source_name, line,
                  column));
    DStrAppendStr(errpos, "Entry ");
    DStrAppendInt(errpos, handle->ident);
    DStrAppendStr(errpos, " conflicts with existing entries");
    Error(DStrView(errpos), SYNTAX_ERROR);
    DStrFree(errpos);
      }
      count++;
      DStrReleaseRef(source_name);
   }
   return count;
}


/*-----------------------------------------------------------------------
//
// Function: ExampleSetSelectByDist()
//
//   Push idents of the most similar examples onto results. How many
//   examples is controlled by select, set_part, and dist_part:
//   Selected are at most select examples, at most part*setsize
//   examples and only examples whose distance is not larger than
//   dist_part times average distance.
//
// Global Variables: -
//
// Side Effects    : Memory operations, somewhat costly.
//
/----------------------------------------------------------------------*/

long ExampleSetSelectByDist(PStack_p results, ExampleSet_p set,
             Features_p target, double pred_w, double
             func_w, double *weights, long sel_no,
             double set_part, double dist_part)
{
   long             set_size = NumTreeNodes(set->ident_index),
                    i, climit;
   double           dlimit, dist, avg;
   WeightedObject_p tmp_array = WeightedObjectArrayAlloc(set_size);
   PStack_p     stack;
   NumTree_p    cell;
   ExampleRep_p current;

   i = 0;
   avg = 0;
   stack = NumTreeTraverseInit(set->ident_index);
   while((cell = NumTreeTraverseNext(stack)))
   {
      current = cell->val1.p_val;
      dist = NumFeatureDistance(target, current->features, pred_w,
            func_w, weights);
      tmp_array[i].weight       = dist;
      tmp_array[i].object.p_val = current;
      avg += dist;
      i++;
   }
   assert(i == set_size);
   NumTreeTraverseExit(stack);
   avg = avg /(double)set_size;

   WeightedObjectArraySort(tmp_array, set_size);
   climit = MIN(sel_no, set_part*set_size);
   dlimit = dist_part*avg;
   assert(climit <= set_size);
   for(i=0; i<climit && tmp_array[i].weight <= dlimit; i++)
   {
      current = tmp_array[i].object.p_val;
      if(Verbose)
      {
    fprintf(stderr, "Selected problem %ld: %s\n", current->ident,
       current->name);
      }
      PStackPushInt(results, current->ident);
   }
   WeightedObjectArrayFree(tmp_array);
   return i;
}


/*---------------------------------------------------------------------*/
/*                        End of File                                  */
/*---------------------------------------------------------------------*/