-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHitas.cpp
264 lines (224 loc) · 7.54 KB
/
Hitas.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
/*
* 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 <cstdio>
#include <cmath>
#include "Hitas.h"
using namespace std;
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Write files to be used in Hitas
CHitas::CHitas()
{
NameTabFile = "Hitastab.txt";
NameSecFile = "hitassec.txt";
}
CHitas::~CHitas()
{
}
/*
[Frequency rule]
MINCOUNT=3 ; Cell should contain at least (MINCOUNT + 1) contributants // SafeMinRec minus 1 in avdw's geval
[Dominance rule] // !!! is vervallen !!!!
DOMCOUNT=1 ; Largest DOMCOUNT should contribute less than
DOMPRO=0.7 ; DOMPRO times cellvalue
[Safety ranges]
LOWERMARG=0.99 ; lowerbound in case of marginal cell // zo laten
UPPERMARG=1.01 ; same for upperbound // zo laten
[Costs]
DISTANCE=0 ; 0 if cost specified in BTab.dat, 1 if distance is to be used // 0 geen afstandsfunctie, 1 Anco
D1=1 3 5 9 17 ; costs var1 distance=1, 2, 3, 4, 5 (=max cost)
D2=1 3 5 9 17 ; costs var2 distance=1, 2, 3, 4, 5 (=max cost)
D3=1 3 5 17 17 ; costs var3 distance=1, 2, 3, 4, 5 (=max cost)
[Misc]
MINTABVAL=0 ; Each cell is non-negative // ondergrens
MAXTABVAL=150000000 ; and less than MAXTABVAL // totaal generaal van Resp (Shadow? Anco)
OUTDIR=c:/knb/hitas/test/ ; Will contain ouput-files (= TEMP-dir?) // werkdirectory
*/
bool CHitas::WriteParameterFile(FILE *fd, CTable& tab)
{
fprintf(fd, "[Frequency rule]\n");
fprintf(fd, "MINCOUNT=%d\n\n", tab.SafeMinRec);
/*
fprintf(fd, "[Dominance rule]\n");
fprintf(fd, "DOMCOUNT=%d\n", tab.DominanceNumber);
fprintf(fd, "DOMPRO=0.%d\n\n", tab.DominancePerc);
*/
fprintf(fd, "[Safety ranges]\n");
fprintf(fd, "LOWERMARG=0.99\n");
fprintf(fd, "UPPERMARG=1.01\n\n");
fprintf(fd, "[Misc]\n");
fprintf(fd, "MINTABVAL=0\n");
fprintf(fd, "MAXTABVAL=%.0f\n\n", tab.GetCell(0L)->GetResp() * 1.5); // should be enough
// MakeTempPath(); Wordt nu door TAU doorgegeven.
// fprintf(fd, "OUTDIR=%s\n\n", (LPCTSTR) TempPath);
fprintf(fd, "[Costs]\n");
fprintf(fd, "DISTANCE=0\n");
return true;
}
/*
3 // aantal dimensies tabel
c:\knb\hitas\test3\data\SBI3.txt // naam bestand met opspanvariabele 1
c:\knb\hitas\test3\data\GK3.txt // naam bestand met opspanvariabele 2
c:\knb\hitas\test3\data\Regio3.txt // naam bestand met opspanvariabele 3
c:\knb\hitas\test3\data\BTab.dat // naam bestand met basistabel
c:\knb\hitas\test\status3.dat // naam bestand met resultaat (alleen secundaire cellen)
*/
bool CHitas::WriteFilesFile(FILE *fd, CTable &tab, CVariable *var)
{
int i;
// string fname/*, varname*/;
//char fname[MAX_PATH];
//char varname[10];
string fname;
string varname;
fprintf(fd, "%ld\n", tab.nDim); // dimensies
// name files with codelists
for (i = 0; i < tab.nDim; i++) {
//sprintf(fname, "%shitasv%d.txt", TempPath.c_str(), i + 1);
fname = TempPath + "hitasv" + std::to_string(i+1) + ".txt";
varname = "Var_" + std::to_string(i+1);
fprintf(fd, "%s\n", fname.c_str());
var[tab.ExplVarnr[i]].WriteCodelist(fname.c_str(), ".", varname.c_str(), true);
}
// name file with table
//sprintf(fname, "%s%s", TempPath.c_str(), NameTabFile.c_str());
fname = TempPath + NameTabFile;
fprintf(fd, "%s\n", fname.c_str());
// write cells
WriteCellFile(fname.c_str(), tab, var);
// name file for result with secondary cell dimensions
//sprintf(fname, "%s%s", TempPath.c_str(), NameSecFile.c_str());
fname = TempPath + NameSecFile;
fprintf(fd, "%s\n", fname.c_str());
return true;
}
bool CHitas::WriteCellFile(LPCTSTR FileName, CTable &tab, CVariable *var)
{
long dimsCell[MAXDIM];
long dimsCodeList[MAXDIM];
FILE *fd;
fd = fopen(FileName, "w");
if (fd == 0) return 0;
WriteCellDim(fd, tab, var, dimsCell, dimsCodeList, 0);
fclose(fd);
return true;
}
bool CHitas::WriteCellDim(FILE *fd, CTable &tab, CVariable *var,
long *dimsCell, long *dimsCodeList, int niv)
{
int i, c;
int nDecResp, F;
double C;
if (niv == tab.nDim) {
nDecResp = var[tab.ResponseVarnr].nDec;
// double dRoundConst = (0.5)/pow(10,nDecResp);
CDataCell *dc = tab.GetCell(dimsCell);
double cell = dc->GetResp();
for (i = 0; i < tab.nDim; i++) {
fprintf(fd, "%5ld ", dimsCodeList[i]);
}
fprintf(fd, "%12.*f ", nDecResp, cell);
double dRoundConst = (1.0)/pow(10,nDecResp);
switch (dc->GetStatus() ) {
case CS_SAFE:
case CS_SAFE_MANUAL:
case CS_EMPTY_NONSTRUCTURAL:
fprintf(fd, "s ");
break;
case CS_UNSAFE_RULE:
case CS_UNSAFE_FREQ:
case CS_UNSAFE_PEEP:
case CS_UNSAFE_ZERO:
case CS_UNSAFE_SINGLETON:
case CS_UNSAFE_SINGLETON_MANUAL:
case CS_UNSAFE_MANUAL:
fprintf(fd, "u ");
// fprintf(fd, "%c", 117);
// if (dc->GetFreq() <= tab.SafeMinRec) {
// fprintf(fd, "1 1 "); // Anco, niet altijd sporend met de wensch van PP
// } else {
double UPL, LPL; // Sliding, Capacity;
LPL =0; UPL =0; // Sliding =0; Capacity = 0;
//fprintf(fd, "%.0f %.0f ", UPL, LPL);
LPL = dc->GetLowerProtectionLevel();
UPL = dc->GetUpperProtectionLevel();
nDecResp = nDecResp + 3;
// if (LPL < dRoundConst) LPL = dRoundConst;
// lijkt me niet nodog. Alleen de bovengrens is voldoende
if (UPL < dRoundConst) UPL = dRoundConst;
// fprintf(fd, "%12.*f ", nDecResp, LPL+dRoundConst);
// fprintf(fd, "%12.*f ", nDecResp, UPL+dRoundConst);
// fprintf(fd, "%12.*f ", nDecResp, LPL);
// fprintf(fd, "%12.*f ", nDecResp, UPL);
// fprintf(fd, "a %12.*f %12.*f ", nDecResp, LPL, nDecResp, UPL);
fprintf(fd, "%12.*f %12.*f ", nDecResp, LPL, nDecResp, UPL);
// }
break;
case CS_PROTECT_MANUAL:
case CS_EMPTY:
fprintf(fd, "z ");
break;
case CS_SECONDARY_UNSAFE:
case CS_SECONDARY_UNSAFE_MANUAL:
return false;
break;
default:
ASSERT(false);
}
// freq
F = dc->GetFreq ();
if (tab.ApplyHolding) F = dc->GetFreqHolding();
fprintf(fd, "%d ", F ); // AHNL 6.1.2004 Ik heb F nodig bij de minCost
// MinCost => eps bij scorende nul-cellen
// cost
C = dc->GetCost(tab.Lambda);
if ( C == 0 && F > 0) C = 0.00001;
if (tab.CostVarnr < 0) {
//fprintf(fd, "%.0f ", dc->GetCost(tab.Lambda) );
//fprintf(fd, "%G ", dc->GetCost(tab.Lambda) );
fprintf(fd, "%G ", C );
}
else {
// int nDecCost = var[tab.CostVarnr].nDec;
// fprintf(fd, "%.0f ", dc->GetCost(tab.Lambda) );
// fprintf(fd, "%G ", dc->GetCost(tab.Lambda) );
fprintf(fd, "%G ", C );
}
fprintf(fd, "\n");
return true;
}
CVariable *v = &(var[tab.ExplVarnr[niv]]);
CCode *phCode = v->GethCode();
int k = v->GetnCode();
for (i = c = 0; i < k; i++, c++) {
dimsCell[niv] = i;
if (!v->IsHierarchical || phCode[i].nChildren != 1) {
dimsCodeList[niv] = c;
WriteCellDim(fd, tab, var, dimsCell, dimsCodeList, niv + 1);
}
else {
c--;
}
}
return true;
}