⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transaction.cpp

📁 关联规则挖掘数据产生程序.VISUAL C++ 可产生满足要求的挖掘数据.
💻 CPP
字号:
// Transaction.cpp: implementation of the Transaction class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Transaction.h"

#include "Choose.h"

#include <iomanip.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//------------------------------ Transaction ------------------------------


// static variables
const int Transaction::cid_len = 10;
const int Transaction::tid_len = 10;
const int Transaction::item_len = 10;

int Transaction::tid = 0;

Transaction::Transaction(int sz)
  : tlen(sz), nitems(0), maxsize(5 * sz)
{
  // maximum size of a transaction is 5 * sz
  items = new int [maxsize];
}


Transaction::~Transaction()
{
  delete [] items;
}

void Transaction::sort(void)
{
  int val;
  int i, j;

  for (i = 1; i < nitems; i++ )
    {
      val = items[i];
      for ( j = i; j > 0 && items[j-1] > val; j-- )
	items[j] = items[j-1];
      items[j] = val;
    }
}


bool Transaction::add_item(int itm)
{ 
  int i;

  for (i = 0; i < nitems; i++)
    if ( items[i] == itm ) return false;

  if (nitems >= maxsize) {	// allocate more memory
    int *old_items = items;
    maxsize *= 2;
    items = new int [maxsize];
    for (i = 0; i < nitems; i++)
      items[i] = old_items[i];
    delete [] old_items;
  }

  items[nitems++] = itm;
  return true;
}


// adds pattern to transaction
// returns TRUE if pattern was added, FALSE else
//
bool Transaction::add(ARString &pat, bool corrupt)
{
  static UniformDist ud;
  int i, patlen;

  // corrupt the pattern by reducing its length;
  // conf_lvl for a pattern is decided at the time of pattern creation
  patlen = pat.size();
  if ( corrupt )
    while ( patlen > 0 && ud() > pat.conf_lvl() )
      patlen--;
  
  // in half of the cases, we drop the pattern that won't fit
  if ( patlen+nitems > tlen )	// not enough space left
    if ( ud() > 0.5 )
      return false;
  
  // pick "patlen" items at random from pattern
//  if ( patlen < pat.size() )
  CChoose shuffle(pat.size(), patlen);
  for (i = 0; i < patlen; i++)
    add_item( pat.item(shuffle.pos(i)) ); // allocates extra space if necessary
//    pat.shuffle(patlen);
//  for (i = 0; i < patlen; i++)
//    add_item( pat.rand_item(i) ); // allocates extra space if necessary
  
  return true;
}


void Transaction::write(ofstream &fp, int cid)
{
  if ( nitems == 0 )
    return;
  sort();

  tid++;
  if (cid == 0)		// no customer-id; set cust-id to trans-id
    cid = tid;

  fp.write((char *)&cid, sizeof(int));
  fp.write((char *)&tid, sizeof(int));
  fp.write((char *)&nitems, sizeof(int));
  fp.write((char *)items, nitems * sizeof(int));
}

void Transaction::write_asc(ofstream &fp, int cid)
{
  if ( nitems == 0 )
    return;
  sort();

  tid++;
  if (cid == 0)		// no customer-id; set cust-id to trans-id
    cid = tid;

  for (int i = 0; i < nitems; i++) {
    fp << setw(cid_len) << cid << " ";
    fp << setw(tid_len) << tid << " ";
    fp << setw(item_len) << items[i] << endl;
  }
}


//------------------------------ CustSeq ------------------------------


CustSeq::CustSeq(int cust_id, int seq_len, int trans_len)
  : cid(cust_id), slen(seq_len), tlen(trans_len), nitems(0),
    ntrans(seq_len), maxsize(5 * seq_len)
{
  // we reallocate memory if necessary
  trans = new TransactionP [maxsize];
  for (int i = 0; i < maxsize; i++)
    trans[i] = NULL;
}


CustSeq::~CustSeq()
{
  for (int i = 0; i < maxsize; i++)
    if ( trans[i] )
      delete trans[i];
  delete [] trans;
}


// adds pattern to CustSeq
// returns TRUE if pattern was added, FALSE else
// REWORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
bool CustSeq::add(ARString &pat, StringSet &lits)
{
  static UniformDist ud;
  int i, patlen;
  int pos;
  int newitems, olditems;
  bool corrupt;	// if TRUE, corrupt transactions too

  if ( ud() > pat.conf_lvl() )
    corrupt = true;		// corrupt transactions
  else
    corrupt = false;		// don't corrupt transactions

  // corrupt the pattern by reducing its length;
  // conf_lvl for a pattern is decided at the time of pattern creation
  patlen = pat.size();
  if ( corrupt )
    while ( patlen > 0 && ud() > pat.conf_lvl() )
      patlen--;
  if ( patlen == 0 )	// no trans. left in sequence
    return true;

  // allows transactions to be dropped randomly from the sequence
//  if ( patlen < pat.size() )
  CChoose shuffle(pat.size(), patlen);
//    pat.shuffle(patlen);

  // calculate # of items in pattern
  for (newitems = 0, i = 0; i < patlen; i++)
    newitems += lits.get_pat( pat.item( shuffle.pos(i) ) )->size();
//    newitems += lits.get_pat( pat.rand_item(i) )->size();

  // in half of the cases, we drop the pattern that won't fit
  if ( (patlen > slen) || (newitems + nitems > slen * tlen) )
    if ( ud() > 0.5 )
      return false;

  if ( patlen > maxsize ) {	// need to allocate more memory
    TransactionP *old_trans = trans;
    int oldsize = maxsize;
    maxsize = patlen*2;
    trans = new TransactionP [maxsize];
    for (i = 0; i < oldsize; i++)
      trans[i] = old_trans[i];
    for (; i < maxsize; i++)
      trans[i] = NULL;
    delete [] old_trans;
  }

  // add new sequence
  CChoose *shuffle1 = NULL;
  if (ntrans > patlen)
    shuffle1 = new CChoose(ntrans, patlen);
  for (i = 0; i < patlen; i++)
    {
      if ( shuffle1 )
	pos = shuffle1->pos(i);
      else
	pos = i;
      if ( trans[pos] == NULL )
	trans[pos] = new Transaction(tlen);
      olditems = trans[pos]->size();
      trans[pos]->add( *lits.get_pat(pat.item( shuffle.pos(i) )), corrupt ); 
//      trans[pos]->add( *lits.get_pat(pat.rand_item(i)), corrupt ); 
      nitems += trans[pos]->size() - olditems;  // update count of #items
    }
  delete shuffle1;

//   pos = ud() * ntrans / patlen;
//   for (i = 0; i < patlen; i++)
//     {
//       if ( trans[pos] == NULL )
// 	trans[pos] = new Transaction(tlen);
//       olditems = trans[pos]->size();
//       trans[pos]->add( *lits.get_pat(pat.item( shuffle.pos(i) )), corrupt ); 
// //      trans[pos]->add( *lits.get_pat(pat.rand_item(i)), corrupt ); 
//       nitems += trans[pos]->size() - olditems;  // update count of #items
//       pos += 1 + ud() * ntrans / patlen;
//     }

  return true;
}


void CustSeq::write(ofstream &fp)
{
  for (int i = 0; i < ntrans-1; i++)
    if ( trans[i]->size() > 0 )
      trans[i]->write(fp, cid);
}


void CustSeq::write_asc(ofstream &fp)
{
  for (int i = 0; i < ntrans-1; i++)
    if ( trans[i]->size() > 0 )
      trans[i]->write_asc(fp, cid);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -