void SOM::operation(std::vector< std::vector<double> >& inputData, std::vector<int>& target)
{
    int xWinner, yWinner;
    int hits, errors;

    hits = 0;
    errors = 0;

    for (unsigned int example = 0; example < inputData.size(); example++)
    {
        shortestDistance(inputData[example]);

        xWinner = winnerNeuron / dimension;
        yWinner = winnerNeuron % dimension;

        if(neurons[xWinner][yWinner].label == target[example])
            hits++;
        else
            errors++;

        cout << "Test " << example << ": "
             << "(Actual --> " << neurons[xWinner][yWinner].label << ") | "
             << "(Expected --> " << target[example] << ")" << endl;
    }
    cout << endl << "Hits: " << hits << endl;
    cout << "Errors: " << errors << endl;
}
Esempio n. 2
0
int main () {
    
    std::cout << "Graph example program "
              << " - find shortest path in a directed graph." 
              << std::endl;

    static const char pendleton[]  = "Pendleton";
    static const char pensacola[]  = "Pensacola";
    static const char peoria[]     = "Peoria";
    static const char phoenix[]    = "Phoenix";
    static const char pierre[]     = "Pierre";
    static const char pittsburgh[] = "Pittsburgh";
    static const char princeton[]  = "Princeton";
    static const char pueblo[]     = "Pueblo";

    Cities cityMap;

    cityMap[pendleton][phoenix]    = 4;
    cityMap[pendleton][pueblo]     = 8;
    cityMap[pensacola][phoenix]    = 5;
    cityMap[peoria][pittsburgh]    = 5;
    cityMap[peoria][pueblo]        = 3;
    cityMap[phoenix][peoria]       = 4;
    cityMap[phoenix][pittsburgh]   = 10;
    cityMap[phoenix][pueblo]       = 3;
    cityMap[pierre][pendleton]     = 2;
    cityMap[pittsburgh][pensacola] = 4;
    cityMap[princeton][pittsburgh] = 2;
    cityMap[pueblo][pierre]        = 3;
    
    Distances dist;
    
    shortestDistance (cityMap, pierre, dist);
    Distances::iterator where;

    std::cout << "Find the shortest path from : " 
              << pierre << '\n';

    for (where = dist.begin (); where != dist.end (); ++where)
        std::cout << "  Distance to: " << (*where).first << ":"
                  <<  (*where).second << '\n';
        
    std::cout << "End of graph example program" << '\n';

    return 0;
}
void SOM::training(std::vector< std::vector<double> >& inputData)
{
    for (int epoch = 0; epoch < numEpochs; epoch++)
    {
        for (unsigned int example = 0; example < inputData.size(); example++)
        {
            shortestDistance(inputData[example]);
            int index = ( epoch * inputData.size() ) + example;
            adjustWeights(index, inputData[example]);
        }

        lastSeason++;

        if(distance < acceptableError)
            break;
    }
}
Esempio n. 4
0
int sdistance(char* Maze, int lrud, int dest)
{
    Maze[2650] = '$';
    char store = Maze[dest];
    Maze[dest] = 'R';
	int i;
	int visit[52*52];
	for(i=0;i<52*52;i++)
	{
		visit[i]=0;	
	}

	Queue* p;
	p=queue_new();
	p=queue_push(p,lrud);
	p=queue_push(p,-1);              // '#' to -5 changed		
	
	int dis=shortestDistance(Maze,visit,p,0,52);
	Maze[dest] = store;
	Maze[2650] = 'R';
	return dis;
}
void SOM::clustering(std::vector< std::vector<double> >& inputData, std::vector<int>& target)
{
    int xWinner, yWinner;

    for (unsigned int example = 0; example < inputData.size(); example++)
    {
        shortestDistance(inputData[example]);

        xWinner = winnerNeuron / dimension;
        yWinner = winnerNeuron % dimension;

        neurons[xWinner][yWinner].label = target[example];
    }

    for (unsigned int x = 0; x < neurons.size(); x++)
    {
        for (unsigned int y = 0; y < neurons[x].size(); y++)
        {
            cout << neurons[x][y].label << " ";
        }
        cout << endl;
    }
    cout << endl;
}
Esempio n. 6
0
void HClustering<Pt>::cluster(Pt *data, unsigned nElm)
{
    unsigned i, j, k, n;
    typedef std::pair<unsigned,float> shortestDistanceRow;
    std::vector<shortestDistanceRow> 
	shortestDistance(nElm, shortestDistanceRow(nElm, INF));

    // Initially there are nElm clusters, so have these many
    // TreeNodes and link them to the input data.
    clusters.resize(nElm);
    for (i = 0; i < nElm; i++) {
	clusters[i].id = i;
	clusters[i].rep = i;
    }

    cacheDistances(data, nElm);

    // compute the shortest distance array.
    for (i = 0; i < nElm; i++) {
	for (j = 0; j < nElm; j++) {
	    float linkageIJ;
	    // No need to check for cluster rep here since
	    // at this point everybody is his own rep.
	    assert(IS_REP(i));
	    if (i == j)
		continue;
	    linkageIJ = computeLinkage(i, j);
	    if (shortestDistance[i].second == INF ||
		linkageIJ < shortestDistance[i].second)
	    {
		shortestDistance[i].first = j;
		shortestDistance[i].second = linkageIJ;
	    }
	}
    }

    // Run the clustering loop till there are only numClusters remaining.
    for (n = nElm; n > numClusters; n--) {
	float shortestDistanceSeen = INF;
	unsigned shortestDistanceFrom = nElm;

	// TODO: Keep a list of elements that represent a cluster.
	// TODO: Maybe some kind of sorted list? I don't know
	for (i = 0; i < nElm; i++) {
	    // If this element is not the rep for the cluster it is in:
	    if (!IS_REP(i))
		continue;
	    if (shortestDistanceSeen == INF ||
		shortestDistanceSeen > shortestDistance[i].second)
	    {
		shortestDistanceSeen = shortestDistance[i].second;
		shortestDistanceFrom = i;
	    }
	}

	assert (shortestDistanceSeen != INF && shortestDistanceFrom < nElm);
	i = shortestDistanceFrom;
	j = shortestDistance[i].first;
	assert(i != j);
	// i and j must both represent their clusters already
	// (since we only look at reps during during each iteration).
	assert(IS_REP(i) && IS_REP(j));

	// If the shortest distance was from i to j, then j to i
	// must also be the shortest distance.
	assert(shortestDistance[j].second == shortestDistanceSeen);

	if (clusters[i].members.size() < clusters[j].members.size()) {
	    // let 'j' represent the smaller cluster.
	    unsigned temp = i;
	    i = j;
	    j = temp;
	}
	// We now need to combine the clusters i and j,
	// So make 'j' belong to 'i' as it is smaller.
	clusters[i].members.push_back(j);
	clusters[i].members.insert(clusters[i].members.end(), 
				   clusters[j].members.begin(),
				   clusters[j].members.end());
	clusters[j].rep = i;
	{
	    // Just to free up memory used for the members array.
	    std::vector<unsigned> tmp;
	    clusters[j].members.swap(tmp);
	}
	// The shortest distance from i needs recomputation.
	shortestDistance[i].first = nElm;
	shortestDistance[i].second = INF;
	
	// compute linkage between clusters (w.r.t i).
	for (k = 0; k < nElm; k++) {
	    float distance;

	    // ignore non rep data. also ignore i itself.
	    if (!IS_REP(k) || k == i)
		continue;
	    distance = computeLinkage(i, k);
	    assert(distance != INF);
	    // Now update the shortestDistance array.
	    if ((shortestDistance[i].second == INF ||
		 shortestDistance[i].second > distance))
	    {
		shortestDistance[i].second = distance;
		shortestDistance[i].first = k;
	    }
	    // for 'k', the shortestDistance array may be
	    // pointing to a non-rep now, or the distance
	    // to cluster 'i' may change (as cluster 'i' has
	    // more members now - not always LINK_MINIMUM).
	    // TODO: Maybe this can be done on-demand, i.e.
	    // when we see (i, j) distance with j being non-rep.
	    if (!IS_REP(shortestDistance[k].first) ||
		shortestDistance[k].first == i) 
	    {
		assert(clusters[shortestDistance[k].first].rep ==
		       clusters[i].id);
		if (linkageType == LINK_MINIMUM) {
		    // In case of LINK_MINIMUM the nearest
		    // cluster would now be 'i' (since 'j'
		    // now belongs to 'i').
		    assert(shortestDistance[k].second == distance);
		    shortestDistance[k].first = i;
		} else {
		    // computeLinkage(i, k) cannot be smaller than
		    // the known shortest distance for node k
		    assert(distance >= shortestDistance[k].second);
		    // reset shortestDistance for 'k' since its now invalid.
		    shortestDistance[k].second = INF;
		    shortestDistance[k].first = nElm;
		    // Will now need to look through ALL
		    // clusters for a new nearest linkage.
		    for (j = 0; j < nElm; j++) {
			float linkageKJ;
			if (j == k || !IS_REP(j))
			    continue;
			linkageKJ = computeLinkage(k, j);
			if (shortestDistance[k].second == INF ||
			    linkageKJ < shortestDistance[k].second)
			{
				shortestDistance[k].first = j;
				shortestDistance[k].second = linkageKJ;
			}
		    }
		}
		// At this point, the shortest distance from k must be to a rep.
		assert(IS_REP(shortestDistance[k].first));
	    }
	}
    }
}