// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   TestObject.java

package drm.signal.neuro;

import drm.signal.*;
import java.util.Iterator;
import java.util.Vector;

// Referenced classes of package drm.signal.neuro:
//			NeuroValue, AbstractNeuroContainer

public class TestObject
{

	public long MAX_TEST_STEP;
	protected Vector test;
	protected Processor p;
	public AbstractNeuroContainer testObject;

	public TestObject()
	{
		MAX_TEST_STEP = 1000L;
		test = null;
		p = new Processor();
		testObject = null;
	}

	public TestObject(Vector test)
	{
		MAX_TEST_STEP = 1000L;
		this.test = null;
		p = new Processor();
		testObject = null;
		setTestVector(test);
	}

	public void setTestVector(Vector test)
	{
		this.test = test;
	}

	public Vector getTestVector()
	{
		return test;
	}

	public double test(double weights[])
	{
		double sum = 0.0D;
		testObject.setAllWeights(weights);
		for(Iterator it = test.iterator(); it.hasNext();)
		{
			testObject.setStep(0L);
			testObject.clearSignals();
			for(Iterator its = testObject.in.iterator(); its.hasNext(); ((Connector)its.next()).setChanged(true));
			Vector io[] = (Vector[])it.next();
			Iterator itt = io[0].iterator();
			for(int nr = 0; itt.hasNext(); nr++)
			{
				Signal s = new SimpleSignal(new NeuroValue(((Double)itt.next()).doubleValue()));
				testObject.setInSignal(nr, s);
			}

			p.proccess((Vector)testObject.in.clone(), MAX_TEST_STEP);
			sum += compare(testObject.out, io[1]);
		}

		return sum / (double)test.size();
	}

	private double compare(Vector out, Vector req)
	{
		double sum = 0.0D;
		for(int i = 0; i < out.size(); i++)
		{
			Signal outS = ((Connector)out.get(i)).getSignal();
			double outD = 0.0D;
			try
			{
				outD = ((NeuroValue)outS.getValue()).value;
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
			double reqD = ((Double)req.get(i)).doubleValue();
			sum += compare(outD, reqD);
		}

		return sum / (double)out.size();
	}

	private double compare(double out, double req)
	{
		double sub = req - out;
		return sub * sub;
	}
}
