eventsimulation.java
来自「JAVA版的蚂蚁算法(Ant Colony Optimization Algor」· Java 代码 · 共 865 行 · 第 1/2 页
JAVA
865 行
tmpInput = input.readLine();
}
// Add the remaining routers to packageCreationDistribution
while ( currentRouterNumber < network.getNumberOfVertices() ) {
packageCreationDistribution.add( new Double( 0 ) );
currentRouterNumber++;
}
while ( !input.readLine().equals( "Router probability" ) ){};
tmpInput = input.readLine();
// Read in all the probabilities until the empty line
currentRouterNumber = 0;
while ( !tmpInput.trim().equals( "" ) ) {
st = new StringTokenizer( tmpInput );
newRouterNumber = Integer.parseInt( st.nextToken() );
// Insert the routers we have skipped
while ( newRouterNumber > currentRouterNumber ) {
packageDestinationDistribution.add( new Double( 0 ) );
currentRouterNumber++;
}
value = Double.parseDouble( st.nextToken() );
packageDestinationDistribution.add( new Double( value ) );
currentRouterNumber++;
tmpInput = input.readLine();
}
// Add the remaining routers to packageCreationDistribution
while ( currentRouterNumber < network.getNumberOfVertices() ) {
packageDestinationDistribution.add( new Double( 0 ) );
currentRouterNumber++;
}
// Normalize the probability for the destinations
Algorithms.normalizeDistribution( packageDestinationDistribution );
while ( !input.readLine().equals( "Time Eventtype Router1 Router2 Value" ) ){};
// Read in all the custom events
tmpInput = input.readLine();
while ( tmpInput != null ) {
st = new StringTokenizer( tmpInput );
time = Double.parseDouble( st.nextToken() );
eventType = st.nextToken();
if ( eventType.equals( "RemoveRouter" ) ) {
// 800 RemoveRouter 3 - - <br>
newRouterNumber = Integer.parseInt( st.nextToken() );
// CREATE EVENT RemoveRouter------------------------------------------
}
else if ( eventType.equals( "SetWireDelay" ) ) {
// 900 SetWireDelay 2 1 10 <br>
newRouterNumber = Integer.parseInt( st.nextToken() );
newRouterNumber2 = Integer.parseInt( st.nextToken() );
value = Double.parseDouble( st.nextToken() );
// CREATE EVENT SetWireDelay------------------------------------------
}
else {
System.err.println( "Parsing error : cannnot recognize event type " +
eventType );
}
tmpInput = input.readLine();
}
input.close();
}
catch ( IOException IOe ) {
IOe.printStackTrace();
}
}
/**
* This method increment the number of lost packages in this simulation with
* 1.
*/
public void incrementLostPackages() {
lostPackages++;
}
/**
* Sets the number of lost packages in this simulation to the value given as
* argument.
*
* @param newVal the new value for the number of lost packages in this simulation
*/
public void setLostPackages( long newVal ) {
lostPackages = newVal;
}
/**
* Accessor for the number of lost in this simulation
*
* @return the number of lost packages in this simulation
*/
public long getLostPackages() {
return lostPackages;
}
/**
* Accessor for the <code>Network</code> contained in the simulation
*
* @return the <code>Network</code> contained in the simulation
*/
public Network getNetwork() {
return network;
}
/**
* Returns the duration of the simulation
*
* @return the duration of the simulation
*/
public long getDuration() {
return duration;
}
/**
* Accessor for the <code>BinaryHeap</code> containing events in the simulation
*
* @return the <code>BinaryHeap</code> containing events in the simulation
*/
public BinaryHeap getEventHeap() {
return bh;
}
/**
* Accessor for the <code>RoutingAlgorithm</code> used in this simulation.
*
* @return the <code>RoutingAlgorithm</code> used in the simulation
*/
public RoutingAlgorithm getRoutingAlgorithm() {
return routingAlgorithm;
}
/**
* This method starts a simulation run. The simulation continues running
* until all scheduled packages have either died or reached their destination.
*/
public void runSimulation() {
System.err.println( "Running simulation..." );
if ( !initialized ) {
throw new RuntimeException( "EventSimulation object not initialized.");
}
Event event;
while ( !bh.isEmpty() ) {
// Extract the element with the minimum priority from the priority queue
event = ( ( Event ) bh.extractMin() );
simTime = event.getTime();
event.doEvent();
}
initialized = false;
System.err.println( "Simulation run completed succesfully." );
}
/**
* Accessor for the name of the file, which contains the layout of the network
* NOTE - that the filename is returned without the filetype ('.txt')
* @return the filename, which contains the layout of the network
*/
public String getNetworkName(){
return networkFile.substring( 0, networkFile.length() - 4 );
}
/**
* Accessor for the name of the file, which contains the layout of the simulation
* NOTE - that the filename is returned without the filetype ('.txt')
* @return the file, which contains the layout of the simulation
*/
public String getSimulationName() {
return simulationFile.substring( 0, simulationFile.length() - 4 );
}
/**
* Accessor for the number of initial packages used in this simulation.
*
* @return the <code>numInitPackages</code> used in the simulation
*/
public int getNumberOfInitialPackages() {
return numInitPackages;
}
/**
* Accessor for the seed used for the random number source.
*
* @return the seed of the random number source.
* @see #rnd
*/
public long getSeed() {
return seed;
}
/**
* Returns <code>true</code> if the <code>initialize</code>-method has
* been run and <code>runSimulation()</code> has not.
*
* @return whether <code>this EventSimulation</code> has been initialized.
* @see #initialize()
* @see #runSimulation()
*/
public boolean isInitialized() {
return initialized;
}
/**
* Accessor for the simulation statistics collected during a run.
*
* @return the simulation statistics collected during a run.
*
* @see #simulationStatistics
* @see #addStat(double[])
* @see #addStatHeaders(String[])
*/
public List getSimStatistics() {
return simulationStatistics;
}
/**
* Increments the accumulated time spent in the network by all packages.
* This value is used as measure for the workload laid upon the network.
*
* @param time the time to increment with
* @param deadAnt is the ant calling this method dead or alive
*/
public void incrementAccumulatedPackageTime( double time, boolean deadAnt ) {
if ( !deadAnt || countDeadAntPackageTime ) {
accumulatedPackageTime += time;
}
}
/**
* This method sets the headers for the gathered statistical data for an
* <code>Eventsimulation</code>-run in the <code>simulationStatistics</code>-list.
* It is called upon the first processing of
* an <code>EventGatherStat</code>-event.
*
* @param headers the header names for the gather data
*
* @see #simulationStatistics
* @see EventGatherStat
* @see #addStat(double[])
*/
public void addStatHeaders( String[] headers ) {
String[] res = new String[ headers.length + 1 ];
res[ 0 ] = "simTime";
System.arraycopy( headers, 0, res, 1, headers.length );
simulationStatistics.add( res );
}
/**
* This method adds a line of data to the gathered statistical data for an
* <code>Eventsimulation</code>-run in the <code>simulationStatistics</code>-list.
* It is called when processing of an <code>EventGatherStat</code>-event.
*
* @param data the date to add to the statistics
*
* @see #simulationStatistics
* @see EventGatherStat
* @see #addStatHeaders(String[])
*/
public void addStat( double[] data ) {
double[] res = new double[ data.length + 1 ];
res[ 0 ] = this.simTime;
System.arraycopy( data, 0, res, 1, data.length );
simulationStatistics.add( res );
}
/**
* Returns the number of ants died from TTL
*
* @return number of ants died from TTL
*/
public int getTTLDeaths() {
return tllDeaths;
}
/**
* Increments the number of ants died from TTL with the given argument
*
* @param val the value to increment the number of ants died from TTL
*/
public void incTTLDeaths( int val ) {
tllDeaths += val;
}
/**
* Returns the number of ants died on inputqueues
*
* @return number of ants died on inputqueues
*/
public int getInputQueueDeaths() {
return inputQueueDeaths;
}
/**
* Increments the number of ants died on inputqueues with the given argument
*
* @param val the value to increment the number of ants died on inputqueues
*/
public void incInputQueueDeaths( int val ) {
inputQueueDeaths += val;
}
/**
* Returns the number of ants died on outputqueues
*
* @return number of ants died on outputqueues
*/
public int getOutputQueueDeaths() {
return outputQueueDeaths;
}
/**
* Increments the number of ants died on outputqueues with the given argument
*
* @param val the value to increment the number of ants died on outputqueues
*/
public void incOutputQueueDeaths( int val ) {
outputQueueDeaths += val;
}
/**
* Returns the number of ants died from no possible routing
*
* @return number of ants died from no possible routing
*/
public int getNPRDeaths() {
return nprDeaths;
}
/**
* Increments the number of ants died from no possible routing with the given
* argument
*
* @param val the value to increment the number of ants died from no possible
* routing
*/
public void incNPRDeaths( int val ) {
nprDeaths += val;
}
/**
* Returns the path/location of the files in this simulation
*
* @return the path/location of the files in this simulation
*/
public String getPath() {
return path;
}
/**
* Returns the accumulated time for packages in this simulation
*
* @return the accumulated time for packages in this simulation
*/
public double getAccumulatedPackageTime() {
return accumulatedPackageTime;
}
/**
* Returns the current time in simulation
*
* @return the current time in simulation
*/
public double getSimTime() {
return simTime;
}
/**
* Returns <code>true</code> if this simulation should count the time
* of dead packages otherwise <code>false</code>
*
* @return returns <code>true</code> if this simulation should count the time
* of dead packages otherwise <code>false</code>
*/
public boolean getCountDeadAntPackageTime() {
return countDeadAntPackageTime;
}
/**
* Returns the argument array given in the simulation file
*
* @return the argument array given in the simulation file
*/
public String[] getArgsArray() {
return argsArray;
}
/**
* Sets the specified <code>index</code> to the value given in <code>value</code>
*
* @param index the index at which to input
* @param value the value to put into the args array
*/
public void setArgs( int index, String value ) {
argsArray[ index ] = value;
}
/**
* The main method, which creates a <code>EventSimulation</code>, runs it
* and draws a graph.
*
* @param args the arguments given to this class
*/
public static void main( String args[] ) {
String path = "..//..//..//..//..//Data//";
EventSimulation sim1 = new EventSimulation( path, "TenByTenGridNetworkHeavy.txt",
"TenByTenSimulationAnt.txt" );
// "TenByTenSimulationDijkstra.txt" );
PicsAndStatGenerator gen1 = new PicsAndStatGenerator( sim1 );
gen1.testRoutingAlgorithm( -1, -1, -1, -1, 0.1, 1 );
/* EventSimulation sim2 = new EventSimulation( path, "TenByTenGridNetwork.txt",
"TenByTenSimulationDijkstra.txt" );
PicsAndStatGenerator gen2 = new PicsAndStatGenerator( sim2 );
gen2.testRoutingAlgorithm(1, 1, 1, 1, 0.5, 1 );*/
// gen2.testNonAntRoutingAlgorithm();
System.exit( 0 );
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?