Beispiel #1
0
autoFeatureWeights FeatureWeights_computeRELIEF
(
    ///////////////////////////////
    // Parameters                //
    ///////////////////////////////

    PatternList pp,         // source pattern
                        //
    Categories c,       // source categories
                        //
    long k              // k(!)
                        //
)

{
	autoPatternList p = Data_copy (pp);
	autoFeatureWeights me = FeatureWeights_create (p -> nx);

	/////////////////////////////////
	// Initial weights <- 0        //
	/////////////////////////////////

	for (long i = 1; i <= p->nx; i++) {
		my fweights -> data [1] [i] = 0.0;
	}

	/////////////////////////////////
	// Normalization               //
	/////////////////////////////////

	autoNUMvector <double> min (0L, p->nx - 1);
	autoNUMvector <double> max (0L, p->nx - 1);
	for (long x = 1; x <= p -> nx; x ++) {
		max [x] = p -> z [1] [x];   // BUG: this will just crash because of array index out of bounds
		min [x] = max [x];
	}

	for (long y = 1; y <= p -> ny; y ++) {
		for (long x = 1; x <= p->nx; x++) {
			if (p->z[y][x] > max[x]) max[x] = p->z[y][x];
			if (p->z[y][x] < min[x]) min[x] = p->z[y][x];
		}
	}

	autoNUMvector <double> alfa (0L, p -> nx - 1);
	for (long x = 1; x <= p -> nx; x ++) {
		alfa [x] = max [x] - min [x];   // BUG: this will just crash because of array index out of bounds
	}

	for (long y = 1; y <= p->ny; y++) {
		for (long x = 1; x <= p->nx; x++) {
			if (alfa [x] != 0.0) {
				p->z[y][x] = (p->z[y][x] - min[x]) / alfa[x];
			} else {
				p->z[y][x] = 0.0;
			}
		}
	}

	/////////////////////////////////
	// Computing prior class probs //
	/////////////////////////////////

	autoNUMvector <double> priors (0L, c->size - 1);   // worst-case allocations
	autoNUMvector <long> classes (0L, c->size - 1);//
	autoNUMvector <long> enemies (0L, c->size - 1);//
	autoNUMvector <long> friends (0L, c->size - 1);//
	long nclasses = FeatureWeights_computePriors (c, classes.peek(), priors.peek());
	Melder_assert (nclasses >= 2);

	/////////////////////////////////
	// Updating the w.vector       //
	/////////////////////////////////

	for (long y = 1; y <= p -> ny; y ++) {

		long nfriends = KNN_kFriends (p.get(), p.get(), c, y, k, friends.peek());
		long nenemies = KNN_kUniqueEnemies (p.get(), p.get(), c, y, nclasses - 1, enemies.peek());

		if (nfriends && nenemies) {
			autoNUMvector <double> classps (0L, nenemies - 1);
			for (long eq = 0; eq < nenemies; eq ++) {
				for (long iq = 0; iq < nclasses; iq ++) {
					if (FeatureWeights_areFriends (c->at [enemies [eq]], c->at [classes [iq]])) {
						classps [eq] = priors [iq];
						break;
					}
				}
			}
			for (long x = 1; x <= p -> nx; x ++) {
				double p1 = 0.0;
				double p2 = 0.0;
				for (long ec = 0; ec < nfriends; ec ++) {
					p1 += fabs (p -> z [y] [x] - p -> z [friends [ec]] [x]) / (p -> ny * nfriends);
				}
				for (long ec = 0; ec < nenemies; ec++) {
					p2 += (fabs (p->z[y][x] - p->z[enemies[ec]][x]) * classps[ec]) / p->ny;
				}
				my fweights -> data [1] [x] = my fweights -> data [1] [x] - p1 + p2;
			}
		}
	}
	return me;
}
void cGuild::load( const QSqlQuery& result )
{
	serial_ = result.value( 0 ).toInt();
	name_ = result.value( 1 ).toString();
	abbreviation_ = result.value( 2 ).toString();
	charta_ = result.value( 3 ).toString();
	website_ = result.value( 4 ).toString();
	alignment_ = ( eAlignment ) result.value( 5 ).toInt();
	leader_ = dynamic_cast<P_PLAYER>( World::instance()->findChar( result.value( 6 ).toInt() ) );
	founded_.setTime_t( result.value( 7 ).toInt() );
	guildstone_ = World::instance()->findItem( result.value( 8 ).toInt() );

	// Load members and canidates
	QSqlQuery members( QString( "SELECT player,showsign,guildtitle,joined FROM guilds_members WHERE guild = %1" ).arg( serial_ ) );

	while ( members.next() )
	{
		P_PLAYER player = dynamic_cast<P_PLAYER>( World::instance()->findChar( members.value( 0 ).toInt() ) );

		if ( player )
		{
			player->setGuild( this );
			members_.append( player );

			MemberInfo* info = new MemberInfo;
			info->setShowSign( members.value( 1 ).toInt() != 0 );
			info->setGuildTitle( members.value( 2 ).toString() );
			info->setJoined( members.value( 3 ).toInt() );
			memberinfo_.insert( player, info );
		}
	}

	QSqlQuery canidates( QString( "SELECT player FROM guilds_canidates WHERE guild = %1" ).arg( serial_ ) );

	while ( canidates.next() )
	{
		P_PLAYER player = dynamic_cast<P_PLAYER>( World::instance()->findChar( canidates.value( 0 ).toInt() ) );

		if ( player )
		{
			player->setGuild( this );
			canidates_.append( player );
		}
	}

	// Clear the leader if he's not a member of the guild
	if ( !members_.contains( leader_ ) )
	{
		leader_ = 0;
	}

	Guilds::instance()->registerGuild( this );

	QSqlQuery allies( QString( "SELECT ally FROM guilds_allies WHERE guild = %1" ).arg( serial_ ) );

	while ( allies.next() )
	{
		allies_.append( allies.value( 0 ).toInt() );
	}

	QSqlQuery enemies( QString( "SELECT enemy FROM guilds_enemies WHERE guild = %1" ).arg( serial_ ) );

	while ( enemies.next() )
	{
		enemies_.append( enemies.value( 0 ).toInt() );
	}
}