-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataCell.cpp
392 lines (342 loc) · 10.1 KB
/
DataCell.cpp
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
/*
* Argus Open Source
* Software to apply Statistical Disclosure Control techniques
*
* Copyright 2014 Statistics Netherlands
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the European Union Public Licence
* (EUPL) version 1.1, as published by the European Commission.
*
* You can find the text of the EUPL v1.1 on
* https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
*
* This software is distributed on an "AS IS" basis without
* warranties or conditions of any kind, either express or implied.
*/
#include <cstring>
#include <cmath>
#include "General.h"
#include "DataCell.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BIGNUMBER 1.0E16
/////////////////////////////////////////////////////////////////////////////
// CDataCell
// Constructor used in principle for creating a new cell that is added to the table
CDataCell::CDataCell(int NumberMaxScoreCell, int NumberMaxScoreHolding, int IsHolding, int IsWeight)
{
// FILE *fd;
// fd = fopen("E:/Temp/Debug.txt","a");
// fprintf(fd,"%Xh%s\n", this," CDatacell Const () ");
// fclose(fd);
Resp = 0;
NWResp = 0;
Cost = 0;
Shadow = 0;
CellKey = 0;
CellKeyNoZeros = 0;
Freq = 0;
Weight = 0;
FreqHolding = 0;
RoundedResp = 0;
CTAValue = 0;
CKMValue = 0;
// Needed for CKMType = "D"
// Set to large double
MinScoreCell = BIGNUMBER;
MinScoreWeightCell = 0;
Status = CS_EMPTY;
TempShadow = 0;
HoldingNr = IsHolding ? WITH_HOLDING : WITHOUT_HOLDING;
PeepSortCell = EMPTY;
PeepSortHolding = EMPTY;
TempPeepSort = EMPTY;
PeepCell = 0;
PeepHolding = 0;
//set ini values for realized upper and lower.as 0
RealizedUpperValue = 0;
RealizedLowerValue = 0;
LowerProtectionLevel = 0;
UpperProtectionLevel = 0;
MaxScoreCell = 0; //initialize to NULL
MaxScoreWeightCell = 0; //initialize to NULL
MaxScoreHolding = 0; //initialize to NULL
MaxScoreWeightHolding = 0; //initialize to NULL
HoldingnrPerMaxScore = 0; //initialize to NULL
nMaxScoreCell = NumberMaxScoreCell;
if (nMaxScoreCell > 0){
// allocate memory for MaxScore and MaxScoreWeight
// and fill with values 0.0
MaxScoreCell = new double[nMaxScoreCell];
memset(MaxScoreCell, 0, sizeof(double) * nMaxScoreCell);
if (IsWeight){
MaxScoreWeightCell = new double[nMaxScoreCell];
memset(MaxScoreWeightCell, 0, sizeof(double) * nMaxScoreCell);
}
}
nMaxScoreHolding = NumberMaxScoreHolding;
if (IsHolding && nMaxScoreHolding > 0){
// allocate memory for MaxScore and MaxScoreWeight
MaxScoreHolding = new double[nMaxScoreHolding];
memset( MaxScoreHolding, 0, sizeof(double) * nMaxScoreHolding);
if (IsWeight){
MaxScoreWeightHolding = new double[nMaxScoreHolding];
memset( MaxScoreWeightHolding, 0, sizeof(double) * nMaxScoreHolding);
}
// Allocate Memory for HoldingnrMaxScore
HoldingnrPerMaxScore = new int[nMaxScoreHolding];
memset( HoldingnrPerMaxScore, -1, sizeof(int) * nMaxScoreHolding);
}
IsFilled = false;
}
// constructor used within functions. does not have the arrays for
// maxscore
CDataCell::CDataCell()
{
// FILE *fd;
// fd = fopen("E:/Temp/Debug.txt","a");
// fprintf(fd,"%Xh%s\n", this," CDatacell Const () ");
// fclose(fd);
Resp = 0;
NWResp = 0;
Cost = 0;
Shadow = 0;
CellKey = 0;
CellKeyNoZeros = 0;
Freq = 0;
Weight = 0;
FreqHolding = 0;
RoundedResp = 0;
CTAValue = 0;
CKMValue = 0;
Status = CS_EMPTY;
TempShadow = 0;
HoldingNr = WITHOUT_HOLDING;
PeepSortCell = EMPTY;
PeepSortHolding = EMPTY;
TempPeepSort = EMPTY;
PeepCell = 0;
PeepHolding = 0;
//set ini values for realized upper and lower.as 0
RealizedUpperValue = 0;
RealizedLowerValue = 0;
LowerProtectionLevel = 0;
UpperProtectionLevel = 0;
// Needed for CKMType = "D"
// Set to large double
MinScoreCell = BIGNUMBER;
MinScoreWeightCell = 0;
nMaxScoreCell = 0;
MaxScoreCell = 0;
MaxScoreWeightCell = 0;
nMaxScoreHolding = 0;
MaxScoreHolding = 0;
MaxScoreWeightHolding = 0;
HoldingnrPerMaxScore = 0;
IsFilled = false;
}
// Destructor. All arrays that are created should be cleaned.
CDataCell::~CDataCell()
{
// FILE *fd;
// fd = fopen("E:/Temp/Debug.txt","a");
// fprintf(fd,"%Xh%s\n", this," CDatacell Dest ");
// fclose(fd);
delete [] MaxScoreCell;
delete [] MaxScoreWeightCell;
delete [] MaxScoreHolding;
delete [] MaxScoreWeightHolding;
delete [] HoldingnrPerMaxScore;
}
// merges the max score holding from two cells along with the holding numbers.
// this is used in recoded tables.
void CDataCell::MergeScoreHolding(double *a, int *ah, double *b, int *bh, int n)
{
int i,j,ai = 0;
if (b == 0) return;
double *tempbval = new double [n] ;
int *tempbhol = new int [n];
for (i= 0; i< n; i++){
tempbval[i] = b[i];
tempbhol[i] = bh[i];
}
// add holdings that are equal with b and put a[i] in the temp
for (i=0; i<n; i ++){
for (j= 0; j<n; j++){
if ((ah[i] == tempbhol[j])){
a[i] = a[i] + tempbval[j];
tempbval[j] = a[i] + tempbval[j];
}
}
}
for (i = 0; i < n; i++){
if (tempbval[i] > a[ai]){
// shift down
for (j = n - 2; j >= ai; j--){
a[j + 1] = a[j];
ah[j + 1] = ah[j];
}
a[ai] = tempbval[i];
ah[ai] = tempbhol[i];
}
else{
ai++;
i--;
}
if (ai == n) break; // all elements from b smaller than a
}
delete [] tempbval;
delete [] tempbhol;
}
// merges two arrays when cells are merged. This is used in recoded tables
void CDataCell::MergeScore(double *a, double *aw, double* b, double *bw, int n) // for MaxScore and MaxScoreWeight
{
int i, j, ai = 0;
if (b == 0) return; // no scores
for (i = 0; i < n; i++){
if (b[i] > a[ai]){
// shift down
for (j = n - 2; j >= ai; j--){
a[j + 1] = a[j];
if (aw != 0){
aw[j + 1] = aw[j];
}
}
a[ai] = b[i];
if ((aw != 0) && (bw != 0 )){
aw[ai++] = bw[i];
}
}
else{
ai++;
i--;
}
if (ai == n) break; // all elements from b smaller than a
}
}
// Note the two functions below could be combined and made into one function with a boolean
// to direct you to use weight or not
// Gets the dominance Perc of cell.
double CDataCell::GetDominancePercCell(bool ApplyWeight, bool ApplyWeightOnSafetyRule, long DominanceNumber)
{
ASSERT(MaxScoreCell != 0);
if (MaxScoreCell[0] == 0) return 0;
if (Shadow == 0) return 0;
return ComputeWeightedScoreCell(ApplyWeight && ApplyWeightOnSafetyRule,DominanceNumber ) * 100.0 / Shadow;
}
// Gets the dominance Perc of holding. These two
double CDataCell::GetDominancePercHolding(bool ApplyWeight, bool ApplyWeightOnSafetyRule, long DominanceNumber)
{
ASSERT(MaxScoreHolding != 0);
if (MaxScoreHolding[0] == 0) return 0;
if (Shadow == 0) return 0;
return ComputeWeightedScoreHolding(ApplyWeight && ApplyWeightOnSafetyRule,DominanceNumber ) * 100.0 / Shadow;
}
// Gets PQ for cell.
double CDataCell::GetPQCell(double p, double q, long n, bool ApplyWeight, bool ApplyWeightOnSafetyRule)
{
if (!ApplyWeight || !ApplyWeightOnSafetyRule) {
int i;
double s = 0;
for (i = 0; i <= n; i++) {
s += MaxScoreCell[i];
}
return (p / 100) * MaxScoreCell[0] + (q / 100) * (s - Shadow);
}
// Apply weight // Is this correct?
return (p / 100) * MaxScoreCell[0] + (q / 100) *
(ComputeWeightedScoreCell(true,n+1) - Shadow);
}
// gets PQ holding
double CDataCell::GetPQHolding(double p, double q, long n, bool ApplyWeight, bool ApplyWeightOnSafetyRule)
{
if (!ApplyWeight || !ApplyWeightOnSafetyRule) {
int i;
double s = 0;
for (i = 0; i <= n; i++) {
s += MaxScoreHolding[i];
}
return (p / 100) * MaxScoreHolding[0] + (q / 100) * (s - Shadow);
}
// Apply weight
return (p / 100) * MaxScoreHolding[0] + (q / 100) * (ComputeWeightedScoreHolding(true,n+1) - Shadow);
}
// Each of the top n is factored with the corresponding weights.
double CDataCell::ComputeWeightedScoreCell(bool DoWeight, long NumberOfScores )
{
int i;
double score = 0;
if (DoWeight) {
double WeightScore = 0;
for (i = 0; i < NumberOfScores; i++) {
if (WeightScore + MaxScoreWeightCell[i] < NumberOfScores) {
score += MaxScoreCell[i] * MaxScoreWeightCell[i];
WeightScore += MaxScoreWeightCell[i];
}
else {
score += MaxScoreCell[i] * (NumberOfScores - WeightScore); // remaining part
break;
}
}
return score;
}
// no weight
for (i = 0; i < NumberOfScores; i++) {
score += MaxScoreCell[i];
}
return score;
}
// each of the top n holdings is factored with holdings.
double CDataCell::ComputeWeightedScoreHolding(bool DoWeight, long NumberOfScores )
{
int i;
double score = 0;
if (DoWeight) {
double WeightScore = 0;
for (i = 0; i < NumberOfScores; i++) {
if (WeightScore + MaxScoreWeightHolding[i] < NumberOfScores) {
score += MaxScoreHolding[i] * MaxScoreWeightHolding[i];
WeightScore += MaxScoreWeightHolding[i];
}
else {
score += MaxScoreHolding[i] * (NumberOfScores - WeightScore); // remaining part
break;
}
}
return score;
}
// no weight
for (i = 0; i < NumberOfScores; i++) {
score += MaxScoreHolding[i];
}
return score;
}
// compare two cells. comparison bases is frequency, shadow, response and cost.
// returns true if the two cells are equal and false if not.
bool CDataCell::Compare(const CDataCell &a) const
{
return Freq == a.Freq
&& DBL_EQ(Resp, a.Resp)
&& DBL_EQ(Shadow, a.Shadow)
&& DBL_EQ(Cost, a.Cost);
}
/*double CDataCell::GetCost(double Lambda)
{
if (Lambda > 0) {
return pow(Cost, Lambda);
}
else
{
return log(Cost + 1);
}
}*/
void CDataCell::Write(){
printf("NumberofMaxScoreCell = %d\n",nMaxScoreCell);
for (int i=0;i<nMaxScoreCell;i++){
printf("%7.5lf, ",MaxScoreCell[i]);
}
}