In the preceding demos, all edges are considered equivalent. In this demo, we consider an attribute of an edge, termed the 'Edge Weight'. An Edge Weight is a value that is associated with an edge. Weighted graphs are useful in a wide array of domains, and analysis routines that calculate network structure using edge weights are also common.

Repast HPC's native implementation of an edge includes a 'weight' attribute; all that is needed is to make use of it.

For our demo, we will simply set the edge weight when the edge is connected. This can be done by simply adding the weight as an additional argument to the addEdge method call in the RepastHPCDemoModel::connectAgentNetwork method:

void RepastHPCDemoModel::connectAgentNetwork(){
	repast::SharedContext<RepastHPCDemoAgent>::const_local_iterator iter    = context.localBegin();
	repast::SharedContext<RepastHPCDemoAgent>::const_local_iterator iterEnd = context.localEnd();
	while(iter != iterEnd) {
		RepastHPCDemoAgent* ego = &**iter;
		std::vector<RepastHPCDemoAgent*> agents;
		agents.push_back(ego);                          // Omit self
		context.selectAgents(5, agents, true);          // Choose 5 other agents randomly
		// Make an undirected connection
		for(size_t i = 0; i < agents.size(); i++){
         	    std::cout << "CONNECTING: " << ego->getId() << " to " << agents[i]->getId() << std::endl;
  	  	    agentNetwork->addEdge(ego, agents[i], i + 1);	
		}
		iter++;
	}	
}

In this case we are simply setting the edge weight to whatever the value of 'i' happens to be plus one (to avoid zero), which means we will get one edge with a weight of 1, one with a weight of 2, etc.

To make use of this value, we need only retrieve the edge from the network and call the weight() method of it, which we will do in our play() method:

void RepastHPCDemoAgent::play(repast::SharedNetwork<RepastHPCDemoAgent,
                              repast::RepastEdge<RepastHPCDemoAgent>,
                              repast::RepastEdgeContent<RepastHPCDemoAgent>,
                              repast::RepastEdgeContentManager<RepastHPCDemoAgent> > *network){
    std::vector<RepastHPCDemoAgent*> agentsToPlay;
    network->successors(this, agentsToPlay);

    double cPayoff     = 0;
    double totalPayoff = 0;
    std::vector<RepastHPCDemoAgent*>::iterator agentToPlay = agentsToPlay.begin();
    while(agentToPlay != agentsToPlay.end()){
        double edgeWeight = network->findEdge(this, *agentToPlay)->weight();
        bool iCooperated = cooperate();                          // Do I cooperate?
        double payoff = (iCooperated ?
						 ((*agentToPlay)->cooperate() ?  7 : 1) :     // If I cooperated, did my opponent?
						 ((*agentToPlay)->cooperate() ? 10 : 3));     // If I didn't cooperate, did my opponent?
        if(iCooperated) cPayoff += payoff + weight * weight;
        totalPayoff             += payoff + weight * weight;
		
        agentToPlay++;
    }
    c      += cPayoff;
    total  += totalPayoff;
	
}

Here we are multiplying the payoff by the weight squared (to make the differences profound). Results should be identical to earlier runs, but with much larger numbers for total payoffs.