
public class EvaluateCosts {

	/*
	 * Define here the time units each level takes
	 */	
	public static double n1 =4; 
	public static double  n2 =8; 
	public static double  n3 =12; 
	public static double  n4 =20;
	
	/*
	 * Define here the probability for each level with which you pass a single time
	 * unit
	 */
	public static double  p1 =0.95; 
	public static double  p2 =0.98; 
	public static double  p3 =0.99; 
	
	/* Define here how much time you have available */
	static double t =50; 
	
	/* Define here how many times you want to run the function */
	static int numberOfTimes = 10000; 
	
	public static double y; 
	static double cost; 
	static double random; 
	public static boolean goal = false;
	static int counts = 0; 

	/*
	 * The main function calls the function 'cost1' for the defined number of times
	 * and calculates the percentage of times it returned a positive result
	 */
	
	public static void main(String[] args) 
	{
	double runs = numberOfTimes;
	while(numberOfTimes>0)	
	{
	cost= cost1(t);
	if(goal) 
	{
		counts=counts+1;
	}
	System.out.println("Cost: "+cost);
	System.out.print("Goal: "+goal+"\r\n"+"\r\n");
	goal=false;
	numberOfTimes--;
	}
	System.out.print("PercentageOfWin: "+(counts/runs)*100+" % "+"\r\n");
	} 

	/*
	 * Level 1: When each time unit has a positive result successively, the
	 * function passes the time (costs) it had so far and calls function 'cost2'
	 * (Level 2) Should the time units needed exceed the provisioned time
	 * available defined in 't', the function gives out a negative result along with
	 * the time (costs) consumed altogether
	 */	
	
	public static double cost1(double t){ 
	t= t-n1; 
	System.out.println("t1:"+t); 
	if(t<0||goal) 
	{ 
	System.out.print("Remaining time:  "+t+"\r\n"); 
	return y; 
	} 
	else 
	y= (n1) + cost2(t); 
	return y; 
	} 
	

	/*
	 * Level 2: When each time unit has a positive result successively, the function
	 * passes the time (costs) it had so far and calls function 'cost3' (rises to
	 * Level 3). Each time a time unit has a negative result, the function passes
	 * the time (costs) it had so far and calls function 'cost1' again (falls back
	 * to Level 1). Should the time units needed exceed the provisioned time
	 * available defined in 't', the function gives out a negative result along with
	 * the time (costs) consumed altogether
	 */
	
	public static double cost2(double t){ 
	t= t-n2; 
	System.out.println("t2:"+t); 
	if(t<0||goal) 
	{ 
	System.out.print("Remaining time: "+t+"\r\n");
	return y; 
	} 
	else 
	{
	random = Math.random(); 
	if(random < Math.pow(p1,n2))
	{ 
	y= n2+cost3(t); 
	return y; 
	} 
	else 
	{ 
	y= n2/2+(cost1(t+(n2/2))); 
	return y; 
	} 
	} 
	} 
	 
	
	/*
	 * Level 3: When each time unit has a positive result successively, the function
	 * passes the time (costs) it had so far and calls function 'cost4' (rises to
	 * Level 4) Each time a time unit has a negative result, the function passes the
	 * time (costs) it had so far and calls function 'cost2' again (falls back to
	 * Level 2). Should the time units needed exceed the provisioned time available
	 * defined in 't', the function gives out a negative result along with the time
	 * (costs) consumed altogether
	 */
	
	public static double cost3(double t){ 
	t= t-n3; 
	System.out.println("t3:"+t); 
	if(t<0||goal) 
	{ 
	System.out.print("Remaining time: "+t+"\r\n");
	return y; 
	} 
	else 
	{
	random = Math.random(); 
	if(random < Math.pow(p2,n3))
	{ 
	y= n3+cost4(t); 
	return y; 
	} 
	else 
	{ 
	y= n3/2+(cost2(t+(n3/2))); 
	return y; 
	} 
	} 
	} 
	 

	/*
	 * Level 4: When each time unit has a positive result successively, the function
	 * gives out a positive result together with the time (costs) it consumed
	 * altogether Each time a time unit has a negative result, the function passes
	 * the time (costs) it had so far and calls function 'cost3' again (falls back
	 * to Level 3). Should the time units needed exceed the provisioned time
	 * available defined in 't', the function gives out a negative result along with
	 * the time (costs) consumed altogether
	 */
	
	public static double cost4(double t){ 
	t=t-n4; 
	System.out.println("t4:"+t); 
	if(t<0) 
	{
	System.out.print("Remaining time: "+t+"\r\n");
	return y; 
	} 
	else 
	{
	random = Math.random(); 
	if(random < Math.pow(p3,n4))
	{ 
	y= n4; 
	goal=true; 
	System.out.print("Remaining time:  "+t+"\r\n"); 
	return y; 
	} 
	else 
	{ 
	y= n4/2+(cost3(t+(n4/2))); 
	return y ; 
	} 
	} 
	} 
	 
}
