/*
 * Title:        CloudSim Toolkit
 * Description:  CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * Copyright (c) 2009-2012, The University of Melbourne, Australia
 */

package org.cloudbus.cloudsim.power;

import java.util.*;

import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.power.lists.PowerVmList;
import org.cloudbus.cloudsim.util.MathUtil;

/**
 * A VM allocation policy that uses Inter Quartile Range (IQR)  to compute
 * a dynamic threshold in order to detect host over utilization.
 * 
 * <br/>If you are using any algorithms, policies or workload included in the power package please cite
 * the following paper:<br/>
 * 
 * <ul>
 * <li><a href="http://dx.doi.org/10.1002/cpe.1867">Anton Beloglazov, and Rajkumar Buyya, "Optimal Online Deterministic Algorithms and Adaptive
 * Heuristics for Energy and Performance Efficient Dynamic Consolidation of Virtual Machines in
 * Cloud Data Centers", Concurrency and Computation: Practice and Experience (CCPE), Volume 24,
 * Issue 13, Pages: 1397-1420, John Wiley & Sons, Ltd, New York, USA, 2012</a>
 * </ul>
 * 
 * @author Anton Beloglazov
 * @since CloudSim Toolkit 3.0
 */
public class PowerVmAllocationPolicyMigrationInterQuartileRange extends
		PowerVmAllocationPolicyMigrationAbstract {

	/** The safety parameter in percentage (at scale from 0 to 1).
         * It is a tuning parameter used by the allocation policy to 
         * estimate host utilization (load). The host overload detection is based
         * on this estimation.
         * This parameter is used to tune the estimation
         * to up or down. If the parameter is set as 1.2, for instance, 
         * the estimated host utilization is increased in 20%, giving
         * the host a safety margin of 20% to grow its usage in order to try
         * avoiding SLA violations. As this parameter decreases, more
         * aggressive will be the consolidation (packing) of VMs inside a host,
         * what may lead to optimization of resource usage, but rising of SLA 
         * violations. Thus, the parameter has to be set in order to balance
         * such factors.
         */
	private double safetyParameter = 0;

	/** The fallback VM allocation policy to be used when
         * the IQR over utilization host detection doesn't have
         * data to be computed.
	 * 		fallback:n 应急计划，应变计划，减少，削减，储备物
	 * */
	private PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy;

	/**
	 * Instantiates a new PowerVmAllocationPolicyMigrationInterQuartileRange.
	 * 
	 * @param hostList the host list
	 * @param vmSelectionPolicy the vm selection policy
	 * @param safetyParameter the safety parameter
	 * @param utilizationThreshold the utilization threshold
	 */
	/*public PowerVmAllocationPolicyMigrationInterQuartileRange(
			List<? extends Host> hostList,
			PowerVmSelectionPolicy vmSelectionPolicy,
			double safetyParameter,
			PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy,
			double utilizationThreshold) {
		super(hostList, vmSelectionPolicy);
		setSafetyParameter(safetyParameter);
		setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy);
	}*/
	/**
	 * Instantiates a new PowerVmAllocationPolicyMigrationInterQuartileRange.
	 * 
	 * @param hostList the host list
	 * @param vmSelectionPolicy the vm selection policy
	 * @param safetyParameter the safety parameter
	 */
	public PowerVmAllocationPolicyMigrationInterQuartileRange(
			List<? extends Host> hostList,
			PowerVmSelectionPolicy vmSelectionPolicy,
			double safetyParameter,
			PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy) {
		super(hostList, vmSelectionPolicy);
		setSafetyParameter(safetyParameter);
		setFallbackVmAllocationPolicy(fallbackVmAllocationPolicy);
	}

	/**
	 * Checks if the host is over utilized, based on CPU utilization.
	 * 
	 * @param host the host
	 * @return true, if the host is over utilized; false otherwise
	 */
	@Override
	protected boolean isHostOverUtilized(PowerHost host) {
		PowerHostUtilizationHistory _host = (PowerHostUtilizationHistory) host;
		double upperThreshold = 0;
		try {
			//此处改过
			upperThreshold = 0.9 - getSafetyParameter() * getHostUtilizationIqr(_host);
		} catch (IllegalArgumentException e) {
			return getFallbackVmAllocationPolicy().isHostOverUtilized(host);
		}
		addHistoryEntry(host, upperThreshold);
		double totalRequestedMips = 0;
		for (Vm vm : host.getVmList()) {
			totalRequestedMips += vm.getCurrentRequestedTotalMips();
		}
		double utilization = totalRequestedMips / host.getTotalMips();
		return utilization > upperThreshold;
	}

	/**
	 * Gets the host CPU utilization percentage IQR.
	 * 
	 * @param host the host
	 * @return the host CPU utilization percentage IQR
	 */

	protected double getHostUtilizationIqr(PowerHostUtilizationHistory host) throws IllegalArgumentException {
		double[] data = host.getUtilizationHistory();
		for (int i = 0; i < data.length; i++) {
			//System.out.println("该host #" + (i + 1) + "的历史利用率为：" + data[i]);
		}
		if (MathUtil.countNonZeroBeginning(data) >= 12) { // 12 has been suggested as a safe value
			return MathUtil.iqr(data);
		}
		throw new IllegalArgumentException();
	}

	/*@Override
	protected List<Map<String, Object>> getNewVmPlacement(List<? extends Vm> vmsToMigrate, Set<? extends Host> excludedHosts) {
		List<Map<String, Object>> migrationMap = new LinkedList<Map<String, Object>>();
		//获取整个过程中的主机列表
		//List<PowerHost> allhostList = this.<PowerHost>getHostList();
		//关闭的主机
		List<PowerHost> switchedOffHosts = this.<PowerHost>getSwitchedOffHosts();
		// ArrayList<? extends Host> hostlist = new ArrayList<>(excludedHosts);
		PowerVmList.sortByCpuUtilization(vmsToMigrate);
		for (Vm vm : vmsToMigrate) {
			double minProfomance=Double.MIN_VALUE;
			double minPower = Double.MAX_VALUE;
			Host allocatedHost = null;
			for (Host host : excludedHosts) {
//               if ( isHostUnderUtilized((PowerHost) host)) {
//                   continue;
//                }
//               else{
				double dertaUtilizaiton  = //host.getTotalMips()-vm.getCurrentRequestedTotalMips();
						1 - ((PowerHost) host).getUtilizationOfCpu() - (vm.getCurrentUtilizationOfCpu() * vm.getMips()) / host.getTotalMips();
				// System.out.println("利用率之差为："+dertaUtilizaiton);
				//然后计算相关的关于主机的消耗模型，就是得获得相关主机的消耗模型
				if (dertaUtilizaiton > 0){
//                   System.out.println("该主机的利用率差为"+(host.getAvailableMips()-vm.getCurrentRequestedTotalMips()));
					//  if ((host.getAvailableMips()-vm.getCurrentRequestedTotalMips()) > 0){
					if (host.isSuitableForVm(vm)) {
						double power = getPowerAfterAllocation((PowerHost) host, vm);
						double powerDiff = power - ((PowerHost) host).getPower();
						//double resouceAvaiable=Math.abs(((PowerHost) host).getUtilizationOfCpu()-host.getUtilizationOfStorge())/((PowerHost) host).getUtilizationOfCpu()+host.getUtilizationOfStorge();
						double profomance =(1-host.getUtilizaionOfCpu())* (Math.exp(-powerDiff));//e得-powerDiff次方
						if (profomance > minProfomance) {//谁大选谁
							minProfomance = power;
							allocatedHost = host;
						}
					}
				}
				//          }
				//设置所产生的利用率差值^U
				if (allocatedHost != null) {
					Log.printConcatLine("VM #", vm.getId(), " allocated to host #", allocatedHost.getId());
					Map<String, Object> migrate = new HashMap<String, Object>();
					migrate.put("vm", vm);
					migrate.put("host", allocatedHost);
					migrationMap.add(migrate);
				}
			}
			if (vm != null) {
				System.out.println("还存在虚拟机未分配完");
				minProfomance=Double.MIN_VALUE;
				for(PowerHost host: switchedOffHosts){
					if (host.isSuitableForVm(vm)){
						double power = getPowerAfterAllocation((PowerHost) host, vm);
						double powerDiff = power - ((PowerHost) host).getPower();
						//double resouceAvaiable=Math.abs(((PowerHost) host).getUtilizationOfCpu()-host.getUtilizationOfStorge())/((PowerHost) host).getUtilizationOfCpu()+host.getUtilizationOfStorge();
						double profomance =(1-host.getUtilizaionOfCpu())* (Math.exp(-powerDiff));
						if (profomance > minProfomance) {
							minProfomance = power;
							allocatedHost = host;
						}
					}
				}
				if (allocatedHost != null) {
					Log.printConcatLine("VM #", vm.getId(), " allocated to host #", allocatedHost.getId());
					Map<String, Object> migrate = new HashMap<String, Object>();
					migrate.put("vm", vm);
					migrate.put("host", allocatedHost);
					migrationMap.add(migrate);
				}
			}
		}
		return migrationMap;
	}*/




	/**
	 * Sets the safety parameter.
	 * 
	 * @param safetyParameter the new safety parameter
	 */
	protected void setSafetyParameter(double safetyParameter) {
		if (safetyParameter < 0) {
			Log.printConcatLine("The safety parameter cannot be less than zero. The passed value is: ",
					safetyParameter);
			System.exit(0);
		}
		this.safetyParameter = safetyParameter;
	}

	/**
	 * Gets the safety parameter.
	 * 
	 * @return the safety parameter
	 */
	protected double getSafetyParameter() {
		return safetyParameter;
	}

	/**
	 * Sets the fallback vm allocation policy.
	 * 
	 * @param fallbackVmAllocationPolicy the new fallback vm allocation policy
	 */
	public void setFallbackVmAllocationPolicy(
			PowerVmAllocationPolicyMigrationAbstract fallbackVmAllocationPolicy) {
		this.fallbackVmAllocationPolicy = fallbackVmAllocationPolicy;
	}

	/**
	 * Gets the fallback vm allocation policy.
	 * 
	 * @return the fallback vm allocation policy
	 */
	public PowerVmAllocationPolicyMigrationAbstract getFallbackVmAllocationPolicy() {
		return fallbackVmAllocationPolicy;
	}

}
