Questo sito utilizza cookies, anche di terze parti, per mostrare pubblicità e servizi in linea con il tuo account. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Problema compilazione: ofstream
Forum - C/C++ - Problema compilazione: ofstream

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
paky88fg (Ex-Member)
Rookie


Messaggi: 38
Iscritto: 07/04/2009

Segnala al moderatore
Postato alle 18:55
Giovedì, 18/03/2010
Salve ragazzi, premetto che di C++ non ne so niente,  ma ho necessità di compilare un insieme di file scritti in C++ e la compilazione mi da i seguenti errori:

Codice sorgente - presumibilmente Delphi

  1. gen.h:26: error: ‘ostream’ has not been declared
  2. gen.h:45: error: ‘ostream’ has not been declared
  3. gen.h:64: error: ‘ostream’ has not been declared
  4. gen.h:95: error: ‘ostream’ has not been declared
  5. gen.h:123: error: ‘ofstream’ has not been declared
  6. gen.h:124: error: ‘ofstream’ has not been declared
  7. gen.h:125: error: ‘ofstream’ has not been declared
  8. gen.h:160: error: ‘ofstream’ has not been declared
  9. gen.h:182: error: ‘ofstream’ has not been declared
  10. gen.h:183: error: ‘ofstream’ has not been declared
  11. gen.h:183: error: ‘StringSet’ has not been declared
  12. gen.h:221: error: ‘ofstream’ has not been declared
  13. gen.h:222: error: ‘ofstream’ has not been declared
  14. gen.h:265: error: ‘ofstream’ has not been declared
  15. gen.h:266: error: ‘ofstream’ has not been declared
  16. gen.h:288: error: ‘ofstream’ has not been declared
  17. gen.h:289: error: ‘ofstream’ has not been declared
  18. gen.c:10: error: variable or field ‘write’ declared void
  19. gen.c:10: error: ‘ostream’ was not declared in this scope
  20. gen.c:10: error: ‘fp’ was not declared in this scope
  21. gen.c:20: error: variable or field ‘write’ declared void
  22. gen.c:20: error: ‘ostream’ was not declared in this scope
  23. gen.c:20: error: ‘fp’ was not declared in this scope
  24. gen.c:77: error: variable or field ‘write’ declared void
  25. gen.c:77: error: ‘ostream’ was not declared in this scope
  26. gen.c:77: error: ‘fp’ was not declared in this scope
  27. gen.c:92: error: variable or field ‘write’ declared void
  28. gen.c:92: error: ‘ostream’ was not declared in this scope
  29. gen.c:92: error: ‘fp’ was not declared in this scope
  30. gen.c:167: error: variable or field ‘write’ declared void
  31. gen.c:167: error: ‘ofstream’ was not declared in this scope
  32. gen.c:167: error: ‘fp’ was not declared in this scope



e nel file gen.h ci sono le seguenti inclusioni:

Codice sorgente - presumibilmente C/C++

  1. #include <stream.h>
  2. #include <fstream.h>



ho provato anche a togliere i .h come avevo letto in qualche forum ma non ha funzionato.
Qualcuno di voi sa di cosa potrebbe trattarsi?

Grazie mille :)


PM Quote
Avatar
Poggi Marco (Member)
Guru


Messaggi: 951
Iscritto: 05/01/2010

Segnala al moderatore
Postato alle 19:44
Giovedì, 18/03/2010
Posta il programma. IDE diversi, danno messaggi diversi. Inoltre, solo dai messaggi
d' errore, è difficile aiutarti.


Nulla va più veloce della luce, quindi rilassati.
PM Quote
Avatar
paky88fg (Ex-Member)
Rookie


Messaggi: 38
Iscritto: 07/04/2009

Segnala al moderatore
Postato alle 19:51
Giovedì, 18/03/2010
sono messaggi di un semplice make da terminale

Comunque il file in questione è questo:

Codice sorgente - presumibilmente C++

  1. #include "glob.h"
  2. #include "dist.h"
  3. #include <stream.h>
  4. #include <fstream.h>
  5.  
  6.  
  7. //=============================  Parameters  =============================
  8.  
  9.  
  10. // Parameters used for StringSet
  11. // StringSet can be either large itemsets, or sequences
  12. //
  13. class PatternPar {
  14. public:
  15.   LINT npats;   // number of patterns
  16.   FLOAT patlen; // average length of pattern
  17.   FLOAT corr;   // correlation between consecutive patterns
  18.   FLOAT conf;   // average confidence in a rule
  19.   FLOAT conf_var;  // variation in the confidence
  20.   LINT seed;
  21.  
  22.   PatternPar(void)
  23.     : npats(10000), patlen(4.0), corr(0.25), conf(0.75), conf_var(0.1), seed(0)
  24.   {}
  25.  
  26.   void write(ostream &fp);
  27. };
  28.  
  29.  
  30. // Parameters used to generate transactions
  31. //
  32. class TransPar {
  33. public:
  34.   LINT ntrans;  // number of transactions in database
  35.   FLOAT tlen;   // average transaction length
  36.   LINT nitems;  // number of items
  37.   PatternPar lits;      // parameters for potentially large itemsets
  38.   BOOLEAN ascii;        // Generate in ASCII format
  39.   LINT seed;    // Seed to initialize RandSeed with before x-act generation
  40.  
  41.   TransPar(void)
  42.     : ntrans(1000000), tlen(10), nitems(100000), ascii(FALSE), seed(INIT_SEED)
  43.   {}
  44.  
  45.   void write(ostream &fp);
  46. };
  47.  
  48.  
  49. // Parameters used to generate transactions
  50. //
  51. class TaxPar:public TransPar {
  52. public:
  53.   LINT nroots;   // number of roots
  54.   FLOAT fanout;  // average fanout at each interiori node
  55.   FLOAT nlevels; // average number of levels
  56.   FLOAT depth_ratio;     // affects ratio of itemsets chosen from higher levels
  57.  
  58.   TaxPar(void)
  59.     : nroots(0), fanout(0), nlevels(0), depth_ratio(1)
  60.   {}
  61.  
  62.   void calc_values(void);       // calculates nroots, given nlevels
  63.         // default values: nroots = 250, fanout = 5
  64.   void write(ostream &fp);
  65. };
  66.  
  67.  
  68. // Parameters used to generate sequences
  69. //
  70. class SeqPar {
  71. public:
  72.   LINT ncust;   // number of customers in database
  73.   FLOAT slen;   // average sequence length
  74.   FLOAT tlen;   // average transaction length
  75.   LINT nitems;  // number of items
  76.  
  77.   FLOAT rept;           // repetition-level (between 0 and 1)
  78.   FLOAT rept_var;       // variation in repetition-level
  79.  
  80.   BOOLEAN ascii;        // Generate in ASCII format
  81.  
  82.   PatternPar lits;      // parameters for potentially large itemsets
  83.   PatternPar lseq;      // parameters for potentially large sequences
  84.  
  85.   SeqPar(void)
  86.     : ncust(100000), slen(10), tlen(2.5), nitems(10000),
  87.       rept(0), rept_var(0.1)
  88.   {
  89.     lits.npats = 25000;
  90.     lseq.npats = 5000;
  91.     lits.patlen = 1.25;
  92.     lseq.patlen = 4.0;
  93.   }
  94.  
  95.   void write(ostream &fp);
  96. };
  97.  
  98.  
  99. //------------------------------ Taxonomy ------------------------------
  100.  
  101.  
  102. //
  103. // models taxonomy over items as a tree
  104. // 0 is a valid item here (get rid of it when actually adding item
  105. //
  106. class Taxonomy
  107. {
  108.   friend class TaxStat;
  109. private:
  110.   LINT nitems;  // number of items
  111.   LINT nroots;  // number of roots
  112.   FLOAT depth;  // used when assigning probabilities to items
  113.  
  114.   LINT *par;
  115.   LINT *child_start;
  116.   LINT *child_end;
  117.  
  118.   static const LINT item_len;  // ASCII field-width of item-id
  119. public:
  120.   Taxonomy(LINT nitems, LINT nroots, FLOAT fanout, FLOAT depth_ratio);
  121.   ~Taxonomy(void);
  122.  
  123.   void write(ofstream &fp);             // write taxonomy to file
  124.   void write_asc(ofstream &fp); // write taxonomy to ASCII file
  125.   void display(ofstream &fp);   // display taxonomy (for user)
  126.  
  127.   FLOAT depth_ratio(void) { return depth; }
  128.   LINT num_roots(void) { return nroots; }
  129.   LINT root(Item itm) { return (par[itm] == -1); }
  130.  
  131.   LINT num_children(Item itm) { return child_end[itm] -  child_start[itm]; }
  132.   LINT child(Item itm, LINT n) { return child_start[itm]+n; }
  133.         // returns the n'th child of itm
  134.   LINT first_child(Item itm) { return child_start[itm]; }
  135.   LINT last_child(Item itm) { return child_end[itm]-1; }
  136.  
  137.   Item parent(Item itm) { return par[itm]; }    // -1 => no parent
  138. };
  139.  
  140.  
  141. //--------------------------------------------------------------------------
  142.  
  143.  
  144. //
  145. // 0 is a valid item here (get rid of it when actually adding item
  146. //
  147. class ItemSet
  148. {
  149. private:
  150.   LINT nitems;          // number of items
  151.   Taxonomy *tax;        // taxonomy (optional)
  152.   FLOAT *cum_prob;      // cumulative probability
  153.   FLOAT *tax_prob;      // cumulative probability of choosing a child
  154.   UniformDist rand;
  155.  
  156.   void normalize(FLOAT prob[], LINT low, LINT high);
  157. public:
  158.   ItemSet(LINT nitems, Taxonomy *tax = NULL);
  159.   ~ItemSet();
  160.   void display(ofstream &fp);
  161.   Item get_item(void);          // returns a random item (weighted)
  162.   Item specialize(Item itm);    // if no taxonomy, returns itm
  163.   FLOAT weight(Item itm);       // returns prob. of choosing item
  164. };
  165.  
  166.  
  167. class String
  168. {
  169. friend class StringSet;
  170.   LINT nitems;  // number of items
  171.   Item *items;  // list of the items
  172. //  FLOAT *rval;        // random value (used to get random ordering of the items)
  173. //  Item *ritems;       // randomly chosen items
  174.   FLOAT prob;   // probability that this string is chosen
  175.   FLOAT conf;   // probability that this string is corrrupted
  176.  
  177. //  void shuffle(void); // shuffles items in string
  178. public:
  179.   String(LINT nitems);
  180.   ~String(void);
  181.  
  182.   void display(ofstream &fp, LINT prob_comp = 1);
  183.   void display(ofstream &fp, StringSet &lits, LINT prob_comp = 1);
  184.         // prob is multiplied by prob_comp before being writeed
  185.  
  186.   LINT size(void) { return nitems;}
  187.   Item item(LINT n) { return items[n];} // return nth item of the string
  188.   FLOAT conf_lvl(void) { return conf; }
  189.  
  190.   void set_size(LINT newsize) { nitems = newsize;}
  191.   void set_item(LINT n, Item itm) { items[n] = itm;}
  192.   void set_conf_lvl(FLOAT newconf) { conf = newconf; }
  193.  
  194. //  void shuffle(LINT k);       // allows selection of k random items from the string
  195. //  Item rand_item(LINT n) { return ritems[n];} // works with shuffle
  196. };
  197.  
  198. typedef String *StringP;
  199.  
  200.  
  201. class StringSet
  202. {
  203.   friend class StringSetIter;
  204. private:
  205.   ItemSet *items;
  206.   Taxonomy *tax;
  207.   LINT npats;           // number of patterns
  208.   StringP *pat;         // array of patterns
  209.   StringP answer;
  210.   FLOAT *cum_prob;      // cumulative probabilities of patterns
  211.  
  212.   StringP specialize(LINT i);   // specializes pattern #i
  213. public:
  214.   StringSet(LINT nitems,          // number of items
  215.             PatternPar par,       // npats, patlen, corr, conf & conf_var
  216.             Taxonomy *tax = NULL, // taxonomy (optional)
  217.             FLOAT rept = 0,       // repetition-level
  218.             FLOAT rept_lvl = 0.2  // variation in repetition-level
  219.             );
  220.   ~StringSet();
  221.   void display(ofstream &fp);   // for large itemsets
  222.   void display(ofstream &fp, StringSet &lit);   // for sequences
  223.   StringP get_pat(LINT i);      // returns pattern #i
  224. };
  225.  
  226.  
  227. class StringSetIter
  228. {
  229. private:
  230.   UniformDist rand;
  231.   StringSet *strset;
  232.   LINT last_pat;        // if -ve, unget_pat() was called
  233. public:
  234.   StringSetIter(StringSet &str_set) : strset(&str_set), last_pat(0) {};
  235.   StringP get_pat(void);        // returns a random pattern
  236.   void unget_pat(void);         // the last pattern is put back in the sequence
  237. };
  238.  
  239.  
  240. //--------------------------------------------------------------------------
  241.  
  242.  
  243. class Transaction {
  244. private:
  245.   LINT tlen;    // expected number of items in transaction
  246.   LINT nitems;  // number of items currently in transaction
  247.   LINT maxsize; // size of array items
  248.   LINT *items;  // items in the transaction
  249.  
  250.   static const LINT cid_len;   // ASCII field-width of customer-id
  251.   static const LINT tid_len;   // ASCII field-width of transaction-id
  252.   static const LINT item_len;  // ASCII field-width of item-id
  253.  
  254.   static LINT tid;      // transaction-id
  255.  
  256.   void sort(void);
  257.   BOOLEAN add_item(LINT itm);// returns TRUE if added, FALSE if already present
  258. public:
  259.   Transaction(LINT sz);
  260.   ~Transaction();
  261.  
  262.   BOOLEAN add(String &pat, BOOLEAN corrupt = TRUE);
  263.         // adds pattern to transaction
  264.         // returns TRUE if added, FALSE if trans. full
  265.   void write(ofstream &fp, LINT cid = 0);
  266.   void write_asc(ofstream &fp, LINT cid = 0);
  267.   LINT size(void) { return nitems; }
  268. };
  269.  
  270. typedef Transaction *TransactionP;
  271.  
  272.  
  273. class CustSeq {
  274. private:
  275.   Cid cid;      // customer-id
  276.   LINT slen;    // expected number of transactions in sequence
  277.   LINT tlen;    // avg. expected number of items in a transaction
  278.   LINT ntrans;  // number of transactions in sequence
  279.   LINT nitems;  // number of items in sequence
  280.   LINT maxsize; // size of array trans
  281.   TransactionP *trans;  // transaction in the sequence
  282.  
  283. public:
  284.   CustSeq(Cid cid, LINT seq_len, LINT tot_items);
  285.   ~CustSeq(void);
  286.  
  287.   BOOLEAN add(String &pat, StringSet &lits);    // adds pattern to transaction
  288.   void write(ofstream &fp);
  289.   void write_asc(ofstream &fp);
  290.   LINT size(void) { return nitems; }
  291. };



PM Quote
Avatar
paky88fg (Ex-Member)
Rookie


Messaggi: 38
Iscritto: 07/04/2009

Segnala al moderatore
Postato alle 19:52
Giovedì, 18/03/2010
e questo è il file gen.c

Codice sorgente - presumibilmente Delphi

  1. #include "gen.h"
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6.  
  7. //------------------------------- Parameters -------------------------------
  8.  
  9.  
  10. void PatternPar::write(ostream &fp)
  11. {
  12.   fp << "\tNumber of patterns = " << npats << endl;
  13.   fp << "\tAverage length of pattern = " << patlen << endl;
  14.   fp << "\tCorrelation between consecutive patterns = " << corr << endl;
  15.   fp << "\tAverage confidence in a rule = " << conf << endl;
  16.   fp << "\tVariation in the confidence = " << conf_var << endl;
  17. }
  18.  
  19.  
  20. void TransPar::write(ostream &fp)
  21. {
  22.   fp << "Number of transactions in database = " << ntrans << endl;
  23.   fp << "Average transaction length = " << tlen << endl;
  24.   fp << "Number of items = " << nitems << endl;
  25.  
  26.   fp << "Large Itemsets:" << endl;
  27.   lits.write(fp);
  28.   fp << endl;
  29. }
  30.  
  31.  
  32. // calculate the number of roots, given the number of levels
  33. void TaxPar::calc_values(void)
  34. {
  35.   LINT nset;
  36.  
  37.   nset = 0;
  38.   nset += (nlevels != 0);
  39.   nset += (fanout != 0);
  40.   nset += (nroots != 0);
  41.  
  42.   switch (nset)
  43.     {
  44.     case 0:     // fill in defaults
  45.       nroots = 250;
  46.       fanout = 5;
  47.       return;
  48.  
  49.     case 1:     // need to fill in 1 value
  50.       assert (nlevels == 0);
  51.       if (fanout == 0)
  52.         fanout = 5;
  53.       else if (nroots == 0)
  54.         nroots = 250;
  55.       return;
  56.  
  57.     case 2:
  58.       if (nlevels == 0)         // all set!
  59.         return;
  60.       if (fanout != 0) {        // calculate nroots
  61.         nroots = nitems / (1 + pow(DOUBLE(fanout), DOUBLE(nlevels-1)));
  62.         if (nroots < 1)
  63.           nroots = 1;
  64.       }
  65.       else if (nroots != 0) {   // calculate fanout
  66.         FLOAT temp;
  67.         temp = (FLOAT)nitems / nroots - 1;
  68.         temp = log((DOUBLE)temp) / (nlevels - 1);
  69.         fanout = exp((DOUBLE)temp);
  70.       }
  71.     case 3:                     // all set!
  72.       return;
  73.     }
  74. }
  75.  
  76.  
  77. void TaxPar::write(ostream &fp)
  78. {
  79.   fp << "Number of transactions in database = " << ntrans << endl;
  80.   fp << "Average transaction length = " << tlen << endl;
  81.   fp << "Number of items = " << nitems << endl;
  82.   fp << "Number of roots = " << nroots << endl;
  83.   fp << "Number of levels = " << nlevels << endl;
  84.   fp << "Average fanout = " << fanout << endl;
  85.  
  86.   fp << "Large Itemsets:" << endl;
  87.   lits.write(fp);
  88.   fp << endl;
  89. }
  90.  
  91.  
  92. void SeqPar::write(ostream &fp)
  93. {
  94.   fp << "Number of customers in database = " << ncust << endl;
  95.   fp << "Average sequence length = " << slen << endl;
  96.   fp << "Average transaction length = " << tlen << endl;
  97.   fp << "Number of items = " << nitems << endl;
  98.   fp << "Repetition-level = " << rept << endl;
  99.   fp << "Variation in repetition-level = " << rept_var << endl;
  100.  
  101.   fp << "Large Itemsets:" << endl;
  102.   lits.write(fp);
  103.   fp << "Large Sequences:" << endl;
  104.   lseq.write(fp);
  105.   fp << endl;
  106. }
  107.  
  108.  
  109. //------------------------------ Taxonomy ------------------------------
  110.  
  111. const LINT Taxonomy::item_len = 7;
  112.  
  113. //
  114. // Constructor: reads taxonomy file and builds taxonomy as a DAG
  115. //
  116. Taxonomy::Taxonomy(LINT num_items,      // total number of items
  117.                    LINT num_roots,      // number of roots
  118.                    FLOAT fanout,        // average fanout
  119.                    FLOAT depth_ratio    // average ratio of ....
  120.                    )
  121.   : nitems(num_items), nroots(num_roots), depth(depth_ratio)
  122. {
  123.   LINT i, j;
  124.   LINT next_child;
  125.   PoissonDist nchildren(fanout-1);      // string length
  126.  
  127.   // allocate memory
  128.   par = new LINT [nitems];
  129.   child_start = new LINT [nitems];
  130.   child_end = new LINT [nitems];
  131.  
  132.   next_child = nroots;
  133.  
  134.   // initialize parents (or lack thereof) for roots
  135.   for (i = 0; i < nroots; i++)
  136.     par[i] = -1;
  137.  
  138.   // set up all the interior nodes
  139.   for (i = 0, j = next_child; i < nitems && next_child < nitems; i++)
  140.     {
  141.       child_start[i] = next_child;
  142.       next_child += nchildren() + 1;
  143.       if (next_child > nitems)
  144.         next_child = nitems;
  145.       child_end[i] = next_child;
  146.       for (; j < next_child; j++)
  147.         par[j] = i;
  148.     }
  149.  
  150.   // initialize children (or lack thereof) for all the leaves
  151.   for (; i < nitems; i++)
  152.     child_start[i] =
  153.     child_end[i] = -1;
  154. }
  155.  
  156.  
  157. Taxonomy::~Taxonomy(void)
  158. {
  159.   LINT i;
  160.  
  161.   delete [] par;
  162.   delete [] child_start;
  163.   delete [] child_end;
  164. }
  165.  
  166.  
  167. void Taxonomy::write(ofstream &fp)
  168. {
  169.   for (LINT i = 0; i < nitems; i++)
  170.     if (par[i] >= 0) {
  171.       assert(i != par[i]);
  172.       fp.write((char *)&i, sizeof(LINT));
  173.       fp.write((char *)&par[i], sizeof(LINT));
  174.     }
  175. }
  176.  
  177.  
  178. void Taxonomy::write_asc(ofstream &fp)
  179. {
  180.   for (LINT i = 0; i < nitems; i++)
  181.     if (par[i] >= 0) {
  182.       assert(i != par[i]);
  183.       fp << setw(item_len) << i << " " << setw(item_len) << par[i] << endl;
  184.     }
  185. }
  186.  
  187.  
  188. void Taxonomy::display(ofstream &fp)
  189. {
  190.   fp << "Taxonomy: " << endl;
  191.   for (LINT i = 0; i < nitems && child_start[i] > 0; i++)
  192.     fp << i << " " << child_start[i] << " " << child_end[i]-1 << endl;
  193.   fp << endl;
  194. }
  195.  
  196.  
  197. //------------------------------- ItemSet -------------------------------
  198.  
  199.  
  200. ItemSet::ItemSet(LINT num_items,        // number of items
  201.                  Taxonomy *ptax         // taxonomy (optional)
  202.                  )
  203.   : tax(ptax), nitems(num_items)
  204. {
  205.   ExpDist freq;
  206.   LINT i, j;
  207.  
  208.   cum_prob = new FLOAT [nitems];
  209.   if (tax)
  210.     tax_prob = new FLOAT [nitems];
  211.   else
  212.     tax_prob = NULL;
  213.   for (i = 0; i < nitems; i++)
  214.     cum_prob[i] = freq();       // prob. that this pattern will be picked
  215.  
  216.   if (tax) {                    // weight(itm) += wieght(children)
  217.     // normalize probabilities for the roots and for children
  218.     normalize(cum_prob, 0, tax->num_roots()-1);
  219.     for (i = 0; i < nitems && tax->num_children(i) > 0; i++)
  220.       normalize(cum_prob, tax->first_child(i), tax->last_child(i));
  221.  
  222.     // calulate cumulative probabilities for children
  223.     for (i = 0; i < nitems; i++)
  224.       tax_prob[i] = cum_prob[i];
  225.     for (i = 1; i < nitems; i++)
  226.       if (tax->num_children(i) > 0)
  227.         for (j = tax->first_child(i); j < tax->last_child(i); j++)
  228.           tax_prob[j+1] += tax_prob[j];
  229.  
  230.     // set real probabilities
  231.     for (i = tax->num_roots(); i < nitems; i++)
  232.       cum_prob[i] *= cum_prob[ tax->parent(i) ] * tax->depth_ratio();
  233.   }
  234.  
  235.   // normalize probabilites (why -- see get_pat)
  236.   normalize(cum_prob, 0, nitems-1);
  237.   for (i = 1; i < nitems; i++)  // calulate cumulative probabilities
  238.     cum_prob[i] += cum_prob[i-1];
  239. }
  240.  
  241.  
  242. ItemSet::~ItemSet()
  243. {
  244.   delete [] cum_prob;
  245. }
  246.  
  247.  
  248. //  normalize probabilities between low and high
  249. //
  250. void ItemSet::normalize(FLOAT prob[], LINT low, LINT high)
  251. {
  252.   FLOAT tot;
  253.   LINT i;
  254.  
  255.   // normalize probabilites
  256.   tot = 0;
  257.   for (i = low; i <= high; i++)
  258.     tot += prob[i];
  259.   for (i = low; i <= high; i++)
  260.     prob[i] /= tot;
  261. }
  262.  
  263.  
  264. // returns a pattern chosen at random
  265. //
  266. Item ItemSet::get_item(void)
  267. {
  268.   FLOAT r;
  269.   LINT i;
  270.  
  271.   // find the desired pattern using cum_prob table
  272.   r = rand();
  273.   // want item i such that cum_prob[i-1] < r <= cum_prob[i];
  274.   i = r * nitems;                       // guess location of item
  275.   i += (r-cum_prob[i]) * nitems;        // refine guess
  276.   if (i >= nitems)                      // check boundaries
  277.     i = nitems-1;
  278.   if (i < 0)
  279.     i = 0;
  280.   while ( i < (nitems-1) && r > cum_prob[i] )   // find item
  281.     i++;
  282.   while ( i > 0 && r <= cum_prob[i-1] )
  283.     i--;
  284.   return i;
  285. };
  286.  
  287.  
  288. // if no taxonomy, returns itm
  289. //
  290. Item ItemSet::specialize(Item itm)
  291. {
  292.   FLOAT r;
  293.   LINT i, nchildren;
  294.   Item first, last;
  295.  
  296.   if (!tax)             // no taxonomy
  297.     return itm;
  298.  
  299.   nchildren = tax->num_children(itm);
  300.   if (nchildren == 0)           // no children
  301.     return itm;  
  302.  
  303.   first = tax->child(itm, 0);
  304.   last = tax->child(itm, nchildren-1);
  305.  
  306.   // find the desired pattern using cum_prob table
  307.   r = rand();
  308.   i = first + r * nchildren;
  309.   if (i == last)
  310.     i--;
  311.   while ( i < last && r > tax_prob[i] )
  312.     i++;
  313.   while ( i > first && r < tax_prob[i-1] )
  314.     i--;
  315.   return specialize(i);
  316. }  
  317.  
  318.  
  319. FLOAT ItemSet::weight(Item itm) // returns prob. of choosing item
  320. {
  321.   if (itm == 0)
  322.     return cum_prob[itm];
  323.   else
  324.     return cum_prob[itm] - cum_prob[itm-1];
  325. }
  326.  
  327.  
  328. void ItemSet::display(ofstream &fp)
  329. {
  330. //  if (tax != NULL)
  331. //    tax->display(fp);
  332.  
  333.   fp << "Items:" << endl;
  334.   fp << setprecision(3);
  335.  
  336.   if (tax != NULL) {
  337.     if (cum_prob[0] * nitems > 10)
  338.       fp << 0 << "  " << cum_prob[0] * nitems << " "
  339.         << tax->first_child(0) << " " << tax->last_child(0) << endl;
  340.     for (LINT i = 1; i < nitems; i++)
  341.       if ((cum_prob[i]-cum_prob[i-1]) * nitems > 10)
  342.         fp << i << "  " << (cum_prob[i]-cum_prob[i-1]) * nitems << " "
  343.           << tax->first_child(i) << " " << tax->last_child(i) << endl;
  344.   }
  345.   else {
  346.     if (cum_prob[0] * nitems > 5)
  347.       fp << 0 << "  " << cum_prob[0] * nitems << endl;
  348.     for (LINT i = 1; i < nitems; i++)
  349.       if ((cum_prob[i]-cum_prob[i-1]) * nitems > 5)
  350.         fp << i << "  " << (cum_prob[i]-cum_prob[i-1]) * nitems << endl;
  351.   }
  352.  
  353.   fp << setprecision(0);
  354.   fp << endl;
  355. }
  356.    
  357.  
  358. //--------------------------------- String ---------------------------------
  359.  
  360.  
  361. String::String(LINT n)  // number of items
  362.   : nitems(n)
  363. {
  364.   items = new LINT [nitems];
  365. //  rval = new FLOAT [nitems];
  366. //  ritems = new LINT [nitems];
  367. }
  368.  
  369.  
  370. String::~String(void)
  371. {
  372.   delete [] items;
  373. //  delete [] rval;
  374. //  delete [] ritems;
  375. }
  376.  
  377.  
  378. void String::display(ofstream &fp, LINT prob_comp)
  379. {
  380.   fp << setw(6) << prob_comp * prob << " " << setw(6) << conf << " ";
  381.   for(LINT i = 0; i < nitems; i++)
  382.     fp << " " << items[i];
  383.   fp << endl;
  384.   return;
  385. }
  386.  
  387. void String::display(ofstream &fp, StringSet &lits, LINT prob_comp)
  388. {
  389.   LINT i, j;
  390.   StringP lstr;
  391.  
  392.   fp << setw(6) << prob_comp * prob << " " << setw(6) << conf << " ";
  393.   for(i = 0; i < nitems; i++)
  394.     {
  395.       fp << "  << ";
  396.       lstr = lits.get_pat(items[i]);
  397.       for (j = 0; j < lstr->nitems; j++)
  398.         fp << lstr->items[j] << " ";
  399.       fp << ">>";
  400.     }
  401.   fp << endl;
  402.   return;
  403. }
  404.  
  405.  
  406. // shuffles items in string
  407. //
  408. // void String::shuffle(void)
  409. // {
  410. //   UniformDist ud;
  411. //   LINT i, j, ival;
  412. //   FLOAT fval;
  413.  
  414. //   // associate a random value with each item
  415. //   for (i = 0; i < nitems; i++)
  416. //     rval[i] = ud();
  417.  
  418. //   // sort items according to the values in rval
  419. //   for (i = 0; i < nitems; i++ )
  420. //     {
  421. //       ival = items[i]; fval = rval[i];
  422. //       for ( j = i; j > 0 && rval[j-1] > fval; j-- ) {
  423. //        items[j] = items[j-1];
  424. //        rval[j] = rval[j-1];
  425. //      }
  426. //       items[j] = ival;
  427. //       rval[j] = fval;
  428. //     }
  429. // }
  430.  
  431.  
  432. //------------------------------- StringSet -------------------------------
  433.  
  434.  
  435. StringSet::StringSet(LINT nitems,       // number of items
  436.                      PatternPar par,    // npats, patlen, corr, conf & conf_var
  437.                      Taxonomy *ptax,    // taxonomy (optional)
  438.                      FLOAT rept,        // repetition-level
  439.                      FLOAT rept_var     // variation in repetition-level
  440.                      )
  441.   : tax(ptax)
  442. {
  443.   NormalDist conf(par.conf, par.conf_var);
  444.   ExpDist freq;
  445.   ExpDist corr_lvl;
  446.   PoissonDist len(par.patlen-1);        // string length
  447.   NormalDist repeat(rept, rept_var);
  448.   UniformDist ud;
  449.  
  450.   items = new ItemSet(nitems, tax);     // associate probabilities with items
  451.  
  452.   LINT i, j, num_same;
  453.   FLOAT tot;
  454.  
  455.   npats = par.npats;
  456. //  last_pat = 0;
  457.   pat = new StringP [npats];
  458.   for (i = 0; i < npats; i++)
  459.     {
  460.       pat[i] = new String( 1+len() );
  461.  
  462.       // fill correlated items
  463.       if (par.corr > 0 && i > 0) {      // correlated patterns
  464.         // each pattern has some items same as the previous pattern
  465.         num_same = LINT( pat[i]->size() * par.corr * corr_lvl() + 0.5 );
  466.         if ( num_same > pat[i-1]->size() )
  467.           num_same = pat[i-1]->size();
  468.         if ( num_same > pat[i]->size() )
  469.           num_same = pat[i]->size();
  470.         // choose num_same items at random from previous pattern
  471.         Choose shuffle(pat[i-1]->size(), num_same);
  472.         for (j = 0; j < num_same; j++)
  473.           pat[i]->items[j] = pat[i-1]->item( shuffle.pos(j) );
  474. //      pat[i-1]->shuffle(num_same);
  475. //      for (j = 0; j < num_same; j++)
  476. //        pat[i]->items[j] = pat[i-1]->rand_item(j);
  477.       }
  478.       else {    // no correlation
  479.         num_same = 0;
  480.       }
  481.  
  482.       if (rept == 0) {
  483.         // fill remaining items at random
  484.         for (j = num_same; j < pat[i]->size(); j++)
  485.           pat[i]->items[j] = items->get_item();
  486. //      pat[i]->items[j] = LINT(1 + nitems * rand());
  487.       }
  488.       else {
  489.         // some items are repetitions
  490.         FLOAT rept_lvl = repeat();
  491.         for (j = num_same; j < pat[i]->size(); j++)
  492.           if ( j > 0 && ud() < rept_lvl )       // pick a previous item
  493.             pat[i]->items[j] = pat[i]->items[ LINT(j*ud()) ];
  494.           else  // pick random item
  495.             pat[i]->items[j] = items->get_item();
  496.       }
  497.       pat[i]->prob = freq(); // prob. that this pattern will be picked
  498.       pat[i]->conf = conf(); // used in Transaction::add and CustSeq::add
  499.                              // to decide how many items to drop from
  500.                              //  this pattern to corrupt it
  501.     }
  502.  
  503.   if (tax) {
  504.     // weight probabilites with geometric mean of probabilities of items
  505.     for (i = 0; i < npats; i++)
  506.       {
  507.         DOUBLE weight = 1;
  508.         for (j = 0; j < pat[i]->size(); j++)
  509.           weight *= items->weight(pat[i]->items[j]);
  510. //      cerr << "WEIGHT = " << weight;
  511.         weight = pow(weight, DOUBLE(1)/pat[i]->size());
  512. //      cerr << "  " << weight << endl;
  513.         pat[i]->prob *= weight;
  514.       }
  515.   }
  516.  
  517.   // normalize probabilites (why -- see get_pat)
  518.   cum_prob = new FLOAT [npats];
  519.   tot = 0;
  520.   for (i = 0; i < npats; i++)
  521.     tot += pat[i]->prob;
  522.   for (i = 0; i < npats; i++)
  523.     pat[i]->prob /= tot;
  524.  
  525.   // calulate cumulative probabilities
  526.   cum_prob[0] = pat[0]->prob;
  527.   for (i = 1; i < npats; i++)
  528.     cum_prob[i] = cum_prob[i-1] + pat[i]->prob;
  529. //  cerr << cum_prob[npats-1] << endl << flush;
  530.  
  531.   // allocate space for answer
  532.   LINT maxlen = 0;
  533.   for (i = 1; i < npats; i++)
  534.     if (pat[i]->size() > maxlen)
  535.       maxlen = pat[i]->size();
  536.   answer = new String(maxlen);
  537. }
  538.  
  539.  
  540. StringSet::~StringSet()
  541. {
  542.   LINT i;
  543.  
  544.   for (i = 0; i < npats; i++)
  545.     delete pat[i];
  546.   delete [] pat;
  547. }
  548.  
  549.  
  550. // specialize each item in pattern #i and store result in answer
  551. //
  552. StringP StringSet::specialize(LINT i)
  553. {
  554.   answer->set_size( pat[i]->size() );
  555.   answer->set_conf_lvl( pat[i]->conf_lvl() );
  556.   for (LINT j = 0; j < pat[i]->size(); j++)
  557.     answer->set_item(j, items->specialize( pat[i]->item(j) ));
  558.   return answer;
  559. }
  560.  
  561.  
  562. // returns pattern #i
  563. //
  564. StringP StringSet::get_pat(LINT i)
  565. {
  566.   if (!tax)
  567.     return pat[i];
  568.   else
  569.     return specialize(i);
  570. };
  571.  
  572.  
  573. void StringSet::display(ofstream &fp)
  574. {
  575.   LINT i;
  576.  
  577.   items->display(fp);
  578.  
  579.   fp << "ItemSets:" << endl;
  580.   fp << setprecision(3);
  581.   // too lazy to do a sort, so print high-prob. patterns first
  582.   for (i = 0; i < npats; i++)
  583.     if (pat[i]->prob * npats > 10)
  584.       pat[i]->display(fp, npats);
  585.   for (i = 0; i < npats; i++)
  586.     if (pat[i]->prob * npats <= 10 && pat[i]->prob * npats > 1)
  587.       pat[i]->display(fp, npats);
  588.   fp << setprecision(0);
  589.   fp << endl;
  590. }
  591.  
  592.  
  593. void StringSet::display(ofstream &fp, StringSet &lits)
  594. {
  595.   LINT i;
  596.  
  597.   fp << setprecision(3);
  598.   // too lazy to do a sort, so print high-prob. patterns first
  599.   for (i = 0; i < npats; i++)
  600.     if (pat[i]->prob * npats > 6)
  601.       pat[i]->display(fp, lits, npats);
  602.   for (i = 0; i < npats; i++)
  603.     if (pat[i]->prob * npats <= 6)
  604.       pat[i]->display(fp, lits, npats);
  605.   fp << setprecision(0);
  606. }
  607.  
  608.  
  609. //------------------------------- StringSet -------------------------------
  610.  
  611.  
  612. // returns a pattern chosen at random
  613. //
  614. StringP StringSetIter::get_pat(void)
  615. {
  616.   FLOAT r;
  617.   LINT i = 0;
  618.  
  619.   if (last_pat < 0) {
  620.     last_pat = -last_pat;
  621.     if (!strset->tax)
  622.       return strset->pat[last_pat];
  623.     else
  624.       return strset->specialize(last_pat);
  625.   }
  626.  
  627.   // find the desired pattern using cum_prob table
  628.   r = rand();
  629.   i = r * strset->npats;
  630.   if (i == strset->npats)
  631.     i--;
  632.   while ( i < (strset->npats-1) && r > strset->cum_prob[i] )
  633.     i++;
  634.   while ( i > 0 && r < strset->cum_prob[i-1] )
  635.     i--;
  636.   last_pat = i;
  637.  
  638.   if (!strset->tax)
  639.     return strset->pat[i];
  640.   else
  641.     return strset->specialize(i);
  642. };
  643.  
  644.  
  645. void StringSetIter::unget_pat(void)
  646. {
  647.   last_pat = -last_pat;
  648. }
  649.    
  650.  
  651. //------------------------------ Transaction ------------------------------
  652.  
  653.  
  654. // static variables
  655. const LINT Transaction::cid_len = 10;
  656. const LINT Transaction::tid_len = 10;
  657. const LINT Transaction::item_len = 10;
  658.  
  659. LINT Transaction::tid = 0;
  660.  
  661. Transaction::Transaction(LINT sz)
  662.   : tlen(sz), nitems(0), maxsize(5 * sz)
  663. {
  664.   // maximum size of a transaction is 5 * sz
  665.   items = new LINT [maxsize];
  666. }
  667.  
  668.  
  669. Transaction::~Transaction()
  670. {
  671.   delete [] items;
  672. }
  673.  
  674. void Transaction::sort(void)
  675. {
  676.   LINT val;
  677.   LINT i, j;
  678.  
  679.   for (i = 1; i < nitems; i++ )
  680.     {
  681.       val = items[i];
  682.       for ( j = i; j > 0 && items[j-1] > val; j-- )
  683.         items[j] = items[j-1];
  684.       items[j] = val;
  685.     }
  686. }
  687.  
  688.  
  689. BOOLEAN Transaction::add_item(LINT itm)
  690. {
  691.   LINT i;
  692.  
  693.   for (i = 0; i < nitems; i++)
  694.     if ( items[i] == itm ) return FALSE;
  695.  
  696.   if (nitems >= maxsize) {      // allocate more memory
  697.     LINT *old_items = items;
  698.     maxsize *= 2;
  699.     items = new LINT [maxsize];
  700.     for (i = 0; i < nitems; i++)
  701.       items[i] = old_items[i];
  702.     delete [] old_items;
  703.   }
  704.  
  705.   items[nitems++] = itm;
  706.   return TRUE;
  707. }
  708.  
  709.  
  710. // adds pattern to transaction
  711. // returns TRUE if pattern was added, FALSE else
  712. //
  713. BOOLEAN Transaction::add(String &pat, BOOLEAN corrupt)
  714. {
  715.   static UniformDist ud;
  716.   LINT i, patlen;
  717.  
  718.   // corrupt the pattern by reducing its length;
  719.   // conf_lvl for a pattern is decided at the time of pattern creation
  720.   patlen = pat.size();
  721.   if ( corrupt )
  722.     while ( patlen > 0 && ud() > pat.conf_lvl() )
  723.       patlen--;
  724.  
  725.   // in half of the cases, we drop the pattern that won't fit
  726.   if ( patlen+nitems > tlen )   // not enough space left
  727.     if ( ud() > 0.5 )
  728.       return FALSE;
  729.  
  730.   // pick "patlen" items at random from pattern
  731. //  if ( patlen < pat.size() )
  732.   Choose shuffle(pat.size(), patlen);
  733.   for (i = 0; i < patlen; i++)
  734.     add_item( pat.item(shuffle.pos(i)) ); // allocates extra space if necessary
  735. //    pat.shuffle(patlen);
  736. //  for (i = 0; i < patlen; i++)
  737. //    add_item( pat.rand_item(i) ); // allocates extra space if necessary
  738.  
  739.   return TRUE;
  740. }
  741.  
  742.  
  743. void Transaction::write(ofstream &fp, LINT cid)
  744. {
  745.   if ( nitems == 0 )
  746.     return;
  747.   sort();
  748.  
  749.   tid++;
  750.   if (cid == 0)         // no customer-id; set cust-id to trans-id
  751.     cid = tid;
  752.  
  753.   fp.write((char *)&cid, sizeof(LINT));
  754.   fp.write((char *)&tid, sizeof(LINT));
  755.   fp.write((char *)&nitems, sizeof(LINT));
  756.   fp.write((char *)items, nitems * sizeof(LINT));
  757. }
  758.  
  759. void Transaction::write_asc(ofstream &fp, LINT cid)
  760. {
  761.   if ( nitems == 0 )
  762.     return;
  763.   sort();
  764.  
  765.   tid++;
  766.   if (cid == 0)         // no customer-id; set cust-id to trans-id
  767.     cid = tid;
  768.  
  769.   for (LINT i = 0; i < nitems; i++) {
  770.     fp << setw(cid_len) << cid << " ";
  771.     fp << setw(tid_len) << tid << " ";
  772.     fp << setw(item_len) << items[i] << endl;
  773.   }
  774. }
  775.  
  776.  
  777. //------------------------------ CustSeq ------------------------------
  778.  
  779.  
  780. CustSeq::CustSeq(Cid cust_id, LINT seq_len, LINT trans_len)
  781.   : cid(cust_id), slen(seq_len), tlen(trans_len), nitems(0),
  782.     ntrans(seq_len), maxsize(5 * seq_len)
  783. {
  784.   // we reallocate memory if necessary
  785.   trans = new TransactionP [maxsize];
  786.   for (LINT i = 0; i < maxsize; i++)
  787.     trans[i] = NULL;
  788. }
  789.  
  790.  
  791. CustSeq::~CustSeq()
  792. {
  793.   for (LINT i = 0; i < maxsize; i++)
  794.     if ( trans[i] )
  795.       delete trans[i];
  796.   delete [] trans;
  797. }
  798.  
  799.  
  800. // adds pattern to CustSeq
  801. // returns TRUE if pattern was added, FALSE else
  802. // REWORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  803. //
  804. BOOLEAN CustSeq::add(String &pat, StringSet &lits)
  805. {
  806.   static UniformDist ud;
  807.   LINT i, patlen;
  808.   LINT pos;
  809.   LINT newitems, olditems;
  810.   BOOLEAN corrupt;      // if TRUE, corrupt transactions too
  811.  
  812.   if ( ud() > pat.conf_lvl() )
  813.     corrupt = TRUE;             // corrupt transactions
  814.   else
  815.     corrupt = FALSE;            // don't corrupt transactions
  816.  
  817.   // corrupt the pattern by reducing its length;
  818.   // conf_lvl for a pattern is decided at the time of pattern creation
  819.   patlen = pat.size();
  820.   if ( corrupt )
  821.     while ( patlen > 0 && ud() > pat.conf_lvl() )
  822.       patlen--;
  823.   if ( patlen == 0 )    // no trans. left in sequence
  824.     return TRUE;
  825.  
  826.   // allows transactions to be dropped randomly from the sequence
  827. //  if ( patlen < pat.size() )
  828.   Choose shuffle(pat.size(), patlen);
  829. //    pat.shuffle(patlen);
  830.  
  831.   // calculate # of items in pattern
  832.   for (newitems = 0, i = 0; i < patlen; i++)
  833.     newitems += lits.get_pat( pat.item( shuffle.pos(i) ) )->size();
  834. //    newitems += lits.get_pat( pat.rand_item(i) )->size();
  835.  
  836.   // in half of the cases, we drop the pattern that won't fit
  837.   if ( (patlen > slen) || (newitems + nitems > slen * tlen) )
  838.     if ( ud() > 0.5 )
  839.       return FALSE;
  840.  
  841.   if ( patlen > maxsize ) {     // need to allocate more memory
  842.     TransactionP *old_trans = trans;
  843.     LINT oldsize = maxsize;
  844.     maxsize = patlen*2;
  845.     trans = new TransactionP [maxsize];
  846.     for (i = 0; i < oldsize; i++)
  847.       trans[i] = old_trans[i];
  848.     for (; i < maxsize; i++)
  849.       trans[i] = NULL;
  850.     delete [] old_trans;
  851.   }
  852.  
  853.   // add new sequence
  854.   Choose *shuffle1 = NULL;
  855.   if (ntrans > patlen)
  856.     shuffle1 = new Choose(ntrans, patlen);
  857.   for (i = 0; i < patlen; i++)
  858.     {
  859.       if ( shuffle1 )
  860.         pos = shuffle1->pos(i);
  861.       else
  862.         pos = i;
  863.       if ( trans[pos] == NULL )
  864.         trans[pos] = new Transaction(tlen);
  865.       olditems = trans[pos]->size();
  866.       trans[pos]->add( *lits.get_pat(pat.item( shuffle.pos(i) )), corrupt );
  867. //      trans[pos]->add( *lits.get_pat(pat.rand_item(i)), corrupt );
  868.       nitems += trans[pos]->size() - olditems;  // update count of #items
  869.     }
  870.   delete shuffle1;
  871.  
  872. //   pos = ud() * ntrans / patlen;
  873. //   for (i = 0; i < patlen; i++)
  874. //     {
  875. //       if ( trans[pos] == NULL )
  876. //      trans[pos] = new Transaction(tlen);
  877. //       olditems = trans[pos]->size();
  878. //       trans[pos]->add( *lits.get_pat(pat.item( shuffle.pos(i) )), corrupt );
  879. // //      trans[pos]->add( *lits.get_pat(pat.rand_item(i)), corrupt );
  880. //       nitems += trans[pos]->size() - olditems;  // update count of #items
  881. //       pos += 1 + ud() * ntrans / patlen;
  882. //     }
  883.  
  884.   return TRUE;
  885. }
  886.  
  887.  
  888. void CustSeq::write(ofstream &fp)
  889. {
  890.   for (LINT i = 0; i < ntrans-1; i++)
  891.     if ( trans[i]->size() > 0 )
  892.       trans[i]->write(fp, cid);
  893. }
  894.  
  895.  
  896. void CustSeq::write_asc(ofstream &fp)
  897. {
  898.   for (LINT i = 0; i < ntrans-1; i++)
  899.     if ( trans[i]->size() > 0 )
  900.       trans[i]->write_asc(fp, cid);
  901. }



PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 5475
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 21:26
Giovedì, 18/03/2010
In gen.h gli include dovrebbero essere

#include <iostream>
#include <fstream>

e manca una

using namespace std;

prima della dichiarazione delle classi ...

Ma dove hai trovato quel codice? Non è scritto correttamente per il C++ ... gli include dovrebbero essere

<cassert>
<string>
<cmath>

e così via ... sembra codice "vecchio stile" ...


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
PM Quote
Avatar
paky88fg (Ex-Member)
Rookie


Messaggi: 38
Iscritto: 07/04/2009

Segnala al moderatore
Postato alle 21:40
Giovedì, 18/03/2010
E' un tool dell'IBM per generare Database


PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 5475
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 21:45
Giovedì, 18/03/2010
Sarà ... ma è come ti ho detto ... è un vecchio codice ...



Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
PM Quote
Avatar
paky88fg (Ex-Member)
Rookie


Messaggi: 38
Iscritto: 07/04/2009

Segnala al moderatore
Postato alle 21:59
Giovedì, 18/03/2010
Ti credo, ma mi serve.


PM Quote
Avatar
paky88fg (Ex-Member)
Rookie


Messaggi: 38
Iscritto: 07/04/2009

Segnala al moderatore
Postato alle 21:59
Giovedì, 18/03/2010
Ti credo, ma mi serve.


PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo