/*
	Module: 	RouterAnimApplet3.java
	Author: 	F. Michael O'Brien (michael@obrienm.com)
	Version:	1.10.30
	Date:		30 Oct 2000
	Documents:	_projv[nnn].doc
	Notes:		
				written for the Sun JDK 1.2.2
	History:
	28 Sep 2000	- Project started
	01 Oct 2000 - subclass RouterAnimApplet	from AnimApplet
	05 Oct 2000 - fixed adjacency fitness calculation
	20 Oct 2000 - redesign for Ant object swarm
	26 Oct 2000 - begin coding for v1
				- Ant Class started, implemented by RouterAnimApplet
				to provide a Turing machine like independent entity that will
				route the circuit board in parallel with other Ant objects
	27 Oct 2000 - Ant.class using behavior genome model, instead of direct state mapping
				of 64bit genome into 18,446,744,073,709,551,616 states!
	30 Oct 2000 - V5: completed swarm enumeration and display, next ga calculation
	04 Nov 2000 - added selection, crossover code


*/

import java.applet.Applet;
import java.awt.*;
import java.util.StringTokenizer;
import java.lang.Exception;
import java.awt.event.*;
import java.util.BitSet;
import java.io.*;

//----------------------------------------------------------------
// Implements:		Ant3, Circuit, Board
// Inherits:		AnimApplet
// Implementors:	-
//----------------------------------------------------------------
public class RouterAnimApplet3 extends AnimApplet2 { //implements MouseListener, MouseMotionListener, ActionListener {
private int	ivGridCounter=0;
private static int izGridHeight,
				izGridWidth;
//				ivFirstTime=0,
//				ivGridGreen2=0;
private int ivBestFitness=0;
private int izMaxAdjacency;	// max fitness attainable, for statistics
private int ivBestGeneration=0;
private int izDeviceNodes=0;
protected boolean	singleStepSimulation=false;
//protected boolean	singleStepSimulation=true;
protected boolean	advanceSingleStep=false;

// vars used to blink objects
protected boolean 	bvOddFrame=true;
protected int 		ivFrameCount=0;
protected int 		ivFrameFlipCount=10;
protected int 		ivFrames=0;
protected int 		ivFrames2=0;
// show statistics
protected static boolean	bvShowStatistics=true;

//protected int 		counter=0;

/*	private int			izMaxWidth,
						izMaxHeight;
*/

// constants
// fitness criteria
/*	private int		izFitnessCriteriaLowerUnconnected=1;
	private int		izFitnessCriteriaLowerConnected=4;
	private int		izFitnessCriteriaUpperUnconnected=1;
	private int		izFitnessCriteriaUpperConnected=4;
	private int		izFitnessCriteriaBothUnconnected=4;
	private int		izFitnessCriteriaBothConnected=16;
*/
private long lvGeneration=0;
private int ivIndividual=0;

// constants needed for colors
private int izSpacing,izSpacing2,izSpacing3,izSpacing4,izSpacing6;
private Color color_top, color_bottom, color_both, color_none;
private Color color_connect1, color_connect2, color_connect3;
private Color color_connect3_lower, color_connect3_upper, color_connect3_both, color_device;
/*
private static int	ndir[]={9,8,11,10,5,4,7,6};
private static int	xdir[]={1,0,-1,0};
private static int	ydir[]={0,-1,0,1};
*/
private static int	xdir[]={1,0,-1,0};
private static int	ydir[]={0,-1,0,1};
private int	ivBullsEye[][];
private boolean ivDestFound[][];
private static int	izMaxBullsEye=60;

// cell state
private int ivBoard[][];
//	private int ivBoardGenome[][][];
//	private int ivBoardGenomeParents[][][];
private static int population;
private static float probMutation;
private static int izPopulationPerNode;
private static int izNumberNets;
private static int izNumberNodes;		// total nodes - duplicates
private static int izTotalNumberNodes;	// total nodes with duplicates

//private int ivAdjacencyCount[];
//private Ant3 topAntSwarms[][];
// keep the best swarm of all time
private Ant3 bestAntSwarm[];
private Ant3 selectedAnt;
// to keep track of the fitness number for each swarm
//private int	ivTopIndividualsFitness[];
// the number of swarms that can mate
private static float probCrossover;

// trace data structures
private int izNetNodeCount[];
// individual nodes, as part of a net, and separate
private int izNetNodeX[][],izNetNodeY[][];

// Ant GA variables
// x,y,layer board positioning data
private Ant3 antSwarms[][];
private int cSwarm;

/*
// adjacency fitness
private int deviceNodeX[],deviceNodeY[];
private int adjacencyMatrix[][];
private int deviceNodeCount;
*/
// discrete time step animation or GA computation?
private boolean bvPerformCrossover=false;
private boolean bvInitializeIndividuals=true;
private boolean bvSwitchOverNow=false;

// for statistics
private	long	lvFitnessTotal=0;
private int		ivFitnessAverage=0;

// for file output
//private	byte fileBuffer[] = new byte[80];
private DataOutputStream outFile;  
// target nodes
//private int

// trace path vectors
// connection adjacency matrix
//private int connectedAbove, connectedBelow;
// private custom objects

// popup menu
	private String svPopupNames[] = {"Single Step Mode On","Single Step Mode Off","Reset"};
	private MenuItem thePopupMenuItems[];
	private	PopupMenu aPopupMenu;
		
// load the images when the applet begins executing
public void init()
{
	int ivParameterCount;	// used to parse multi-parameter strings
	int ivTemp; 			// used in parsing while loop
	int ivNodeCount;		// current node

	izSleepTime=0;    // milliseconds to sleep
	// set colors
	color_top = new Color(0,255,64);				
	color_bottom = new Color(0,64,255);
	color_both = new Color(255,0,0);
	color_none = Color.darkGray;
	color_device = new Color(160,160,160);
	color_connect1 = new Color(128,128,255);
	color_connect2 = new Color(128,255,128);
	//color_connect3 = new Color(255,128,128);
	color_connect3_lower = new Color(255,0,255);
	color_connect3_upper = new Color(255,255,0);
	color_connect3_both = new Color(255,255,255);

	//super.init();
	if(bvApplet) {
		izMaxHeight=Integer.parseInt(getParameter("pixelHeight"));
		izMaxWidth=Integer.parseInt(getParameter("pixelWidth"));
	}
	// create graphics subsystem
	buffer = createImage(izMaxWidth,izMaxHeight); // create image buffer
	gContext = buffer.getGraphics(); // get graphics context

	// set background of buffer to black
	gContext.setColor(Color.black);	  
	gContext.fillRect(0,0,izMaxWidth,izMaxHeight+30);

	// applet listen for mouse events, ok
	addMouseListener(this);
	addMouseMotionListener(this);

	// get parameters when in applet mode		
	if(bvApplet) {
		izMaxHeight=Integer.parseInt(getParameter("pixelHeight"));
		izMaxWidth=Integer.parseInt(getParameter("pixelWidth"));
		izGridHeight=Integer.parseInt(getParameter("gridHeight"));
		izGridWidth=Integer.parseInt(getParameter("gridWidth"));
		population=Integer.parseInt(getParameter("population"));	
		probMutation=Integer.parseInt(getParameter("mutationMultOutOf10000"))/10000;
		probCrossover=Integer.parseInt(getParameter("replacementPercent"))/100;
		izNumberNets=Integer.parseInt(getParameter("numberNets"));
		izNumberNodes=Integer.parseInt(getParameter("numberNodes"));
		izPopulationPerNode=Integer.parseInt(getParameter("populationPerNode"));
		if(Integer.parseInt(getParameter("showStatistics"))>0)
			bvShowStatistics=true;
		else
			bvShowStatistics=false;
/*
		// initialize node data structures
		izNetNodeCount = new int[izNumberNets];
		izNetNodeX = new int[izNumberNets][izNumberNodes];
		izNetNodeY = new int[izNumberNets][izNumberNodes];
	
		// initialize Ant objects
		antSwarms = new Ant3[population][izNumberNodes];
		//topAntSwarms = new Ant3[population][izNumberNodes];

		ivNodeCount=0;	// start with 0 nodes
		for(short net=0;net<izNumberNets;net++) {
		    try {				   
				//String aParameter = "net" + (new Integer(net+1)).toString() + "Nodes";
				izNetNodeCount[net]=Integer.parseInt(getParameter("net" + (new Integer(net+1)).toString() + "Nodes"));
				// extract out the x coordinates of the net of nodes
				if (getParameter("net" + (new Integer(net+1)).toString() + "x") != null) {
				   	StringTokenizer Points = new StringTokenizer(getParameter("net" + (new Integer(net+1)).toString() + "x"),"\t\n\r ,;.");
					ivParameterCount=0;
		    		while (Points.hasMoreTokens()) {
						izNetNodeX[net][ivParameterCount++] = new Integer(Points.nextToken()).intValue();
						//System.out.println("x" + (new Integer(izNetNodeX[net][ivParameterCount-1])).toString());
					}
				}
				// extract out the y coordinates of the net of nodes
				if (getParameter("net" + (new Integer(net+1)).toString() + "y") != null) {
				   	StringTokenizer Points = new StringTokenizer(getParameter("net" + (new Integer(net+1)).toString() + "y"),"\t\n\r ,;.");
					ivParameterCount=0;
		    		while (Points.hasMoreTokens()) {
						izNetNodeY[net][ivParameterCount++] = new Integer(Points.nextToken()).intValue();
						//System.out.println("y" + (new Integer(izNetNodeY[net][ivParameterCount-1])).toString());
					}
				} 
		    } catch (Exception e) {}
		}
*/
		System.out.println("_izGridHeight: " + (new Integer(izGridHeight)).toString());
		System.out.println("_izGridWidth: " + (new Integer(izGridWidth)).toString());
		System.out.println("_population: " + (new Integer(population)).toString());	

		} else { // taken care of in main()
		// preset some values
		//loadBoardExample1(0, 0);
	}

	// initialize board
	ivBoard = new int[izGridWidth][izGridHeight];

// 	loadBoardExample1(0, 0);
	loadBoardParadc(7,1);
	loadSwarmParadc(7,1);
	izMaxAdjacency=(izGridHeight*izGridWidth-izDeviceNodes-(izGridHeight*2)-((izGridWidth-2)*2))*4*16;
	//System.out.println("i" + (new Integer(izMaxAdjacency)).toString());

	izSpacing=(int)(izMaxHeight-60)/izGridHeight;
	izSpacing2=(int)(izMaxHeight-60)/izGridHeight/2;
	izSpacing3=(int)(izMaxHeight-60)/izGridHeight/3;
	izSpacing4=(int)(izMaxHeight-60)/izGridHeight/6;
	izSpacing6=(int)(izMaxHeight-60)/izGridHeight/12;

	///////////////////////////////////
	// create initial random population
	///////////////////////////////////
	loadRandomPopulation();
	// current swarm being simulated
	cSwarm=0;

	// initialize board grid
	// calculate the number of swarms that can mate
	//izCrossoverCount=(int)(population*izReplacementRatio/100);
	//ivBoard = new BitSet(izGridHeight*izGridWidth*2);
	//ivBoard = new int[izGridWidth][izGridHeight];
	//ivBoardGenome = new int[izGridWidth][izGridHeight][population];
	//ivBoardGenomeParents = new int[izGridWidth][izGridHeight][izCrossoverCount];
	//ivAdjacencyCount = new int[population];
	// to keep track of the fitness number for each swarm
	//ivTopIndividualsFitness = new int[(int)(population*izReplacementRatio/100)];

	// keep a separate copy of the top swarms
	//ivTopIndividuals = new Ant3[(int)(population*izReplacementRatio/100)];
 /*
	deviceNodeX = new int[10];
	deviceNodeY = new int[10];
	deviceNodeCount=2;
	deviceNodeX[0]=1;
	deviceNodeY[0]=1;
	deviceNodeX[1]=1;
	deviceNodeY[1]=izGridHeight-2;
	adjacencyMatrix = new int[izGridWidth][izGridHeight];

	// initialize arrays
	for(int i=0;i<population;i++) {
		ivAdjacencyCount[i]=0;
	}
*/
	// create popup menu
	aPopupMenu = new PopupMenu("Popup");
	thePopupMenuItems = new MenuItem[svPopupNames.length];

	for(int i=0;i<svPopupNames.length;i++)	{
		thePopupMenuItems[i] = new MenuItem(svPopupNames[i]);
		aPopupMenu.add(thePopupMenuItems[i]);
		thePopupMenuItems[i].addActionListener(this);
	}

  	// add popup menu to this canvas
	add(aPopupMenu);
	enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  
  	// write output header to disk for non-applets
  	if(!bvApplet) {
  		try {
  			// create output stream
  			outFile = new DataOutputStream(new FileOutputStream("out.txt",true));
  			outFile.writeBytes("RouterAnimApplet:\r\n");
  			outFile.writeBytes("Population: \t" + (new Integer(population)).toString() + "\r\n");
  			outFile.writeBytes("Crossover: \t" + (new Float(probCrossover)).toString() + " Percent\r\n");
  			outFile.writeBytes("izGridHeight: \t" + (new Integer(izGridHeight)).toString() + "\r\n");
  			outFile.writeBytes("izGridWidth: \t" + (new Integer(izGridWidth)).toString() + "\r\n");
  			outFile.writeBytes("izMutationMultiplier: \t" + (new Float(probMutation)).toString() + "\r\n");
  			//outFile.writeBytes("izGridWidth: \t" + (new Integer(izGridWidth)).toString() + "\r\n");
  			outFile.writeBytes("gen\tfit\tafit\r\n");
  			outFile.close();
  		} catch (Exception e) {
  			String err = e.toString();
  			System.out.println(err);
  		}
  	System.out.println("\nF.Michael O'Brien: RouterAnimApplet.java");
  	System.out.println("must alt-tab out and back in to step past");
  	System.out.println("sun.awt.windows.WGraphics.drawImage(WGraphics.java:360) bug");
  	System.out.println("when using double-buffering");

//	for(int i=0;i<4;i++)
//		System.out.println("xdir["+ (new Integer(xdir[i])).toString() + "]:ydir["+ (new Integer(ydir[i])).toString() + "]");
  	}  
}



// display the image in the Applet's Graphics context
public void paint(Graphics g) {
	int xd,yd;
	int tempx, tempy;
	int ivDirx, ivDiry;
	String stringRep;	// string binary rep of grid cell 0-1024	
	boolean connected;
	int cellColor;
	int ivMaxConnections=0;
	int lastIndex;
	int p1,p2;
	int tempBitPos;
	BitSet aBitSet;

	//super.paint(g);
	g.drawImage(buffer,0,0,this);
	// clear previous image from buffer
	gContext.setColor(Color.black);
	gContext.fillRect(0,0,izMaxWidth,izMaxHeight);

	
	// initialize arrays?
	//initializeSimulation();
 
	// display current solution
	displayCurrentSolution();

	// display genome statistics
	if(bvShowStatistics) {
		displayStatistics();
	}

	// increment timestep
	simulationTimeStep();

	// write out numerical parameters
 	//gContext.setColor(Color.cyan);
	//gContext.drawString("F: " + (new Integer(ivFrames)).toString(),10,izMaxHeight-16);
 	gContext.setColor(Color.white);
	gContext.drawString("Population: " + (new Integer(cSwarm)).toString(),izMaxHeight,16);
	gContext.drawString("Frame: " + (new Integer(ivFrames2)).toString() + ":" + (new Integer(ivFrames)).toString() ,izMaxHeight+100,16);
	//gContext.drawString("I: " + (new Integer(ivIndividual)).toString(),80,izMaxHeight-16);
	//gContext.drawString("G: " + (new Integer((int)lvGeneration)).toString(),120,izMaxHeight-16);
	//gContext.drawString("A: " + (new Integer(ivAdjacencyCount[ivIndividual])).toString(),180,izMaxHeight-16);
	//gContext.drawString("P: " + (new Integer((int)Math.floor(100*ivAdjacencyCount[ivIndividual]/izMaxAdjacency+.5))).toString(),240,izMaxHeight-16);

	// compute fitness
	computeFitness();
	
	// perform crossover
	performSelectionAndCrossoverWithMutation();
 	
  	//try {Thread.sleep( izSleepTime );}
	//catch ( InterruptedException e ){showStatus(e.toString());}
	repaint();  // display buffered image
} // paint()

// display current solution
private void displayCurrentSolution() {
	int px, py;
	Ant3 anAnt;
	int nId;


	// draw board
	for(int i=0;i<izGridWidth;i++) {
		//System.out.println(i);
		for(int j=0;j<izGridHeight;j++) {
			if(ivBoard[i][j]==512) {
				gContext.setColor(Color.red);
				gContext.fillRect(10+i*izSpacing+izSpacing2,
					10+j*izSpacing+izSpacing2,
					izSpacing3,izSpacing3);
			} else if(ivBoard[i][j]>0) {
				gContext.setColor(Color.darkGray);
				gContext.fillRect(10+i*izSpacing+izSpacing2,
					10+j*izSpacing+izSpacing2,
					izSpacing3,izSpacing3);
			
//				gContext.setColor(Color.cyan);
//				gContext.drawString((new Integer(0)).toString(),
//				10+i*izSpacing+izSpacing2,
//				9+j*izSpacing+izSpacing2);
			}
/*			if(ivBoard[i][j]==1) {
				gContext.setColor(Color.yellow);
				gContext.fillRect(10+i*izSpacing+izSpacing2,
						10+j*izSpacing+izSpacing2,
						izSpacing3,izSpacing3);
			}				
*/
		}
	}		 

	// draw Ants
	for(int n=0;n<izTotalNumberNodes;n++) {
		anAnt=antSwarms[cSwarm][n];
		nId=anAnt.getNodeId();
		//px=anAnt.getPosy();	// traced bug for last 24 hours here 1419 2000oct30
		//py=anAnt.getPosx();
		py=anAnt.getPosy();	// traced bug for last 24 hours here 1419 2000oct30
		px=anAnt.getPosx();

		// draw line to target
		if(anAnt.getcEnd())
			gContext.setColor(Color.green);
		else   
			gContext.setColor(Color.blue);
		gContext.drawLine(10+px*izSpacing+izSpacing2+izSpacing4,
					10+py*izSpacing+izSpacing2+izSpacing4,
					10+anAnt.getDestx()*izSpacing+izSpacing2+izSpacing4,
					10+anAnt.getDesty()*izSpacing+izSpacing2+izSpacing4);

		// draw line to src if finished
		if(anAnt.getcEnd()) {
			gContext.setColor(Color.green);
			if((anAnt.getpEnd()||ivBullsEye[cSwarm][nId]<izMaxBullsEye)&&!ivDestFound[cSwarm][nId]) {
				//gContext.setColor(Color.magenta);
				gContext.setColor(Color.green);
				gContext.drawOval(10+px*izSpacing+izSpacing2+izSpacing4-ivBullsEye[cSwarm][nId],
					10+py*izSpacing+izSpacing2+izSpacing4-ivBullsEye[cSwarm][nId],
					ivBullsEye[cSwarm][nId]*2,ivBullsEye[cSwarm][nId]*2);
					ivBullsEye[cSwarm][nId]--;
					if(ivBullsEye[cSwarm][nId]==0) {
						ivDestFound[cSwarm][nId]=true;
						ivBullsEye[cSwarm][nId]=izMaxBullsEye+1;
					}
			} else {
				gContext.drawLine(10+px*izSpacing+izSpacing2+izSpacing4,
					10+py*izSpacing+izSpacing2+izSpacing4,
					10+anAnt.getSrcx()*izSpacing+izSpacing2+izSpacing4,
					10+anAnt.getSrcy()*izSpacing+izSpacing2+izSpacing4);
			}

		}

		//ivBoard[px][py]=1;
		if(selectedAnt.getNodeId()==anAnt.getNodeId())
			gContext.setColor(Color.green);
		else
			gContext.setColor(Color.white);
		gContext.fillRect(10+px*izSpacing+izSpacing2,
					10+py*izSpacing+izSpacing2,
					izSpacing3,izSpacing3);
		gContext.setColor(Color.cyan);
		gContext.drawString((new Integer(anAnt.getNodeId())).toString(),
			10+px*izSpacing+izSpacing2,
			9+py*izSpacing+izSpacing2);
//		gContext.drawString((new Integer(anAnt.getPosx())).toString(),
//			26+px*izSpacing+izSpacing2,
//			9+py*izSpacing+izSpacing2);
//		gContext.setColor(Color.blue);
//		gContext.drawString((new Integer(anAnt.getPosy())).toString(),
//			42+px*izSpacing+izSpacing2,
//			9+py*izSpacing+izSpacing2);
	}								 
}

private int enumerateBoardCell(int x,int y,int anId) {
	Ant3 anAnt;
	// direction:				values:
	// 0-11 10du swne swne		0 = empty			4 = device
	//      0123 4567 8901		1 = trace other		5 = Ant other
	//    p 1098 7654 3210		2 = trace twin		6 = Ant twin
	//                6745		3 = cell src 		7 = cell dest

	//System.out.println("_x,y: " + (new Integer(x)).toString() + "," + (new Integer(y)).toString());
	if(ivBoard[x][y]==512)
		return 4;
	if(ivBoard[x][y]==anId+1)
		return 2;
//	if(ivBoard[x][y]<1)
// 		return 1;
	// check all other ants for position info
	for(int n=0;n<izTotalNumberNodes;n++) {
		anAnt=antSwarms[cSwarm][n];
		// we wont have to check that ant will enumerate itself since we dont check 0,0
		if((anAnt.getPosx()==x)&&(anAnt.getPosy()==y))
			if(anAnt.getNetId()==anId) {
				return 6;
			} else {
				return 5;
			}
	}
	return 0;
}

private void updateProximity() {
	Ant3 anAnt;
	int x,y;
	for(int n=0;n<izTotalNumberNodes;n++) {
		anAnt=antSwarms[cSwarm][n];
		//System.out.println((new Integer(n)).toString());
		// update proximity for ants
		// should need no torodial checking since devices will always be along edges

		x=anAnt.getPosx();
		y=anAnt.getPosy();
		ivBoard[x][y]=anAnt.getNetId()+1;
		anAnt.setProxBelow(
			enumerateBoardCell(x+xdir[0],y+ydir[0],anAnt.getNetId()),
			//enumerateBoardCell(x+1,y-1,anAnt.getNetId()),
			enumerateBoardCell(x+xdir[1],y+ydir[1],anAnt.getNetId()),
			//enumerateBoardCell(x-1,y-1,anAnt.getNetId()),
			enumerateBoardCell(x+xdir[2],y+ydir[2],anAnt.getNetId()),
			//enumerateBoardCell(x-1,y+1,anAnt.getNetId()),
			enumerateBoardCell(x+xdir[3],y+ydir[3],anAnt.getNetId())
			//enumerateBoardCell(x+1,y+1,anAnt.getNetId()));
			);
 /*
		gContext.setColor(Color.cyan);
			gContext.drawString((new Integer(enumerateBoardCell(x+xdir[0],y+ydir[0],anAnt.getNetId()))).toString(),
				140+izMaxHeight+12*3,
				20+14*n);
			gContext.drawString((new Integer(enumerateBoardCell(x+xdir[1],y+ydir[1],anAnt.getNetId()))).toString(),
				140+izMaxHeight+12*2,
				20+14*n);
			gContext.drawString((new Integer(enumerateBoardCell(x+xdir[2],y+ydir[2],anAnt.getNetId()))).toString(),
				140+izMaxHeight+12*1,
				20+14*n);
			gContext.drawString((new Integer(enumerateBoardCell(x+xdir[3],y+ydir[3],anAnt.getNetId()))).toString(),
				140+izMaxHeight+12*0,
				20+14*n);
*/
/*		for(int i=0;i<4;i++)		
			gContext.drawString((new Integer(enumerateBoardCell(x+xdir[i],y+ydir[i],anAnt.getNetId()))).toString(),
				140+izMaxHeight+12*i,
				20+14*n);
*/			  
	}		  
}

private void simulationTimeStep() {
	int x,y;
	Ant3 anAnt;
	//System.out.println("_simulationTimeStep()");

	// first place all the ants in the current swarm
/*	for(int n=0;n<izTotalNumberNodes;n++) {
		ivBoard[antSwarms[cSwarm][n].getPosx()][antSwarms[cSwarm][n].getPosy()]=antSwarms[
	}
*/
	if((!singleStepSimulation)||(advanceSingleStep)) {
		advanceSingleStep=false;

//	if(ivFrames>2000) {
//		ivFrames=0;
//		init();
//	} 

	// update frame and frameflip count
	ivFrames++;
	if(ivFrameCount<ivFrameFlipCount) {
		bvOddFrame=false;
		ivFrameCount++;
	} else {
		if(ivFrameCount<ivFrameFlipCount*2) {
			bvOddFrame=true;
			ivFrameCount++;
		} else {
			bvOddFrame=false;
			ivFrameCount=0;
		}				
	}

	// advance swarm count
	if(ivFrames2<100)
		ivFrames2++;
	else
		ivFrames2=0;

	if(ivFrames2==0) {
		if(cSwarm<population-1) {
			cSwarm++;
		} else {
			cSwarm=0;
		}
		loadBoardParadc(7,1);
	}

		for(int n=0;n<izTotalNumberNodes;n++) {
			anAnt=antSwarms[cSwarm][n];
			if(anAnt.computeNextState()>0) {
				// recompute positions
				// to deal withe the case that 2 ants are 3 cells apart, aproaching each other
				// they will both have clearance to occupy the center cell unless we update the
				// proximity after every move
				updateProximity(); //2000oct30
			}
			//anAnt.getPosx();
		}
	}
}

// display genome statistics
private void displayStatistics() {
	// draw Ants
	int px,py,prox;
	Ant3 anAnt;
	int proxAnt[];

	// draw box around selected node
	gContext.setColor(Color.green);
	gContext.drawRect(izMaxHeight-5,28+13*selectedAnt.getNodeId(),200,13);

	for(int n=0;n<izTotalNumberNodes;n++) {
		anAnt=antSwarms[cSwarm][n];
		//px=anAnt.getPosy();	// traced bug for last 24 hours here 1419 2000oct30
		//py=anAnt.getPosx();
		py=anAnt.getPosy();
		px=anAnt.getPosx();
		//System.out.println("_px,py: " +	(new Integer(px)).toString()+","+(new Integer(px)).toString());
		gContext.setColor(Color.cyan);
		gContext.drawString((new Integer(n)).toString(),
			izMaxHeight,
			40+13*n);
		gContext.drawString((new Integer(anAnt.getNetId())).toString(),
			16+izMaxHeight,
			40+13*n);
		if((px==1)||(px==izGridWidth-2))
			gContext.setColor(Color.red);
		else
			gContext.setColor(Color.cyan);
		gContext.drawString((new Integer(px)).toString(),
			32+izMaxHeight,
			40+13*n);
		if((py==1)||(py==izGridHeight-2))
			gContext.setColor(Color.red);
		else
			gContext.setColor(Color.cyan);

		gContext.drawString((new Integer(py)).toString(),
			48+izMaxHeight,
			40+13*n);

		// draw proximity grid
		gContext.setColor(color_none);
		gContext.fillRect(80+izMaxHeight-4,
					34+13*n-4,
					12,12);

		// draw 4 prox cells		
		for(int d=0;d<4;d++) {
			prox=enumerateBoardCell(px+xdir[d],py+ydir[d],anAnt.getNetId());
			if(prox>0) {
				if(prox==5)
					gContext.setColor(Color.yellow);
				else if(prox==6)
					gContext.setColor(Color.green);
				else if(prox==4)
					gContext.setColor(Color.red);
				gContext.fillRect(80+izMaxHeight+xdir[d]*4,
					34+13*n+ydir[d]*4,
					4,4);
			}
		}
		
/*		// draw proximity grid
		gContext.setColor(Color.lightGray);
		gContext.fillRect(96+izMaxHeight-4,
					34+14*n-4,
					12,12);

		// display prox from ant
		for(int d=0;d<4;d++) {
			prox=anAnt.getProxBelow(d);
			if(prox>0) {
				if(prox==5)
					gContext.setColor(Color.yellow);
				else if(prox==6)
					gContext.setColor(Color.green);
				else if(prox==4)
					gContext.setColor(Color.red);
				gContext.fillRect(96 + izMaxHeight+xdir[d]*4,
					34+14*n+ydir[d]*4,
					4,4);
			}
		}
*/
		if(anAnt.getcEnd())
			gContext.setColor(Color.green);
		else
			gContext.setColor(Color.cyan);
			gContext.drawString((new Integer(enumerateBoardCell(px+xdir[0],py+ydir[0],anAnt.getNetId()))).toString(),
				95+izMaxHeight+8*3,
				40+13*n);
			gContext.drawString((new Integer(enumerateBoardCell(px+xdir[1],py+ydir[1],anAnt.getNetId()))).toString(),
				95+izMaxHeight+8*2,
				40+13*n);
			gContext.drawString((new Integer(enumerateBoardCell(px+xdir[2],py+ydir[2],anAnt.getNetId()))).toString(),
				95+izMaxHeight+8*1,
				40+13*n);
			gContext.drawString((new Integer(enumerateBoardCell(px+xdir[3],py+ydir[3],anAnt.getNetId()))).toString(),
				95+izMaxHeight+8*0,
				40+13*n);
					
	}

}


// override update to eliminate flicker
public void update(Graphics g){paint(g);}

private void computeFitness() {
}

private void performSelectionAndCrossoverWithMutation() {
}

private void performSelectionAndCrossoverWithOutMutation() {
}

private void simulateTimestep() {
}

private void initializeSimulation() {
}

private void loadRandomPopulation() {
	
	// create a swarm of Ant objects with random genomes
	// 
	//for(int i=0;i<

	/*
		//if((int)(Math.floor(Math.random()*512))>500) {
		//	for(int i=0;i<izGridWidth;i++)
		//		for(int j=0;j<izGridHeight;j++)
		//			if(ivBoard[i][j]<512)
		//				//ivBoard[i][j]=(int)(Math.floor(Math.random()*512));
		//				ivBoard[i][j]=511;//i*izGridHeight+j;
		//} else {
			for(int i=0;i<izGridWidth;i++)
				for(int j=0;j<izGridHeight;j++)
					if(ivBoard[i][j]<512)
						ivBoard[i][j]=(int)(Math.floor(Math.random()*512));
		//}
	*/
}

private void copyGenerationTo(int gen) {
// 	for(int i=0;i<izGridWidth;i++)
// 		for(int j=0;j<izGridHeight;j++)
// 			ivBoardGenome[i][j][gen]=ivBoard[i][j];
}


private void loadBoardParadc(int x, int y) {
	int izDeviceNodes[]={
		4,4,4,5,4,6,4,8,4,10,	// 311
		7,4,7,5,7,6,7,8,7,10,
		8,4,8,5,8,6,8,8,8,10,
		11,4,11,5,11,6,11,8,11,10,
		4,15,4,16,4,17,4,18,4,19,4,20,4,21,4,22,	// 541
		7,15,7,16,7,17,7,18,7,19,7,20,7,21,7,22		// 541
		};
	// clear board
	for(int i=0;i<izGridHeight;i++) {
		for(int j=0;j<izGridWidth;j++) {
			ivBoard[j][i]=0;
		}
	}
	// initialize devices
	for(int i=0;i<izGridHeight;i++) {
		ivBoard[0][i]=512;
		ivBoard[izGridWidth-1][i]=512;
	}
	for(int i=0;i<izGridWidth;i++) {
		ivBoard[i][0]=512;
		ivBoard[i][izGridHeight-1]=512;
	}

	for(int i=0;i<izDeviceNodes.length;i+=2) {	
		ivBoard[izDeviceNodes[i]+x][izDeviceNodes[i+1]+y]=512;
	}
}

private void loadSwarmParadc(int x, int y) {
	int lNodeCount=0;
	Ant3 anAnt;

	int izNetNodes[][]={
		{4,22,7,17,7,19,4,10,7,10,8,10,11,10},	// 541,311 g
		{7,15,7,21,4,4,7,4,8,4,11,4},	// 541 p
		{7,16,8,6},	// 541 d0
		{4,15,8,5},	// 541 d1
		{4,16,11,5},	// 541 d2
		{4,17,11,6},	// 541 d3										
		{4,18,4,6},	// 541 d4										
		{4,19,4,5},	// 541 d5										
		{4,20,7,5},	// 541 d6										
		{4,21,7,6},	// 541 d7										
		{7,22,4,8,8,8},	// 541-311 con										
		{7,18,7,20}		// 541 clk										
		};
 
						
 	// initialize node data structures
	izPopulationPerNode=1;
	izNumberNets=izNetNodes.length;
	izNumberNodes=0;
	for(int i=0;i<izNumberNets;i++)
		izNumberNodes+=(int)(izNetNodes[i].length/2);
	izTotalNumberNodes=izNumberNodes*izPopulationPerNode;
  	izNetNodeCount = new int[izNumberNets];
	izNetNodeX = new int[izNumberNets][izTotalNumberNodes];
	izNetNodeY = new int[izNumberNets][izTotalNumberNodes];
	System.out.println("_izNumberNodes: " + (new Integer(izNumberNodes)).toString());
	System.out.println("_izNumberNets: " + (new Integer(izNumberNets)).toString());
	// initialize Ant objects
	antSwarms = new Ant3[population][izTotalNumberNodes];
	ivBullsEye = new int[population][izTotalNumberNodes];
	ivDestFound = new boolean[population][izTotalNumberNodes];
	for(int i=0;i<population;i++)
		for(int j=0;j<izNumberNodes;j++)
			for(int k=0;k<izPopulationPerNode;k++) {
				antSwarms[i][j*izPopulationPerNode+k]=new Ant3(j*izPopulationPerNode+k);
				//System.out.println("_loadBoardExample: " + (new Integer(x)).toString() + "," + (new Integer(y)).toString());
				}

	for(int k=0;k<izPopulationPerNode;k++) {
		lNodeCount=0;
		for(int j=0;j<izNumberNets;j++) {
			izNetNodeCount[j]=(int)(izNetNodes[j].length/2);
			for(int i=0;i<((int)(izNetNodes[j].length/2));i++) {
				izNetNodeX[j][k+i]=izNetNodes[j][i*2]+x; izNetNodeY[j][k+i]=izNetNodes[j][i*2+1]+y;
				for(int p=0;p<population;p++) {
					antSwarms[p][lNodeCount].setPosx(izNetNodeX[j][k+i]);
					antSwarms[p][lNodeCount].setPosy(izNetNodeY[j][k+i]);
					ivBullsEye[p][lNodeCount]=izMaxBullsEye;
					ivDestFound[p][lNodeCount]=false;
				}
				lNodeCount++;
			}
		}
	}

	for(int p=0;p<population;p++) {	
		lNodeCount=0;
		for(int n=0;n<izNumberNets;n++) {
			for(int i=0;i<izNetNodeCount[n];i++) {
				for(int k=0;k<izPopulationPerNode;k++) {
					anAnt=antSwarms[p][lNodeCount];
					// set src, dest nodes
					//ivBoard[anAnt.getPosx()][anAnt.getPosy()]=256;
					if(i==izNetNodeCount[n]-1) {
						anAnt.setDestx(izNetNodeX[n][0]);
						anAnt.setDesty(izNetNodeY[n][0]);
					} else {
						anAnt.setDestx(izNetNodeX[n][i+1]);
						anAnt.setDesty(izNetNodeY[n][i+1]);
					}
					//System.out.println("_n,i: "+(new Integer(n)).toString()+","+(new Integer(i)).toString());
	

					// set sequential properties
					anAnt.setNetId(n);
					anAnt.setLayer(0);
					anAnt.setNodeId(lNodeCount);
					anAnt.setSrcx(anAnt.getPosx());
					anAnt.setSrcy(anAnt.getPosy());
					//System.out.println((new Integer(lNodeCount)).toString());
					lNodeCount++;
				}
			}
		}
	}
	// default selectedAnt
	selectedAnt=antSwarms[0][0];
}


	private void setValues(String event, int x, int y) {
		s=event; xPos=x; yPos=y; //repaint();
	}

	public void mouseClicked(MouseEvent e) { 
		setValues("Clicked",e.getX(),e.getY());
		//advanceSingleStep=true;
 	}

	public void mousePressed(MouseEvent e) { 
		setValues("Pressed",e.getX(),e.getY());
	}

	public void mouseReleased(MouseEvent e)	{ 
		setValues("Released",e.getX(),e.getY());
	}

	public void mouseEntered(MouseEvent e) { 
		setValues("Entered",e.getX(),e.getY());
	}

	public void mouseExited(MouseEvent e) { 
		setValues("Exited",e.getX(),e.getY());
	}

	public void mouseDragged(MouseEvent e) {
		setValues("Dragging",e.getX(),e.getY());
		Ant3 anAnt;
		//gContext.setColor(Color.cyan);
		//gContext.drawString((new Integer(e.getX())).toString(),e.getX(),e.getY());
		//gContext.drawString((new Integer(e.getY())).toString(),e.getX()+30,e.getY());
		for(int n=0;n<izTotalNumberNodes;n++) {
			anAnt=antSwarms[cSwarm][n];
			if((Math.abs(10+anAnt.getPosx()*izSpacing+izSpacing4+izSpacing2-e.getX())<=izSpacing3)&&(Math.abs(10+anAnt.getPosy()*izSpacing+izSpacing2+izSpacing4-e.getY())<=izSpacing3))
				selectedAnt=anAnt;				
		}							   
	}

	public void mouseMoved(MouseEvent e) {
		setValues("Moving",e.getX(),e.getY());
	}

	// capture popup menu event
	public void processMouseEvent(MouseEvent e) {
		if(e.isPopupTrigger())
			aPopupMenu.show(this,e.getX(),e.getY());
		//super.processMouseEvent(e); // only when not sublassed twice
	}
	
	// process popup menu event
	public void actionPerformed(ActionEvent e) {
//	private String svPopupNames[] = {"Single Step Mode On","Single Step Mode Off","Reset"};
		//"Single Step Mode On"
		if(e.getSource()==thePopupMenuItems[0]) {
			singleStepSimulation=true;
			advanceSingleStep=true;
		}
		
		//"Single Step Mode Off"
		if(e.getSource()==thePopupMenuItems[1]) {
			singleStepSimulation=false;
		}
		
		//"Reset"
		if(e.getSource()==thePopupMenuItems[2]) {
			init();
		}
	}	

// start the applet
public void start() {
	super.start();
}

public void stop() {
	super.stop();
}

//static boolean bvApplet=true;
// allow application use outside of browser, appletviewer
public static void main(String args[]) {
	bvApplet=false;
	// cl-parameters = Population MaxHeight MaxWidth GridHeight MutationMultiplier dzReplacementRatio
	// enumerate arguments passed from the console
	if(args.length > 5) {
		//for(int i=0;i<args.length;i++)
		//	if(args[i].equals("-debug")) 
				//bzDebug = true; // set global debug flag
		//	else { usage(); System.exit(0); }
		// get arguments
		population=Integer.parseInt(args[0]);	
		izMaxHeight=Integer.parseInt(args[2]);
		izMaxWidth=Integer.parseInt(args[1]);
		izGridHeight=Integer.parseInt(args[4]);
		izGridWidth=Integer.parseInt(args[3]);
		probMutation=Integer.parseInt(args[5])/10000;	// out of 10000
		probCrossover=Integer.parseInt(args[6])/100;
		if(Integer.parseInt(args[7])>0)
			bvShowStatistics=true;
		else
			bvShowStatistics=false;

		//izNumberNets=Integer.parseInt(args[6]);
		//izPopulationPerNode=Integer.parseInt(args[7]);
	} else {
		// set defaults
		//bzDebug = false;
		usage(); System.exit(0);
	}	

	Frame aFrame = new Frame("Circuit Router:Genetic Algorithm via Ant Swarm-5 (c) F.Michael O'Brien V1.10.30");
	RouterAnimApplet3 anApplet=new RouterAnimApplet3();
	aFrame.add("Center",anApplet);
	aFrame.setSize(izMaxWidth+10,izMaxHeight+30);
	aFrame.show();	
	// must alt-tab out and back in to step past 
	// sun.awt.windows.WGraphics.drawImage(WGraphics.java:360) bug
	// when using double-buffering
	anApplet.init();
	anApplet.start();
}


/** print the expected parameters to the console */
public static void usage() {
	/** 
	-debug : turn console debugging output on, default=off
	 : select grid size
	*/
	System.out.println("\nCommand-Line Options are...\njava RouterAnimApplet " +
		"Population pixelWidth GridWidth GridHeight MutationMultiplier ReplacementPercent" + 
		" [0/1 showStatistics] [-debug]\n");}

/*
	// compute fitness
	lvFitnessTotal+=ivAdjacencyCount[ivIndividual];
	if(ivIndividual<population-1)	{
		ivIndividual++;
	} else {
		ivIndividual=0;
		bvPerformCrossover=true;

		// turn off randomization for initial individuals
		bvInitializeIndividuals=false;

		// select top 10% of population for crossover
		// find maximum

		//System.out.println("\n");
		for(int j=0;j<izCrossoverCount;j++) {
			ivMaxConnections=0;
			lastIndex=0;
			for(int i=0;i<population;i++) {
				if(ivMaxConnections<ivAdjacencyCount[i]) { 
					ivMaxConnections=ivAdjacencyCount[i];
					lastIndex=i;
					//System.out.println("i,j,ivmax,ivadj " +  (new Integer(i)).toString() + " " +
					//		(new Integer(j)).toString() + " " +
					//		(new Integer(ivMaxConnections)).toString() + " " + 
					//		(new Integer(ivAdjacencyCount[i])).toString() + " ");				  				
				} 			
			}
			ivTopIndividuals[j]=lastIndex;
			ivTopIndividualsFitness[j]=ivMaxConnections;
			ivAdjacencyCount[lastIndex]=0;

			// update best scores
			if(ivBestFitness<ivMaxConnections) {
				ivBestFitness=ivMaxConnections;
				ivBestGeneration=(int)lvGeneration;
				System.out.println("g,f,af,pf " +  (new Integer(ivBestGeneration)).toString() + " " +
					(new Integer(ivBestFitness)).toString() + " " + 
					(new Integer((int)(Math.floor(lvFitnessTotal/population+.5)))).toString() + " " +
					(new Integer((int)(Math.floor(100*ivBestFitness/izMaxAdjacency+0.5)))).toString()
					);

				// output to disk for non-applets
				if(!bvApplet) {
					try {
						// create output stream
						outFile = new DataOutputStream(new FileOutputStream("out.txt",true));
						//FileOutputStream fileOut = new FileOutputStream("out.txt",true); // append
						// create a Writer to convert String's to byte[]'s
						//outFile.writeBytes((int)Byte.valueOf(aString.substring(z+1,z+2)));
						outFile.writeBytes(
							(new Integer(ivBestGeneration)).toString() + "\t" +
							(new Integer(ivBestFitness)).toString() + "\t" +
							(new Integer((int)Math.floor(100*ivBestFitness/izMaxAdjacency+0.5))).toString() + "\t" +
							(new Integer((int)(Math.floor(lvFitnessTotal/population+.5)))).toString() + "\t" +
							"\r\n");
						outFile.close();
					} catch (Exception e) {
						String err = e.toString();
						System.out.println(err);
					}
				}
			}
		}

		// perform crossover
		lvGeneration++;
		if(bvPerformCrossover==true) {
			bvPerformCrossover=false;

		// copy parents
		// copy parents out of grid before copying to top of main grid 05 oct 1400
		for(int c=0;c<izCrossoverCount;c++) 
			for(int i=0;i<izGridWidth;i++)
				for(int j=0;j<izGridHeight;j++) 
					ivBoardGenomeParents[i][j][c]=ivBoardGenome[i][j][ivTopIndividuals[c]];

		// mate top x% to create other 100-x%

		// move parents to the top
		for(int c=0;c<izCrossoverCount;c++) 
			for(int i=0;i<izGridWidth;i++)
				for(int j=0;j<izGridHeight;j++)
					ivBoardGenome[i][j][c]=ivBoardGenomeParents[i][j][c];
					//ivBoardGenome[i][j][c]=ivBoardGenome[i][j][ivTopIndividuals[c]];
				
			for(int c=izCrossoverCount;c<population;c++) {
				// get two random parents
				p1=(int)(Math.floor(Math.random()*izCrossoverCount));
				do { // no asexual reproduction thank you
					p2=(int)(Math.floor(Math.random()*izCrossoverCount)); // was *4 oct5 1330
				} while (p1==p2);

				//System.out.println("p1,p2 " +  (new Integer(p1)).toString() + " " +
				//	(new Integer(p2)).toString());
				bvSwitchOverNow=false;

				for(int i=0;i<izGridWidth;i++) {
					if(!bvSwitchOverNow)
						if((int)(Math.floor(Math.random()*100))==1)
							bvSwitchOverNow=true;  
					for(int j=0;j<izGridHeight;j++)
						//if(bvSwitchOverNow)	{
						if((int)(Math.floor(Math.random()*100))>50)	{
							ivBoardGenome[i][j][c]=ivBoardGenomeParents[i][j][p1];
						} else {
							//if(!bvSwitchOverNow)
							//	if((int)(Math.floor(Math.random()*100))==1) {
							//		bvSwitchOverNow=true;
								//System.out.println("i,j " +  (new Integer(i)).toString() + " " +
								//	(new Integer(j)).toString());													
							//	}
							ivBoardGenome[i][j][c]=ivBoardGenomeParents[i][j][p2];
						}
				}

				// perform mutations

				// modify grid
				int mutations=(int)(Math.floor(Math.random()*izMutationMultiplier));
				for(int i=0;i<mutations;i++) {				
					tempx = (int)(Math.floor(Math.random()*izGridWidth));
					tempy = (int)(Math.floor(Math.random()*izGridHeight));	
					if((int)(Math.floor(Math.random()*256))>128)
				 		if(ivBoardGenome[tempx][tempy][c]<512) {
							tempBitPos=(int)(Math.floor(Math.random()*9));
//							if((int)(Math.floor(Math.random()*256))>128) {
							ivBoardGenome[tempx][tempy][c]=(int)(Math.floor(Math.random()*512));
// 								|(1|2|4|8|16|32|64|128);
// 								&-(1|2|4|8|16|32|64|128);
					}
				}												 			
			}
			// re-initialize statistics
			lvFitnessTotal=0;
			ivFitnessAverage=0;	
		} else {

		}	
	}
	
	// 
//	for(int i=0;i<izCrossoverCount;i++) {
//		gContext.drawString(" " + (new Integer(ivTopIndividualsFitness[i])).toString(),240+30*i,izMaxHeight-16);
//	}

  	//try {Thread.sleep( izSleepTime );}
	//catch ( InterruptedException e ){showStatus(e.toString());}
	repaint();  // display buffered image
   	} // paint()
*/

/*
private void loadBoardExample1(int x, int y) {
	int lNodeCount=0;
	Ant3 anAnt;
 
 	// initialize node data structures
	izPopulationPerNode=4;
	izNumberNets=3;
	izNumberNodes=8;
	izTotalNumberNodes=izNumberNodes*izPopulationPerNode;
  	izNetNodeCount = new int[izNumberNets];
	izNetNodeX = new int[izNumberNets][izTotalNumberNodes];
	izNetNodeY = new int[izNumberNets][izTotalNumberNodes];
	// initialize Ant objects
	antSwarms = new Ant3[population][izTotalNumberNodes];
	for(int i=0;i<population;i++)
		for(int j=0;j<izNumberNodes;j++)
			for(int k=0;k<izPopulationPerNode;k++)
			antSwarms[i][j*izPopulationPerNode+k]=new Ant3(j*izPopulationPerNode+k);
		//System.out.println("_loadBoardExample: " + (new Integer(x)).toString() + "," + (new Integer(y)).toString());

	for(int k=0;k<izPopulationPerNode;k++) {
	// net 1
	izNetNodeCount[0]=2;
	izNetNodeX[0][k]=2; izNetNodeY[0][k]=2;
	for(int p=0;p<population;p++) {
		antSwarms[p][lNodeCount].setPosx(2);
		antSwarms[p][lNodeCount].setPosy(2);
	}
	lNodeCount++;

	izNetNodeX[0][k+1]=6; izNetNodeY[0][k+1]=6;
	for(int p=0;p<population;p++) {
		antSwarms[p][lNodeCount].setPosx(6);
		antSwarms[p][lNodeCount].setPosy(6);
	}
	lNodeCount++;

	// net 2
	izNetNodeCount[1]=3;
	izNetNodeX[1][k]=2; izNetNodeY[1][k]=4;
	for(int p=0;p<population;p++) {
		antSwarms[p][lNodeCount].setPosx(2);
		antSwarms[p][lNodeCount].setPosy(4);
	}
	lNodeCount++;

	izNetNodeX[1][k+1]=4; izNetNodeY[1][k+1]=3;
	for(int p=0;p<population;p++) {
		antSwarms[p][lNodeCount].setPosx(4);
		antSwarms[p][lNodeCount].setPosy(3);
	}
	lNodeCount++;

	izNetNodeX[1][k+2]=6; izNetNodeY[1][k+2]=7;
	for(int p=0;p<population;p++) {
		antSwarms[p][lNodeCount].setPosx(6);
		antSwarms[p][lNodeCount].setPosy(7);
	}
	lNodeCount++;


	// net 3
	izNetNodeCount[2]=3;
	izNetNodeX[2][k]=12; izNetNodeY[2][k]=15;
	for(int p=0;p<population;p++) {
		antSwarms[p][lNodeCount].setPosx(12);
		antSwarms[p][lNodeCount].setPosy(15);
	}
	lNodeCount++;

	izNetNodeX[2][k+1]=14; izNetNodeY[2][k+1]=14;
	for(int p=0;p<population;p++) {
		antSwarms[p][lNodeCount].setPosx(14);
		antSwarms[p][lNodeCount].setPosy(14);
	}
	lNodeCount++;

	izNetNodeX[2][k+2]=16; izNetNodeY[2][k+2]=18;
	for(int p=0;p<population;p++) {
		antSwarms[p][lNodeCount].setPosx(16);
		antSwarms[p][lNodeCount].setPosy(18);
	}
	lNodeCount++;
	}

	//topAntSwarms = new Ant3[population][izTotalNumberNodes];
	for(int p=0;p<population;p++) {	
		lNodeCount=0;		
		for(int n=0;n<izNumberNets;n++) {
			for(int i=0;i<izNetNodeCount[n];i++) {
				for(int k=0;k<izPopulationPerNode;k++) {
				anAnt=antSwarms[p][lNodeCount];
				// set src, dest nodes
				ivBoard[anAnt.getPosx()][anAnt.getPosy()]=256;
				if(i>0) {
					anAnt.setDestx(izNetNodeX[n][0]);
					anAnt.setDesty(izNetNodeY[n][0]);
				} else {
					anAnt.setDestx(izNetNodeX[n][izNetNodeCount[n]-1]);
					anAnt.setDesty(izNetNodeY[n][izNetNodeCount[n]-1]);
				}					
				// set sequential properties
				anAnt.setNetId(n);
				anAnt.setLayer(0);
				anAnt.setNodeId(lNodeCount);
				//System.out.println((new Integer(lNodeCount)).toString());
				lNodeCount++;
				}
			}
		}
	}

	for(int i=0;i<izGridHeight;i++) {
		ivBoard[0][i]=512;
		ivBoard[izGridWidth-1][i]=512;
	}
	for(int i=0;i<izGridWidth;i++) {
		ivBoard[i][0]=512;
		ivBoard[i][izGridHeight-1]=512;
	}
  
		
		ivBoard[3+x][2+y]=512;
 		ivBoard[3+x][3+y]=512;
 		ivBoard[3+x][4+y]=512;
		ivBoard[6+x][2+y]=512;
 		ivBoard[6+x][3+y]=512;
 		ivBoard[6+x][4+y]=512;

		// default selectedAnt
		selectedAnt=antSwarms[0][0];
}

*/

 }


