Browse Source

使用java完成结果测试,平均得分7947

liuyuqi-dellpc 6 years ago
parent
commit
646bdb1514

+ 5 - 0
.gitignore

@@ -70,3 +70,8 @@ target/
 /dataset
 /dataset
 /.Rproj.user
 /.Rproj.user
 /.settings
 /.settings
+/res
+/resb
+/.Rhistory
+/.pydevproject
+/*.Rproj

+ 1 - 0
code/main.py

@@ -279,6 +279,7 @@ if __name__ == '__main__':
     scheduling = Scheduling()
     scheduling = Scheduling()
     # 加载数据
     # 加载数据
     scheduling.loadData()
     scheduling.loadData()
+    print("加载数据耗时:", time.time() - start)
     # 开始调度
     # 开始调度
     scheduling.run(start)
     scheduling.run(start)
     # 部署完事
     # 部署完事

+ 35 - 36
java/com/aliyun/tianchi/mgr/evaluate/evaluate/file/evaluator/AlibabaSchedulerEvaluatorRun.java

@@ -1,23 +1,28 @@
 package com.aliyun.tianchi.mgr.evaluate.evaluate.file.evaluator;
 package com.aliyun.tianchi.mgr.evaluate.evaluate.file.evaluator;
 
 
 
 
-import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
+import java.util.List;
+import java.util.ArrayList;
 import java.io.File;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.FileReader;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+import java.io.BufferedReader;
+
+import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.InputStreamReader;
-import java.util.ArrayList;
-import java.util.HashMap;
+import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.IOException;
 import java.util.List;
 import java.util.List;
+import java.util.ArrayList;
 import java.util.Map;
 import java.util.Map;
-
-import org.apache.commons.lang3.tuple.ImmutablePair;
-import org.apache.commons.lang3.tuple.Pair;
-
+import java.util.HashMap;
 import com.google.common.base.Charsets;
 import com.google.common.base.Charsets;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.lang3.tuple.ImmutablePair;
 
 
 /**
 /**
  * Created by mou.sunm on 2018/07/02.
  * Created by mou.sunm on 2018/07/02.
@@ -40,10 +45,9 @@ public class AlibabaSchedulerEvaluatorRun {
     private String[]                apps;
     private String[]                apps;
     private String[]                machines;
     private String[]                machines;
     private Map<String, Integer>    inst2AppIndex;
     private Map<String, Integer>    inst2AppIndex;
-    
-    private double[][]              appResources;//app  
-    private double[][]              machineResources;//主机  
-    private Map<Integer, Integer>[] appInterference;//限制条件
+    private double[][]              appResources;  
+    private double[][]              machineResources;  
+    private Map<Integer, Integer>[] appInterference;
     
     
     // 动态数据
     // 动态数据
     private Map<String, Integer>       inst2Machine;
     private Map<String, Integer>       inst2Machine;
@@ -53,7 +57,7 @@ public class AlibabaSchedulerEvaluatorRun {
     protected double evaluate(BufferedReader bufferedReader) throws IOException {
     protected double evaluate(BufferedReader bufferedReader) throws IOException {
         double costs = 0.;
         double costs = 0.;
         try {
         try {
-            /** 读取执行数据 execs:[(inst_65379,5999), (inst_62243,5998)] */
+            /** 读取执行数据 */
             List<Pair<String, Integer>> execs = new ArrayList<Pair<String, Integer>>();
             List<Pair<String, Integer>> execs = new ArrayList<Pair<String, Integer>>();
             for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
             for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
                 String[] pair = line.split(",", -1);
                 String[] pair = line.split(",", -1);
@@ -70,8 +74,8 @@ public class AlibabaSchedulerEvaluatorRun {
                     //System.out.println("超过EXECUTION LIMIT(" + EXEC_LIMIT+"), 执行中断");
                     //System.out.println("超过EXECUTION LIMIT(" + EXEC_LIMIT+"), 执行中断");
                     //break;
                     //break;
                 //}
                 //}
-                String  inst        = exec.getLeft();//key:inst_65379
-                Integer machineIt   = exec.getRight();//value:5999
+                String  inst        = exec.getLeft();
+                Integer machineIt   = exec.getRight();
                 pickInstance(inst); // 先将inst从当前所属的machine删除
                 pickInstance(inst); // 先将inst从当前所属的machine删除
                 String msg = toMachine(inst, machineIt);
                 String msg = toMachine(inst, machineIt);
                 if (!msg.equals("success")) {
                 if (!msg.equals("success")) {
@@ -130,42 +134,42 @@ public class AlibabaSchedulerEvaluatorRun {
             judge framework 
             judge framework 
         */
         */
         /** cpuIter */
         /** cpuIter */
-        cpuIter = new ArrayList<Integer>();//1,2,3....98
+        cpuIter = new ArrayList<Integer>();
         for (int i = 0; i < T; i++)
         for (int i = 0; i < T; i++)
             cpuIter.add(i);
             cpuIter.add(i);
         /** Read app_resources */
         /** Read app_resources */
-        n = Integer.parseInt(bufferedReader.readLine());//9338
+        n = Integer.parseInt(bufferedReader.readLine());
         apps = new String[n];
         apps = new String[n];
-        for (int i = 0; i < n; i++) {//循环app表每一行
+        for (int i = 0; i < n; i++) {
             // appId,resources
             // appId,resources
             String line = bufferedReader.readLine();
             String line = bufferedReader.readLine();
             String[] parts = line.split(",", -1);
             String[] parts = line.split(",", -1);
             List<Double> resources = new ArrayList<Double>();
             List<Double> resources = new ArrayList<Double>();
-            for (String x : parts[1].split("\\|", -1))//cpu
+            for (String x : parts[1].split("\\|", -1))
                 resources.add(Double.parseDouble(x));
                 resources.add(Double.parseDouble(x));
-            for (String x : parts[2].split("\\|", -1))//mem
+            for (String x : parts[2].split("\\|", -1))
                 resources.add(Double.parseDouble(x));
                 resources.add(Double.parseDouble(x));
-            for (int j = 3; j < parts.length; j++) //disk/P/M/PM
+            for (int j = 3; j < parts.length; j++)
                 resources.add(Double.parseDouble(parts[j]));
                 resources.add(Double.parseDouble(parts[j]));
             if (i == 0) {
             if (i == 0) {
-                k = resources.size();//200
+                k = resources.size();
                 appIndex = new HashMap<String, Integer>();
                 appIndex = new HashMap<String, Integer>();
-                appResources = new double[n][k];//9338*200
+                appResources = new double[n][k];
             }
             }
             if (k != resources.size()) 
             if (k != resources.size()) 
                 throw new IOException("[DEBUG 2]Invaild problem");
                 throw new IOException("[DEBUG 2]Invaild problem");
             if (appIndex.containsKey(parts[0]))
             if (appIndex.containsKey(parts[0]))
                 throw new IOException("[DEBUG 3]Invaild problem");
                 throw new IOException("[DEBUG 3]Invaild problem");
-            appIndex.put(parts[0], i+1);//{app_5269=5268, app_5267=5266, app_6598=6597}
-            apps[i] = parts[0];//appid [app_1, app_2, app_3, app_4]
+            appIndex.put(parts[0], i);
+            apps[i] = parts[0];
             for (int j = 0; j < k; j++)
             for (int j = 0; j < k; j++)
                 appResources[i][j] = resources.get(j);
                 appResources[i][j] = resources.get(j);
         }
         }
         /** Read machine_resources*/
         /** Read machine_resources*/
-        m = Integer.parseInt(bufferedReader.readLine());//6000
+        m = Integer.parseInt(bufferedReader.readLine());
         machineResources = new double[m][k];
         machineResources = new double[m][k];
         machineResourcesUsed = new double[m][k];
         machineResourcesUsed = new double[m][k];
-        machineIndex = new HashMap<String, Integer>();//{machine_3791=3790, machine_3792=3791}
+        machineIndex = new HashMap<String, Integer>();
         machineHasApp = new Map[m];
         machineHasApp = new Map[m];
         machines = new String[m];
         machines = new String[m];
         for (int i = 0; i < m; i++) {
         for (int i = 0; i < m; i++) {
@@ -174,7 +178,7 @@ public class AlibabaSchedulerEvaluatorRun {
             String[] parts = line.split(",", -1);
             String[] parts = line.split(",", -1);
             if (machineIndex.containsKey(parts[0]))
             if (machineIndex.containsKey(parts[0]))
                 throw new IOException("[DEBUG 4]Invaild problem");
                 throw new IOException("[DEBUG 4]Invaild problem");
-            machineIndex.put(parts[0], i+1);
+            machineIndex.put(parts[0], i);
             machines[i] = parts[0];
             machines[i] = parts[0];
             machineHasApp[i] = new HashMap<Integer, Integer>();
             machineHasApp[i] = new HashMap<Integer, Integer>();
             double cpu = Double.parseDouble(parts[1]);
             double cpu = Double.parseDouble(parts[1]);
@@ -189,7 +193,7 @@ public class AlibabaSchedulerEvaluatorRun {
                 machineResourcesUsed[i][j] = 0.;
                 machineResourcesUsed[i][j] = 0.;
         }
         }
         /** Read instance_deploy */
         /** Read instance_deploy */
-        N = Integer.parseInt(bufferedReader.readLine());//68219
+        N = Integer.parseInt(bufferedReader.readLine());
         inst2AppIndex = new HashMap<String, Integer>();
         inst2AppIndex = new HashMap<String, Integer>();
         inst2Machine  = new HashMap<String, Integer>();
         inst2Machine  = new HashMap<String, Integer>();
         for (int i = 0; i < N; i++) {
         for (int i = 0; i < N; i++) {
@@ -207,7 +211,7 @@ public class AlibabaSchedulerEvaluatorRun {
             }
             }
         }
         }
         /** Read app_interference */
         /** Read app_interference */
-        int icnt = Integer.parseInt(bufferedReader.readLine());//35242
+        int icnt = Integer.parseInt(bufferedReader.readLine());
         appInterference = new Map[n];
         appInterference = new Map[n];
         for (int i = 0; i < n; i++)
         for (int i = 0; i < n; i++)
             appInterference[i] = new HashMap<Integer, Integer>();
             appInterference[i] = new HashMap<Integer, Integer>();
@@ -305,11 +309,6 @@ public class AlibabaSchedulerEvaluatorRun {
         
         
         InputStream problem;
         InputStream problem;
         InputStream result;
         InputStream result;
-//        app_resources.csv 
-//        machine_resources.csv 
-//        instance_deploy.csv 
-//        app_interference.csv 
-//        result.csv
         if (args.length == 5) {
         if (args.length == 5) {
             // 将赛题拼成评测数据
             // 将赛题拼成评测数据
             StringBuffer sb = new StringBuffer();
             StringBuffer sb = new StringBuffer();

+ 145 - 75
java/me/yoqi/servermanager/Main.java

@@ -1,10 +1,12 @@
 package me.yoqi.servermanager;
 package me.yoqi.servermanager;
 
 
 import java.io.BufferedReader;
 import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.InputStreamReader;
@@ -16,7 +18,7 @@ import java.util.Map;
 import com.google.common.base.Charsets;
 import com.google.common.base.Charsets;
 
 
 /**
 /**
- * Created by mou.sunm on 2018/07/02.
+ * 不考虑迁移代价而不断迁移,跑b数据9746。 Created by liuyuqi.gov@msn.cn on 2018/07/02.
  */
  */
 public class Main {
 public class Main {
 	// 参数
 	// 参数
@@ -30,41 +32,67 @@ public class Main {
 	private int num_inst; // inst数 68219
 	private int num_inst; // inst数 68219
 	private int num_mac; // machine数 6000
 	private int num_mac; // machine数 6000
 	private int num_k; // 资源种类 200
 	private int num_k; // 资源种类 200
-	private List<Integer> cpuIter; //[0, 1, 2, ... 97]
-	private Map<String, Integer> appIndex; //app_1,app_2字符串用0,1等数字替换,{app_1=0, app_2=1,...,app_4=3}
-	private Map<String, Integer> machineIndex;//{machine_1=0, machine_2=1,
-	private String[] apps; //[app_1, app_2, app_3, app_4, 
-	private String[] machines;//[machine_1, machine_2, machine_3, machine_4,
-	private Map<String, Integer> inst2AppIndex;//{inst_157=49}
+	private List<Integer> cpuIter; // [0, 1, 2, ... 97]
+	private Map<String, Integer> appIndex; // app_1,app_2字符串用0,1等数字替换,{app_1=0,
+											// app_2=1,...,app_4=3}
+	private Map<String, Integer> machineIndex;// {machine_1=0, machine_2=1,
+	private List<Integer> machinePriorIndex = new ArrayList<Integer>();
+	private String[] apps; // [app_1, app_2, app_3, app_4,
+	private String[] machines;// [machine_1, machine_2, machine_3, machine_4,
+	private Map<String, Integer> inst2AppIndex;// {inst_157=49}
 
 
 	private double[][] appResources;// app 9338*200
 	private double[][] appResources;// app 9338*200
 	private double[][] machineResources;// 主机 6000*200
 	private double[][] machineResources;// 主机 6000*200
-	private Map<Integer, Integer>[] appInterference;//9338 限制条件[{}, {}, {3517=2}, {}, {5600=1, 6747=2, 7707=2, 4830=2},
+	private Map<Integer, Integer>[] appInterference;// 9338 限制条件[{}, {},
+													// {3517=2}, {}, {5600=1,
+													// 6747=2, 7707=2, 4830=2},
 
 
 	// 动态数据
 	// 动态数据
-	private Map<String, Integer> inst2Machine;// 部署 {inst_33717=5456, inst_33713=3427, inst_33716=3983,
-	private Map<String, Integer> inst2MachineRemine;
-	private double[][] machineResourcesUsed;//6000*200
-	private Map<Integer, Integer>[] machineHasApp;//6000 [{}, {},{6004=1, 9126=1, 1598=1}, {}, {}, {},
+	private Map<String, Integer> inst2Machine;// 部署 {inst_33717=5456,
+	// 这个数据类型和Map<String, Integer>有点区别,加上list会保存顺序
+	private List<String> deployResult = new ArrayList<String>(); // [inst_33717,5456
+	private List<String> instUnDelpoy = new ArrayList<String>();// 未部署的实例
+	private List<String> instSortByDisk = new ArrayList<String>();// 未部署的实例
+	private Map<String, Integer> inst2MachineDefaultConflict = new HashMap<String, Integer>();;// 记录初始部署冲突的实例和主机
+	private double[][] machineResourcesUsed;// 6000*200
+	private Map<Integer, Integer>[] machineHasApp;// 6000 [{}, {},{6004=1,
+													// 9126=1, 1598=1}, {}, {},
+													// {},
 
 
 	/**
 	/**
 	 * 先对disk排序,然后first fit
 	 * 先对disk排序,然后first fit
+	 * 
+	 * @throws IOException
+	 *             写文件异常
 	 */
 	 */
-	private void run() {
-		
-		while(inst2Machine.size()>0) {
-			
-		}
-	}
-	
-	private void sortInstanceByDisk(ArrayList<Integer> instance) {
-		for(int i=0;i<instance.size();i++) {
-			int appIt = inst2AppIndex.get(i);
+	private void run() throws IOException {
+		// 主机优先顺序,使用过的主机在前,目的紧凑;然后大主机在前。做一个主机优先部署序列
+		String tmp_Res;
+		String inst_tmp = null;
+		int time_count = 0;
+		long startTime=System.currentTimeMillis();
+		long cost_time = 0;
+		for (Map.Entry<String, Integer> entry : inst2AppIndex.entrySet()) {
+			// 判断是否部署过,如果部署过则重新部署
+			inst_tmp = entry.getKey();
+			if (inst2Machine.containsKey(inst_tmp)) {
+				pickInstance(entry.getKey());
+			}
+			for (int i = num_mac - 1; i >= 0; i--) {
+				tmp_Res = toMachine(inst_tmp, i);
+				if (tmp_Res == "success") {
+					deployResult.add(inst_tmp + "," + "machine_" + (i + 1));
+					break;
+				}
+			}
+			time_count++;
+			if (time_count % 5000 == 0) {
+				cost_time = System.currentTimeMillis() - startTime;
+				System.out.println("已经部署:" + time_count+"  剩余部署:" + (num_inst - time_count));
+				System.out.println("预估剩余时间:" + ((cost_time / 1000) * (num_inst - time_count) / time_count)+"s");
+			}
 		}
 		}
-	}
-	
-	private void sort(ArrayList<Integer> list) {
-		
+		saveResult(deployResult);
 	}
 	}
 
 
 	// 读取数据
 	// 读取数据
@@ -105,7 +133,6 @@ public class Main {
 		machineResources = new double[num_mac][num_k];
 		machineResources = new double[num_mac][num_k];
 		machineResourcesUsed = new double[num_mac][num_k];
 		machineResourcesUsed = new double[num_mac][num_k];
 		machineIndex = new HashMap<String, Integer>();// {machine_3791=3790,
 		machineIndex = new HashMap<String, Integer>();// {machine_3791=3790,
-														// machine_3792=3791}
 		machineHasApp = new Map[num_mac];
 		machineHasApp = new Map[num_mac];
 		machines = new String[num_mac];
 		machines = new String[num_mac];
 		for (int i = 0; i < num_mac; i++) {
 		for (int i = 0; i < num_mac; i++) {
@@ -127,7 +154,7 @@ public class Main {
 				machineResources[i][2 * T + j - 3] = Double.parseDouble(parts[j]);
 				machineResources[i][2 * T + j - 3] = Double.parseDouble(parts[j]);
 			for (int j = 0; j < num_k; j++)
 			for (int j = 0; j < num_k; j++)
 				machineResourcesUsed[i][j] = 0.;
 				machineResourcesUsed[i][j] = 0.;
-			
+
 		}
 		}
 		/** Read app_interference */
 		/** Read app_interference */
 		int icnt = Integer.parseInt(bufferedReader.readLine());// 35242
 		int icnt = Integer.parseInt(bufferedReader.readLine());// 35242
@@ -151,8 +178,10 @@ public class Main {
 		}
 		}
 		/** Read instance_deploy */
 		/** Read instance_deploy */
 		num_inst = Integer.parseInt(bufferedReader.readLine());// 68219
 		num_inst = Integer.parseInt(bufferedReader.readLine());// 68219
-		inst2AppIndex = new HashMap<String, Integer>();// 68219*2 {inst_33717=8766, inst_33718=2956}
-		inst2Machine = new HashMap<String, Integer>();// {inst_33717=5456, inst_33713=3427}
+		inst2AppIndex = new HashMap<String, Integer>();// 68219*2
+														// {inst_33717=8766,
+														// inst_33718=2956}
+		inst2Machine = new HashMap<String, Integer>();// {inst_33717=5456,
 		for (int i = 0; i < num_inst; i++) {
 		for (int i = 0; i < num_inst; i++) {
 			String line = bufferedReader.readLine();
 			String line = bufferedReader.readLine();
 			String[] parts = line.split(",", -1);
 			String[] parts = line.split(",", -1);
@@ -164,54 +193,84 @@ public class Main {
 			if (!"".equals(parts[2])) {
 			if (!"".equals(parts[2])) {
 				if (!machineIndex.containsKey(parts[2]))
 				if (!machineIndex.containsKey(parts[2]))
 					throw new IOException("[DEBUG 7]Invaild problem");
 					throw new IOException("[DEBUG 7]Invaild problem");
-				toMachine(parts[0], machineIndex.get(parts[2]), false);
+				toMachineDefault(parts[0], machineIndex.get(parts[2]));
+			} else {
+				instUnDelpoy.add(parts[0]);
 			}
 			}
 		}
 		}
-		System.out.println("finisl");
+		System.out.println("finish init");
+	}
+
+	private String toMachineDefault(String inst, int machineIt) {
+		int appIt = inst2AppIndex.get(inst);
+		Map<Integer, Integer> hasApp = machineHasApp[machineIt];
+		String res_tmp = doCheck(inst, machineIt);
+		if (res_tmp != "success") {
+			inst2MachineDefaultConflict.put(inst, machineIt);
+		}
+		// 将inst放入新的machine
+		inst2Machine.put(inst, machineIt);
+		if (!hasApp.containsKey(appIt))
+			hasApp.put(appIt, 0);
+		hasApp.put(appIt, hasApp.get(appIt) + 1);
+		for (int i = 0; i < num_k; i++)
+			machineResourcesUsed[machineIt][i] += appResources[appIt][i];
+
+		return "success";
 	}
 	}
 
 
 	private String toMachine(String inst, int machineIt) {
 	private String toMachine(String inst, int machineIt) {
 		return toMachine(inst, machineIt, true);
 		return toMachine(inst, machineIt, true);
 	}
 	}
 
 
+	private String doCheck(String inst, int machineIt) {
+		int appIt = inst2AppIndex.get(inst);
+		Map<Integer, Integer> hasApp = machineHasApp[machineIt];
+		// 检查互斥规则,初始数据这里有冲突
+		int nowHas = 0;
+		if (hasApp.containsKey(appIt))
+			nowHas = hasApp.get(appIt);
+		for (Integer conditionalApp : hasApp.keySet()) {
+			if (hasApp.get(conditionalApp) <= 0)
+				continue;
+			if (!appInterference[conditionalApp].containsKey(appIt))
+				continue;
+			if (nowHas + 1 > appInterference[conditionalApp].get(appIt)) {
+				return "App Interference, inst: " + inst + ", " + apps[conditionalApp] + " -> " + apps[appIt] + ", "
+						+ (nowHas + 1) + " > " + appInterference[conditionalApp].get(appIt);
+			}
+		}
+		// 初始数据这里有冲突
+		for (Integer checkApp : hasApp.keySet()) {
+			if (!appInterference[appIt].containsKey(checkApp))
+				continue;
+			if (hasApp.get(checkApp) > appInterference[appIt].get(checkApp)) {
+				return "App Interference, inst: " + inst + ", " + apps[appIt] + " -> " + apps[checkApp] + ", "
+						+ (nowHas + 1) + " > " + appInterference[appIt].get(checkApp);
+			}
+		}
+		// 检查资源限制,初始数据这里没有冲突
+		for (int i = 0; i < num_k; i++)
+			if (dcmp(
+					machineResourcesUsed[machineIt][i] + appResources[appIt][i] - machineResources[machineIt][i]) > 0) {
+				String res = "Resource Limit: inst: " + inst + ", " + "machine: " + machines[machineIt] + ", app: "
+						+ apps[appIt] + ", resIter: " + i + ", " + machineResourcesUsed[machineIt][i] + " + "
+						+ appResources[appIt][i] + " > " + machineResources[machineIt][i];
+				// System.out.println(res);
+				return res;
+			}
+
+		return "success";
+	}
+
 	private String toMachine(String inst, int machineIt, boolean doCheck) {
 	private String toMachine(String inst, int machineIt, boolean doCheck) {
 		int appIt = inst2AppIndex.get(inst);
 		int appIt = inst2AppIndex.get(inst);
 		Map<Integer, Integer> hasApp = machineHasApp[machineIt];
 		Map<Integer, Integer> hasApp = machineHasApp[machineIt];
 		if (doCheck) {
 		if (doCheck) {
-			// 检查互斥规则,初始数据这里有冲突
-			int nowHas = 0;
-			if (hasApp.containsKey(appIt))
-				nowHas = hasApp.get(appIt);
-			for (Integer conditionalApp : hasApp.keySet()) {
-				if (hasApp.get(conditionalApp) <= 0)
-					continue;
-				if (!appInterference[conditionalApp].containsKey(appIt))
-					continue;
-				if (nowHas + 1 > appInterference[conditionalApp].get(appIt)) {
-					return "App Interference, inst: " + inst + ", " + apps[conditionalApp] + " -> " + apps[appIt] + ", "
-							+ (nowHas + 1) + " > " + appInterference[conditionalApp].get(appIt);
-				}
+			String res_tmp = doCheck(inst, machineIt);
+			if (res_tmp != "success") {
+				return res_tmp;
 			}
 			}
-			//初始数据这里有冲突
-			for (Integer checkApp : hasApp.keySet()) {
-				if (!appInterference[appIt].containsKey(checkApp))
-					continue;
-				if (hasApp.get(checkApp) > appInterference[appIt].get(checkApp)) {
-					return "App Interference, inst: " + inst + ", " + apps[appIt] + " -> " + apps[checkApp] + ", "
-							+ (nowHas + 1) + " > " + appInterference[appIt].get(checkApp);
-				}
-			}
-			// 检查资源限制,初始数据这里没有冲突
-			for (int i = 0; i < num_k; i++)
-				if (dcmp(machineResourcesUsed[machineIt][i] + appResources[appIt][i]
-						- machineResources[machineIt][i]) > 0) {
-					String res = "Resource Limit: inst: " + inst + ", " + "machine: " + machines[machineIt] + ", app: "
-							+ apps[appIt] + ", resIter: " + i + ", " + machineResourcesUsed[machineIt][i] + " + "
-							+ appResources[appIt][i] + " > " + machineResources[machineIt][i];
-					System.out.println(res);
-					return res;
-				}
-					
 		}
 		}
 		// 将inst放入新的machine
 		// 将inst放入新的machine
 		inst2Machine.put(inst, machineIt);
 		inst2Machine.put(inst, machineIt);
@@ -227,7 +286,8 @@ public class Main {
 	/**
 	/**
 	 * 把实例inst 移除主机,撤销消耗资源
 	 * 把实例inst 移除主机,撤销消耗资源
 	 * 
 	 * 
-	 * @param inst 实例id
+	 * @param inst
+	 *            实例id
 	 */
 	 */
 	private void pickInstance(String inst) {
 	private void pickInstance(String inst) {
 		if (!inst2Machine.containsKey(inst))
 		if (!inst2Machine.containsKey(inst))
@@ -252,10 +312,24 @@ public class Main {
 		return x < 0. ? -1 : 1;
 		return x < 0. ? -1 : 1;
 	}
 	}
 
 
+	private void saveResult(List<String> res) throws IOException {
+		String res_path = "result.csv";
+		File file = new File(res_path);
+		if (!file.exists()) {
+			file.createNewFile();
+		}
+		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
+		for (int i = 0; i < res.size(); i++) {
+			writer.write(res.get(i));
+			writer.newLine();
+		}
+		writer.close();
+	}
+
 	public static void main(String[] args) throws Exception {
 	public static void main(String[] args) throws Exception {
-		if (args.length != 5 && args.length != 2) {
+		if (args.length != 4 && args.length != 2) {
 			System.err.println(
 			System.err.println(
-					"传入参数有误,使用方式为:java -cp xxx.jar  app_resources.csv machine_resources.csv instance_deploy.csv app_interference.csv result.csv");
+					"传入参数有误,使用方式为:java -cp xxx.jar  app_resources.csv machine_resources.csv instance_deploy.csv app_interference.csv");
 			return;
 			return;
 		}
 		}
 
 
@@ -263,13 +337,11 @@ public class Main {
 		long startTime = System.currentTimeMillis(); // 获取开始时间
 		long startTime = System.currentTimeMillis(); // 获取开始时间
 
 
 		InputStream problem;
 		InputStream problem;
-		InputStream result;
 		// app_resources.csv
 		// app_resources.csv
 		// machine_resources.csv
 		// machine_resources.csv
 		// instance_deploy.csv
 		// instance_deploy.csv
 		// app_interference.csv
 		// app_interference.csv
-		// result.csv
-		if (args.length == 5) {
+		if (args.length == 4) {
 			// 将赛题拼成评测数据
 			// 将赛题拼成评测数据
 			StringBuffer sb = new StringBuffer();
 			StringBuffer sb = new StringBuffer();
 			for (int i = 0; i < 4; i++) {
 			for (int i = 0; i < 4; i++) {
@@ -283,21 +355,19 @@ public class Main {
 			}
 			}
 			String alldata = sb.toString();
 			String alldata = sb.toString();
 			problem = new ByteArrayInputStream(alldata.getBytes());
 			problem = new ByteArrayInputStream(alldata.getBytes());
-			result = new FileInputStream(args[4]);
 		} else {
 		} else {
 			problem = new FileInputStream(args[0]);
 			problem = new FileInputStream(args[0]);
-			result = new FileInputStream(args[1]);
 		}
 		}
 
 
 		// 评测
 		// 评测
 		Main evaluator = new Main();
 		Main evaluator = new Main();
 		evaluator.init(new BufferedReader(new InputStreamReader(problem, Charsets.UTF_8)));
 		evaluator.init(new BufferedReader(new InputStreamReader(problem, Charsets.UTF_8)));
-//		evaluator.run();
+		System.out.println("默认已经部署了:" + evaluator.inst2Machine.size());
+		evaluator.run();
 
 
 		long endTime = System.currentTimeMillis(); // 获取结束时间
 		long endTime = System.currentTimeMillis(); // 获取结束时间
 		System.out.println("程序运行时间:" + (endTime - startTime) / 1000 + "s"); // 输出程序运行时间
 		System.out.println("程序运行时间:" + (endTime - startTime) / 1000 + "s"); // 输出程序运行时间
 		System.out.println("-------部署结束啦--------");
 		System.out.println("-------部署结束啦--------");
-
 	}
 	}
 
 
 }
 }

+ 412 - 0
java/me/yoqi/servermanager/Main2.java

@@ -0,0 +1,412 @@
+package me.yoqi.servermanager;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.google.common.base.Charsets;
+
+/**默认不迁移,初始代码。跑a数据分数6148
+ * Created by mou.sunm on 2018/07/02.
+ */
+public class Main2 {
+	// 参数
+	public static final double alpha = 10.;
+	public static final double beta = 0.5;
+	public static final int T = 98;
+	public static final int EXEC_LIMIT = 100000;
+
+	// 静态数据
+	private int num_app; // app数 9338
+	private int num_inst; // inst数 68219
+	private int num_mac; // machine数 6000
+	private int num_k; // 资源种类 200
+	private List<Integer> cpuIter; // [0, 1, 2, ... 97]
+	private Map<String, Integer> appIndex; // app_1,app_2字符串用0,1等数字替换,{app_1=0,
+											// app_2=1,...,app_4=3}
+	private Map<String, Integer> machineIndex;// {machine_1=0, machine_2=1,
+	private List<Integer> machinePriorIndex=new ArrayList<Integer>();
+	private String[] apps; // [app_1, app_2, app_3, app_4,
+	private String[] machines;// [machine_1, machine_2, machine_3, machine_4,
+	private Map<String, Integer> inst2AppIndex;// {inst_157=49}
+
+	private double[][] appResources;// app 9338*200
+	private double[][] machineResources;// 主机 6000*200
+	private Map<Integer, Integer>[] appInterference;// 9338 限制条件[{}, {},
+													// {3517=2}, {}, {5600=1,
+													// 6747=2, 7707=2, 4830=2},
+
+	// 动态数据
+	private Map<String, Integer> inst2Machine;// 部署 {inst_33717=5456,
+	// 这个数据类型和Map<String, Integer>有点区别,加上list会保存顺序
+	private List<String> deployResult = new ArrayList<String>(); // [inst_33717,5456
+	private List<String> instUnDelpoy = new ArrayList<String>();// 未部署的实例
+	private Map<String, Integer> inst2MachineDefaultConflict = new HashMap<String, Integer>();;// 记录初始部署冲突的实例和主机
+	private double[][] machineResourcesUsed;// 6000*200
+	private Map<Integer, Integer>[] machineHasApp;// 6000 [{}, {},{6004=1,
+													// 9126=1, 1598=1}, {}, {},
+													// {},
+
+	/**
+	 * 先对disk排序,然后first fit
+	 * 
+	 * @throws IOException
+	 *             写文件异常
+	 */
+	private void run() throws IOException {
+//		主机优先顺序,使用过的主机在前,目的紧凑;然后大主机在前。做一个主机优先部署序列
+		
+		
+		// 对默认实例有冲突的进行迁移
+		String tmp_Res;
+		String inst_tmp = null;
+		int machineIt_tmp = 0;
+		for (Map.Entry<String, Integer> entry : inst2MachineDefaultConflict.entrySet()) {
+			// 将实例先迁移出来,释放资源
+			pickInstance(entry.getKey());
+			for (Map.Entry<String, Integer> entry2 : machineIndex.entrySet()) {
+				inst_tmp = entry.getKey();
+				machineIt_tmp = entry2.getValue();
+				tmp_Res = toMachine(inst_tmp, machineIt_tmp);
+				if (tmp_Res == "success") {
+					deployResult.add(inst_tmp + "," + "machine_" + (machineIt_tmp + 1));
+					break;
+				}
+			}
+		}
+		System.out.println("默认冲突数:" + inst2MachineDefaultConflict.size());
+		System.out.println("剩余未部署:" + instUnDelpoy.size());
+		//对剩余未部署的实例进行部署
+
+		for (int i = 0; i < instUnDelpoy.size(); i++) {
+			for (Map.Entry<String, Integer> entry2 : machineIndex.entrySet()) {
+				inst_tmp = instUnDelpoy.get(i);
+				machineIt_tmp = entry2.getValue();
+				tmp_Res = toMachine(inst_tmp, machineIt_tmp);
+				if (tmp_Res == "success") {
+					deployResult.add(inst_tmp + "," + "machine_" + (machineIt_tmp + 1));
+					break;
+				}
+			}
+		}
+		saveResult(deployResult);
+	}
+
+	private void sortInstanceByDisk(ArrayList<Integer> instance) {
+		for (int i = 0; i < instance.size(); i++) {
+			inst2AppIndex.get(i);
+		}
+	}
+
+	private void sort(ArrayList<Integer> list) {
+
+	}
+
+	// 读取数据
+	protected void init(BufferedReader bufferedReader) throws IOException {
+		cpuIter = new ArrayList<Integer>();
+		for (int i = 0; i < T; i++)
+			cpuIter.add(i);
+		/** Read app_resources */
+		num_app = Integer.parseInt(bufferedReader.readLine());// 9338
+		apps = new String[num_app];
+		for (int i = 0; i < num_app; i++) {// 循环app表每一行
+			// appId,resources
+			String line = bufferedReader.readLine();
+			String[] parts = line.split(",", -1);
+			List<Double> resources = new ArrayList<Double>();
+			for (String x : parts[1].split("\\|", -1))// cpu
+				resources.add(Double.parseDouble(x));
+			for (String x : parts[2].split("\\|", -1))// mem
+				resources.add(Double.parseDouble(x));
+			for (int j = 3; j < parts.length; j++) // disk/P/M/PM
+				resources.add(Double.parseDouble(parts[j]));
+			if (i == 0) {
+				num_k = resources.size();
+				appIndex = new HashMap<String, Integer>();
+				appResources = new double[num_app][num_k];
+			}
+			if (num_k != resources.size())
+				throw new IOException("[DEBUG 2]Invaild problem");
+			if (appIndex.containsKey(parts[0]))
+				throw new IOException("[DEBUG 3]Invaild problem");
+			appIndex.put(parts[0], i);
+			apps[i] = parts[0];// [app_1, app_2, app_3, app_4]
+			for (int j = 0; j < num_k; j++)
+				appResources[i][j] = resources.get(j);
+		}
+		/** Read machine_resources */
+		num_mac = Integer.parseInt(bufferedReader.readLine());// 6000
+		machineResources = new double[num_mac][num_k];
+		machineResourcesUsed = new double[num_mac][num_k];
+		machineIndex = new HashMap<String, Integer>();// {machine_3791=3790,
+		machineHasApp = new Map[num_mac];
+		machines = new String[num_mac];
+		for (int i = 0; i < num_mac; i++) {
+			// machineId,resources
+			String line = bufferedReader.readLine();
+			String[] parts = line.split(",", -1);
+			if (machineIndex.containsKey(parts[0]))
+				throw new IOException("[DEBUG 4]Invaild problem");
+			machineIndex.put(parts[0], i);
+			machines[i] = parts[0];
+			machineHasApp[i] = new HashMap<Integer, Integer>();
+			double cpu = Double.parseDouble(parts[1]);
+			double mem = Double.parseDouble(parts[2]);
+			for (int j = 0; j < T; j++) {
+				machineResources[i][j] = cpu;
+				machineResources[i][T + j] = mem;
+			}
+			for (int j = 3; j < parts.length; j++)
+				machineResources[i][2 * T + j - 3] = Double.parseDouble(parts[j]);
+			for (int j = 0; j < num_k; j++)
+				machineResourcesUsed[i][j] = 0.;
+
+		}
+		// // 倒序排列,大主机在前面
+		// List<Map.Entry<String, Integer>> list_tmp = new ArrayList<>();
+		// for (Map.Entry<String, Integer> entry : machineIndex.entrySet()) {
+		// list_tmp.add(entry); // 将map中的元素放入list中
+		// }
+		// list_tmp.sort(new Comparator<Map.Entry<String, Integer>>() {
+		// @Override
+		// public int compare(Map.Entry<String, Integer> o1, Map.Entry<String,
+		// Integer> o2) {
+		// return o2.getValue() - o1.getValue();
+		// }
+		// // 逆序(从大到小)排列,正序为“return o1.getValue()-o2.getValue”
+		// });
+		// machineIndex.clear();
+		// for (Map.Entry<String, Integer> entry : list_tmp) {
+		// machineIndex.put(entry.getKey(), entry.getValue());
+		// }
+		/** Read app_interference */
+		int icnt = Integer.parseInt(bufferedReader.readLine());// 35242
+		appInterference = new Map[num_app];
+		for (int i = 0; i < num_app; i++)
+			appInterference[i] = new HashMap<Integer, Integer>();
+		for (int i = 0; i < icnt; i++) {
+			String line = bufferedReader.readLine();
+			String[] parts = line.split(",", -1);
+			if (!appIndex.containsKey(parts[0]) || !appIndex.containsKey(parts[1]))
+				throw new IOException("[DEBUG 8]Invaild problem");
+			int app1 = appIndex.get(parts[0]);
+			int app2 = appIndex.get(parts[1]);
+			int limit = Integer.parseInt(parts[2]);
+			Map<Integer, Integer> inter = appInterference[app1];
+			if (inter.containsKey(app2))
+				throw new IOException("[DEBUG 9]Invaild problem");
+			if (app1 == app2)
+				limit += 1; // self-interference +1 here
+			inter.put(app2, limit);
+		}
+		/** Read instance_deploy */
+		num_inst = Integer.parseInt(bufferedReader.readLine());// 68219
+		inst2AppIndex = new HashMap<String, Integer>();// 68219*2
+														// {inst_33717=8766,
+														// inst_33718=2956}
+		inst2Machine = new HashMap<String, Integer>();// {inst_33717=5456,
+		for (int i = 0; i < num_inst; i++) {
+			String line = bufferedReader.readLine();
+			String[] parts = line.split(",", -1);
+			if (inst2AppIndex.containsKey(parts[0]))
+				throw new IOException("[DEBUG 5]Invaild problem");
+			if (!appIndex.containsKey(parts[1]))
+				throw new IOException("[DEBUG 6]Invaild problem");
+			inst2AppIndex.put(parts[0], appIndex.get(parts[1]));
+			if (!"".equals(parts[2])) {
+				if (!machineIndex.containsKey(parts[2]))
+					throw new IOException("[DEBUG 7]Invaild problem");
+				toMachineDefault(parts[0], machineIndex.get(parts[2]));
+			} else {
+				instUnDelpoy.add(parts[0]);
+			}
+		}
+		System.out.println("finish init");
+	}
+
+	private String toMachineDefault(String inst, int machineIt) {
+		int appIt = inst2AppIndex.get(inst);
+		Map<Integer, Integer> hasApp = machineHasApp[machineIt];
+		String res_tmp = doCheck(inst, machineIt);
+		if (res_tmp != "success") {
+			inst2MachineDefaultConflict.put(inst, machineIt);
+		}
+		// 将inst放入新的machine
+		inst2Machine.put(inst, machineIt);
+		if (!hasApp.containsKey(appIt))
+			hasApp.put(appIt, 0);
+		hasApp.put(appIt, hasApp.get(appIt) + 1);
+		for (int i = 0; i < num_k; i++)
+			machineResourcesUsed[machineIt][i] += appResources[appIt][i];
+
+		return "success";
+	}
+
+	private String toMachine(String inst, int machineIt) {
+		return toMachine(inst, machineIt, true);
+	}
+
+	private String doCheck(String inst, int machineIt) {
+		int appIt = inst2AppIndex.get(inst);
+		Map<Integer, Integer> hasApp = machineHasApp[machineIt];
+		// 检查互斥规则,初始数据这里有冲突
+		int nowHas = 0;
+		if (hasApp.containsKey(appIt))
+			nowHas = hasApp.get(appIt);
+		for (Integer conditionalApp : hasApp.keySet()) {
+			if (hasApp.get(conditionalApp) <= 0)
+				continue;
+			if (!appInterference[conditionalApp].containsKey(appIt))
+				continue;
+			if (nowHas + 1 > appInterference[conditionalApp].get(appIt)) {
+				return "App Interference, inst: " + inst + ", " + apps[conditionalApp] + " -> " + apps[appIt] + ", "
+						+ (nowHas + 1) + " > " + appInterference[conditionalApp].get(appIt);
+			}
+		}
+		// 初始数据这里有冲突
+		for (Integer checkApp : hasApp.keySet()) {
+			if (!appInterference[appIt].containsKey(checkApp))
+				continue;
+			if (hasApp.get(checkApp) > appInterference[appIt].get(checkApp)) {
+				return "App Interference, inst: " + inst + ", " + apps[appIt] + " -> " + apps[checkApp] + ", "
+						+ (nowHas + 1) + " > " + appInterference[appIt].get(checkApp);
+			}
+		}
+		// 检查资源限制,初始数据这里没有冲突
+		for (int i = 0; i < num_k; i++)
+			if (dcmp(
+					machineResourcesUsed[machineIt][i] + appResources[appIt][i] - machineResources[machineIt][i]) > 0) {
+				String res = "Resource Limit: inst: " + inst + ", " + "machine: " + machines[machineIt] + ", app: "
+						+ apps[appIt] + ", resIter: " + i + ", " + machineResourcesUsed[machineIt][i] + " + "
+						+ appResources[appIt][i] + " > " + machineResources[machineIt][i];
+				// System.out.println(res);
+				return res;
+			}
+
+		return "success";
+	}
+
+	private String toMachine(String inst, int machineIt, boolean doCheck) {
+		int appIt = inst2AppIndex.get(inst);
+		Map<Integer, Integer> hasApp = machineHasApp[machineIt];
+		if (doCheck) {
+			String res_tmp = doCheck(inst, machineIt);
+			if (res_tmp != "success") {
+				return res_tmp;
+			}
+		}
+		// 将inst放入新的machine
+		inst2Machine.put(inst, machineIt);
+		if (!hasApp.containsKey(appIt))
+			hasApp.put(appIt, 0);
+		hasApp.put(appIt, hasApp.get(appIt) + 1);
+		for (int i = 0; i < num_k; i++)
+			machineResourcesUsed[machineIt][i] += appResources[appIt][i];
+
+		return "success";
+	}
+
+	/**
+	 * 把实例inst 移除主机,撤销消耗资源
+	 * 
+	 * @param inst
+	 *            实例id
+	 */
+	private void pickInstance(String inst) {
+		if (!inst2Machine.containsKey(inst))
+			return;
+		int appIt = inst2AppIndex.get(inst);
+		int fromMachine = inst2Machine.get(inst);
+		// 更新machineHasApp
+		Map<Integer, Integer> fromHasApp = machineHasApp[fromMachine];
+		fromHasApp.put(appIt, fromHasApp.get(appIt) - 1);
+		if (fromHasApp.get(appIt) <= 0)
+			fromHasApp.remove(appIt);
+		// 更新machineResourcesUsed
+		for (int i = 0; i < num_k; i++)
+			machineResourcesUsed[fromMachine][i] -= appResources[appIt][i];
+		// 更新inst2Machine
+		inst2Machine.remove(inst);
+	}
+
+	private int dcmp(double x) {
+		if (Math.abs(x) < 1e-9)
+			return 0;
+		return x < 0. ? -1 : 1;
+	}
+
+	private void saveResult(List<String> res) throws IOException {
+		String res_path = "result.csv";
+		File file = new File(res_path);
+		if (!file.exists()) {
+			file.createNewFile();
+		}
+		BufferedWriter writer = new BufferedWriter(new FileWriter(file));
+		for (int i = 0; i < res.size(); i++) {
+			writer.write(res.get(i));
+			writer.newLine();
+		}
+		// for (String line : res) {
+		// writer.write(line);
+		// writer.newLine();
+		// }
+		writer.close();
+	}
+
+	public static void main(String[] args) throws Exception {
+		if (args.length != 4 && args.length != 2) {
+			System.err.println(
+					"传入参数有误,使用方式为:java -cp xxx.jar  app_resources.csv machine_resources.csv instance_deploy.csv app_interference.csv");
+			return;
+		}
+
+		System.out.println("-------开始部署啦--------");
+		long startTime = System.currentTimeMillis(); // 获取开始时间
+
+		InputStream problem;
+		// app_resources.csv
+		// machine_resources.csv
+		// instance_deploy.csv
+		// app_interference.csv
+		if (args.length == 4) {
+			// 将赛题拼成评测数据
+			StringBuffer sb = new StringBuffer();
+			for (int i = 0; i < 4; i++) {
+				List<String> lines = new ArrayList<String>();
+				BufferedReader bs = new BufferedReader(new FileReader(new File(args[i])));
+				for (String line = bs.readLine(); line != null; line = bs.readLine())
+					lines.add(line);
+				sb.append("" + lines.size()).append("\n");
+				for (String line : lines)
+					sb.append(line).append("\n");
+			}
+			String alldata = sb.toString();
+			problem = new ByteArrayInputStream(alldata.getBytes());
+		} else {
+			problem = new FileInputStream(args[0]);
+		}
+
+		// 评测
+		Main2 evaluator = new Main2();
+		evaluator.init(new BufferedReader(new InputStreamReader(problem, Charsets.UTF_8)));
+		System.out.println("默认已经部署了:" + evaluator.inst2Machine.size());
+		evaluator.run();
+
+		long endTime = System.currentTimeMillis(); // 获取结束时间
+		System.out.println("程序运行时间:" + (endTime - startTime) / 1000 + "s"); // 输出程序运行时间
+		System.out.println("-------部署结束啦--------");
+	}
+
+}