import java.util.Set;
import java.util.HashSet;
import java.lang.Integer;
/**
*
* Scheduler for synchronous communication round scheduling.
* This determines the order in which various robots will iterate through
* the broadcast, recieve, update cycle.
*
* @author Michael Schuresko
* @version %I%, %G%
* @since 1.0
*/
public class SynchronousScheduler implements ICommunicationScheduler
{
double m_lfTStep;
Set m_setRobots;
boolean m_bVerbose;
/**
* Constructor
* @param lfTStep time between rounds (uniform)
* @param nRobots number of robots
*/
public SynchronousScheduler(double lfTStep, int nRobots)
{
m_bVerbose = false;
populate(lfTStep, nRobots);
}
/**
* Constructor -- can we eliminate the no-argument constructor?
*/
public SynchronousScheduler()
{
m_bVerbose = false;
populate(1.0, 11);
}
/**
* internal function to populate scheduler params
* Used mostly be constructors
*/
protected void populate(double lfTStep, int nRobots)
{
if(m_bVerbose) {
System.out.println("Populating with "+nRobots+"robots");
}
m_lfTStep = lfTStep;
m_setRobots = new HashSet(nRobots);
for(int i = 0; i < nRobots; ++i) {
m_setRobots.add(new Integer(i));
}
}
/**
* Gets the time of the next communication round.
* Useful for the differential equation solver,
* as these communication times are usually discontinuities
* @param lfCurrTime absolute current time
* @return absolute time of next communication round (not "time till next
* round").
*/
public double getNextRoundTime(double lfCurrTime)
{
int nTimes = (int)Math.floor((lfCurrTime+1.00001*m_lfTStep)/m_lfTStep);
return m_lfTStep*nTimes;
}
/**
* Gets the time of the next communication round,
* populates setActiveAgents
* with the indices of those agents which are active on this round.
* @param lfCurrTime current time
* @param nTotalAgents agents are index 0 through nTotalAgents-1
* @param setActiveAgents initially empty, then populated with
* indices (of type Integer) of agents to fire next round.
* @return absolute time of next communication round
*/
public double getNextRoundAndFiringSchedule(double lfCurrTime,
int nTotalAgents,
Set setActiveAgents)
{
setActiveAgents.addAll(m_setRobots);
return getNextRoundTime(lfCurrTime);
}
}