// 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:   Bayes.java

package lt.ktu.gmj.methods;

import lt.ktu.gmj.*;

// Referenced classes of package lt.ktu.gmj.methods:
//			IPMethodBase, LPPoint, MethodBase, RandomPoint

public class Bayes extends IPMethodBase
{

	private static final int NOT_SEC_POINT = 0;
	private static final int SEC_POINT = 1;
	private static final int PAR1 = 10;
	private static final int PAR2 = 50;
	private static final double VAL = 9.9999999999999995E-07D;
	private static final double COEF = 1E-14D;
	private static final double RMAX = 8.9884656743115785E+307D;
	private Point x2;
	private Point mz;
	private int ix;

	public Bayes()
	{
		super.iterations = 100;
		super.initialPoints = 5;
	}

	private double fiap1(Point point, ResultLogger resultlogger, double d, Result result, int i)
	{
		double d1 = resultlogger.logAt(0).value.fx();
		double d2 = result.value.fx();
		double d3 = d2 - Math.max(9.9999999999999995E-07D, 1E-14D * Math.abs(d2));
		double d4 = Math.min(8.9884656743115785E+307D, 8.9884656743115785E+307D / (d1 - d3));
		for(int j = 0; j < i; j++)
		{
			Result result1 = resultlogger.logAt(j);
			double d5 = point.distance(result1.point);
			double d6 = d5 * d5;
			double d7 = result1.value.fx() - d3;
			if(d6 < Math.min(8.9884656743115785E+307D, d7 * d4))
			{
				d4 = d6 / d7;
				if(d4 <= -d)
					return -d4;
			}
		}

		return -d4;
	}

	private Point mig2f2(ResultLogger resultlogger, Task task, Result result, int i)
	{
		Point point = null;
		Domain domain = task.domain();
		int j = Math.min(50, Math.max(10, i)) * domain.dimensions().length;
		double d = 0.0D;
		double d1 = 0.0D;
		if(ix == 0)
		{
			x2 = new RandomPoint(domain);
			j--;
			ix = 1;
		}
		double d2 = fiap1(x2, resultlogger, d, result, i);
		for(int k = 0; k < j; k++)
		{
			mz = new RandomPoint(domain);
			double d3 = fiap1(mz, resultlogger, d, result, i);
			if(k == 0)
			{
				if(d3 >= d2)
				{
					point = x2;
					x2 = mz;
					d1 = d2;
					d = d2 = d3;
				} else
				{
					point = mz;
					d1 = d3;
					d = d2;
				}
			} else
			if(d3 < d2)
				if(d3 < d1)
				{
					x2 = point;
					point = mz;
					d = d2 = d1;
					d1 = d3;
				} else
				{
					x2 = mz;
					d = d2 = d3;
				}
		}

		return point;
	}

	public Result run(ResultLogger resultlogger, Task task)
	{
		Domain domain = task.domain();
		//Result result = new Result();
		Result result = null;
		ix = 0;
		for(int i = 0; i < super.initialPoints; i++)
		{
			LPPoint lppoint = new LPPoint(domain, i);
			Result result1 = new Result(i, lppoint, task.f(lppoint));
			resultlogger.log(result1);
			if(result == null || result1.value.fx() < result.value.fx())
				result = result1;
		}
		for(int j = super.initialPoints; j < super.iterations; j++)
		{
			Point point = mig2f2(resultlogger, task, result, j);
			Result result2 = new Result(j, point, task.f(point));
			resultlogger.log(result2);
			if(result == null || result2.value.fx() < result.value.fx())
				result = result2;
		}
		return result;
	}
}
