File: cle_numfeatures.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 (464 lines) | stat: -rw-r--r-- 11,617 bytes parent folder | download | duplicates (2)
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
/*-----------------------------------------------------------------------

File  : cle_numfeatures.c

Author: Stephan Schulz

Contents

  Functions for dealing with numerical features of clause sets.

  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> Mon Jul 26 18:55:24 MET DST 1999
    New

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

#include "cle_numfeatures.h"


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


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


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


/*-----------------------------------------------------------------------
//
// Function: relative_difference()
//
//   Return the relative difference of two values.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

static double relative_difference(double v1, double v2)
{
   double abs1, abs2, res;

   if((v1==0.0) && (v2==0.0))
   {
      return 0;
   }
   abs1 = ABS(v1);
   abs2 = ABS(v2);
   res = (v1-v2)/(2*MAX(abs1,abs2));

   return res;
}


/*-----------------------------------------------------------------------
//
// Function: arity_distr_distance()
//
//   Compute the normed euclidean distance beween two arity distribution
//   vectors.
//
// Global Variables: -
//
// Side Effects    : May theoretically extend the smaller distribution
//                   very unlikely, and completely harmless)
//
/----------------------------------------------------------------------*/

static double arity_distr_distance(PDArray_p d1, PDArray_p d2, int
               maxarity)
{
   int i, val1, val2;
   double res = 0;

   assert(maxarity >= -1);
   if(maxarity == -1)
   {
      return 0.0;
   }
   for(i=0; i<=maxarity; i++)
   {
      val1 = PDArrayElementInt(d1, i);
      val2 = PDArrayElementInt(d2, i);
      res += relative_difference(val1,val2)*
    relative_difference(val1,val2);
   }
   return sqrt(res)/(double)(maxarity+1);
}


/*-----------------------------------------------------------------------
//
// Function: parse_sig_distrib()
//
//   Parse a list (n0, n1, ... nn) into a PDArray.
//
// Global Variables: -
//
// Side Effects    : Reads input
//
/----------------------------------------------------------------------*/

static int parse_sig_distrib(Scanner_p in, PDArray_p distrib)
{
   int i= -1, symbols;

   AcceptInpTok(in, OpenBracket);

   if(!TestInpTok(in, CloseBracket))
   {
      i++;
      symbols = ParseInt(in);
      PDArrayAssignInt(distrib, i, symbols);
      while(!TestInpTok(in, CloseBracket))
      {
    i++;
    AcceptInpTok(in, Comma);
    symbols = ParseInt(in);
    PDArrayAssignInt(distrib, i, symbols);
      }
   }
   AcceptInpTok(in, CloseBracket);

   return i;
}

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


/*-----------------------------------------------------------------------
//
// Function: FeaturesAlloc()
//
//   Allocate an empty, initialized FeaturesCell()
//
// Global Variables: -
//
// Side Effects    : Memory operations
//
/----------------------------------------------------------------------*/

Features_p FeaturesAlloc(void)
{
   Features_p handle = FeaturesCellAlloc();

   handle->pred_distrib = PDIntArrayAlloc(5,5);
   handle->func_distrib = PDIntArrayAlloc(5,5);
   return handle;
}


/*-----------------------------------------------------------------------
//
// Function: FeaturesFree()
//
//   Free a FeaturesCell()
//
// Global Variables: -
//
// Side Effects    : Memory operations
//
/----------------------------------------------------------------------*/

void FeaturesFree(Features_p junk)
{
   PDArrayFree(junk->pred_distrib);
   PDArrayFree(junk->func_distrib);
   FeaturesCellFree(junk);
}


/*-----------------------------------------------------------------------
//
// Function: ComputeClauseSetNumFeatures()
//
//   Compute the numerical features of a clause set. This is not as
//   modular as I would have liked, as I expect this to be done fairly
//   often and hence want to do it in a single pass.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

void ComputeClauseSetNumFeatures(Features_p features, ClauseSet_p set,
             Sig_p sig)
{
   PStack_p pos_tdepth = PStackAlloc();
   PStack_p neg_tdepth = PStackAlloc();
   PStack_p pos_tsize  = PStackAlloc();
   PStack_p neg_tsize  = PStackAlloc();
   PStack_p pos_lits   = PStackAlloc();
   PStack_p neg_lits   = PStackAlloc();
   double   average, deviation;
   Clause_p clause;
   Eqn_p    eqn;
   long     *symbol_distrib;
   long     i;

   symbol_distrib = SizeMalloc((sig->size)*sizeof(long));
   for(i=0; i< sig->size; i++)
   {
      symbol_distrib[i] = 0;
   }

   ClauseSetAddSymbolDistribution(set, symbol_distrib);
   features->pred_max_arity =
      SigAddSymbolArities(sig, features->pred_distrib, true,
           symbol_distrib);
   features->func_max_arity =
      SigAddSymbolArities(sig, features->func_distrib, false,
           symbol_distrib);
   SizeFree(symbol_distrib,(sig->size)*sizeof(long));

   features->features[0] = 0;
   features->features[1] = 0;
   features->features[2] = 0;

   for(clause = set->anchor->succ; clause != set->anchor; clause =
     clause->succ)
   {
      if(ClauseIsUnit(clause))
      {
    features->features[0]++;
      }
      else if(ClauseIsHorn(clause))
      {
    features->features[1]++;
      }
      else
      {
    features->features[2]++;
      }
      PStackPushInt(pos_lits, clause->pos_lit_no);
      PStackPushInt(neg_lits, clause->neg_lit_no);

      for(eqn = clause->literals; eqn; eqn = eqn->next)
      {
    if(EqnIsPositive(eqn))
    {
       PStackPushInt(pos_tsize, TermWeight(eqn->lterm,
                  DEFAULT_VWEIGHT,
                  DEFAULT_FWEIGHT));
       PStackPushInt(pos_tsize, TermWeight(eqn->rterm,
                  DEFAULT_VWEIGHT,
                  DEFAULT_FWEIGHT));
       PStackPushInt(pos_tdepth, TermDepth(eqn->lterm));
       PStackPushInt(pos_tdepth, TermDepth(eqn->rterm));
    }
    else
    {
       PStackPushInt(neg_tsize, TermWeight(eqn->lterm,
                  DEFAULT_VWEIGHT,
                  DEFAULT_FWEIGHT));
       PStackPushInt(neg_tsize, TermWeight(eqn->rterm,
                  DEFAULT_VWEIGHT,
                  DEFAULT_FWEIGHT));
       PStackPushInt(neg_tdepth, TermDepth(eqn->lterm));
       PStackPushInt(neg_tdepth, TermDepth(eqn->rterm));
    }
      }
   }
   average = PStackComputeAverage(pos_tdepth, &deviation);
   features->features[3] = average;
   features->features[4] = deviation;
   average = PStackComputeAverage(neg_tdepth, &deviation);
   features->features[5] = average;
   features->features[6] = deviation;
   average = PStackComputeAverage(pos_tsize, &deviation);
   features->features[7] = average;
   features->features[8] = deviation;
   average = PStackComputeAverage(neg_tsize, &deviation);
   features->features[9] = average;
   features->features[10] = deviation;
   average = PStackComputeAverage(pos_lits, &deviation);
   features->features[11] = average;
   features->features[12] = deviation;
   average = PStackComputeAverage(neg_lits, &deviation);
   features->features[13] = average;
   features->features[14] = deviation;

   PStackFree(neg_lits);
   PStackFree(pos_lits);
   PStackFree(neg_tsize);
   PStackFree(pos_tsize);
   PStackFree(neg_tdepth);
   PStackFree(pos_tdepth);
}


/*-----------------------------------------------------------------------
//
// Function: NumFeaturesPrint()
//
//   Print the feature cell.
//
// Global Variables: -
//
// Side Effects    : Output
//
/----------------------------------------------------------------------*/

void NumFeaturesPrint(FILE* out, Features_p features)
{
   char *sep;
   long i;

   fputs("PA: (" , out);
   sep = "";
   for(i=0; i<=features->pred_max_arity; i++)
   {
      fprintf(out, "%s%ld", sep,
         PDArrayElementInt(features->pred_distrib,i));
      sep = ", ";
   }
   fputs(")  FA: (", out);
   sep = "";
   for(i=0; i<=features->func_max_arity; i++)
   {
      fprintf(out, "%s%ld", sep,
         PDArrayElementInt(features->func_distrib,i));
      sep = ", ";
   }
   fprintf(out, ")\n(%f", features->features[0]);
   for(i=1; i<FEATURE_NUMBER;i++)
   {
      fprintf(out, ", %f", features->features[i]);
   }
   fputs(")\n",out);
}


/*-----------------------------------------------------------------------
//
// Function: NumFeaturesParse()
//
//   Parse a set of features.
//
// Global Variables: -
//
// Side Effects    : Allocates memory, reads input
//
/----------------------------------------------------------------------*/

Features_p NumFeaturesParse(Scanner_p in)
{
   Features_p handle = FeaturesAlloc();
   long i;

   AcceptInpId(in, "PA");
   AcceptInpTok(in, Colon);
   handle->pred_max_arity = parse_sig_distrib(in,
                     handle->pred_distrib);

   AcceptInpId(in, "FA");
   AcceptInpTok(in, Colon);
   handle->func_max_arity = parse_sig_distrib(in,
                     handle->func_distrib);
   AcceptInpTok(in, OpenBracket);
   handle->features[0] = ParseFloat(in);
   for(i=1; i< FEATURE_NUMBER; i++)
   {
      AcceptInpTok(in, Comma);
      handle->features[i] = ParseFloat(in);
   }
   AcceptInpTok(in, CloseBracket);

   return handle;
}

/*-----------------------------------------------------------------------
//
// Function: NumFeatureDistance()
//
//   Return the weighted relative distance between the two feature
//   vectors.
//
// Global Variables: -
//
// Side Effects    : -
//
/----------------------------------------------------------------------*/

double NumFeatureDistance(Features_p f1, Features_p f2, double pred_w,
           double func_w, double* weights)
{
   double res, norm, dist, wsq;
   int i;

   dist = arity_distr_distance(f1->pred_distrib, f2->pred_distrib,
                MAX(f1->pred_max_arity,
               f2->pred_max_arity));
   wsq = pred_w*pred_w;
   res = dist*dist*wsq;
   norm = wsq;

   dist = arity_distr_distance(f1->func_distrib, f2->func_distrib,
                MAX(f1->func_max_arity,
               f2->func_max_arity));
   wsq = func_w*func_w;
   res += dist*dist*wsq;
   norm += wsq;

   for(i=0; i<FEATURE_NUMBER; i++)
   {
      dist = relative_difference(f1->features[i],f2->features[i]);
      wsq  = weights[i] * weights[i];
      res += dist*dist*wsq;
      norm += wsq;
   }
   res = res/norm;
   return sqrt(res);
}


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