package org.cloudbus.cloudsim.power;

import org.apache.commons.math3.util.MathUtils;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.util.MathUtil;

import java.util.List;

/**
 * ClassName PowerVmSelectionPolicyMyNewSelectionThird
 * Description
 * Create by HW
 * Date 2021/7/15 9:58
 */
public class PowerVmSelectionPolicyMyNewSelectionThird extends PowerVmSelectionPolicy {
    @Override
    public Vm getVmToMigrate(PowerHost host) {
        List<PowerVm> migratableVms = getMigratableVms(host);
        if (migratableVms.isEmpty()) {
            return null;
        }
        Vm vmToMigrate = null;
        double minMetric = Double.MAX_VALUE;
        for(PowerVm vm:migratableVms){
            if (vm.isInMigration()){
                continue;
            }
            double[] doubles = MathUtil.listToArray(vm.getUtilizationHistory());
            double metric= MathUtil.mean(vm.getUtilizationHistory())*vm.getCurrentRequestedRam()/vm.getHost().getAvailbleBw();
            double value=0.3*vm.getCurrentUtilizationOfCpu()+0.7*vm.getCurrentRequestedRam()/vm.getRam();
            if (value < minMetric){
                minMetric=value;
                vmToMigrate=vm;
            }
        }
//        for (Vm vm : migratableVms) {
//            if (vm.isInMigration()) {
//                continue;
//            }
//            double metrix1=vm.getRam()/host.getAvailbleBw()*(1-vm.getCurrentUtilizationOfCpu()/host.getUtilizationOfCpuMips());
//            if (metrix1 < minMetric) {
//                minMetric = metrix1;
//                vmToMigrate = vm;
//            }
//        }
        return vmToMigrate;
    }
}
