package ps6;
/*
 * Created on Apr 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
/**
 * @author knorton
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Prob01 {

	public static double distance(Message m0, Message m1) {
		double d0 = m0.getP(0) - m1.getP(0);
		double d1 = m0.getP(1) - m1.getP(1);
		return Math.sqrt(d0*d0 + d1*d1);
	}
	
	public static void main(String args[]) {
		Graph g = new Graph();

		
		SoftEquals eg = g.createSoftEquals();
		Inverter x = g.createInverter(eg);
		x.doDampening();
		
		eg.addInputGate(g.createEvidence(new Message(0.9,0.1)));		
		eg.addInputGate(x);
		
		Probe p = g.createProbe("X",x);
		//g.createProbe("Z",eg);
		
		Message pm = null;
		for (int i=0;i<1000;i++) {
			g.step();
			
			//check for convergence
			if ((pm != null) && (distance(p.getValue(),pm) < 1e-3)) {
				System.out.println(i);
				break;
			}
			pm = p.getValue();
		}
	}
}
