import java.awt.AWTException;
import java.util.*;



public class main_program {
		public static  TreeMap<Long,Object> nodes = null;
		public static  UniformLoop uniloop =null;
		public static SeverList serverlist_head = null;
		public static TaskList tasklist_head =null;
		
	    
	    public static void main(String[] args) throws AWTException {
			  
	         Random ran = new Random();  
	         Input input = new Input();
	         uniloop = new UniformLoop();
	         AllocateTask allocatetask = null;
	        		 
	         
	         //一致环
	           
	         serverlist_head = input.input_server();
	         tasklist_head = input.input_task();
	         //打印虚拟服务器和任务信息
	         //serverlist_head.print_SeverList();
	         //tasklist_head.print_TaskList();
	        		 
	         //初始化一致环
	         uniloop.loopInit(serverlist_head);
	         nodes = uniloop.getHashnodes();
	         
	         //依据环和虚拟服务器的内存情况分配
	         allocatetask = new AllocateTask();
	         allocatetask.allocate_task(tasklist_head);
	        		 
	        
	         serverlist_head.print_task();
	         serverlist_head.print_SeverList();
	    	 System.out.println("已经完成了所有的任务");
	       
	    	 //负载均衡相对均衡率
	    	 allocat_rate();
	    	 
	   } 
	    public static void allocat_rate()
	    {
	    	int all_memory=0;
	    	int all_task=0;
	    	int host_num= 0;
	    	float differ_rate = 0;
	    	
	        SeverList serverlist_list = serverlist_head;
	        
	    	while(serverlist_list.getNext() != null)
	    	{
	    		serverlist_list = serverlist_list.getNext();
	    		all_memory  = all_memory+ serverlist_list.get_all_Memory();
	    		all_task  = all_task + serverlist_list.exec_task;
	    		host_num++;
	    	}
	    	 
	    	    serverlist_list = serverlist_head;      
		    	while(serverlist_list.getNext() != null)
		    	{
		    		serverlist_list = serverlist_list.getNext();
		    		differ_rate = differ_rate + Math.abs(( (float)serverlist_list.get_all_Memory()/all_memory ) - ( (float)serverlist_list.exec_task/all_task ));
		    	}
		    	System.out.println("相对均衡率：" + differ_rate);	
	    }

	
}
