Browse Source

libs 文件夹改成utils
data 改成dataset

liuyuqi-dellpc 5 years ago
parent
commit
1da679e3c3

+ 3 - 0
.gitignore

@@ -67,3 +67,6 @@ target/
 /.project
 /.settings/org.eclipse.jdt.core.prefs
 /.settings/org.eclipse.m2e.core.prefs
+/dataset
+/.Rproj.user
+/.settings

+ 19 - 0
R/pre_do.R

@@ -0,0 +1,19 @@
+# 导入数据,预处理
+
+
+#定义变量
+work_space=""
+file_path_d1=""
+file_path_d1=""
+file_path_d1=""
+file_path_d1=""
+
+
+d1=read.csv(file_path_d1,header = FALSE)
+d2=read.csv(file_path_d2,header = FALSE)
+d3=read.csv(file_path_d3,header = FALSE)
+d4=read.csv(file_path_d4,header = FALSE)
+
+
+
+

+ 4 - 0
code/app.py

@@ -37,6 +37,10 @@ df1["mem_avg"] = None
 
 # expand=True表示
 tmp = df1["cpu"].str.split('|', expand=True).astype('float')
+print(type(tmp))
+print(tmp.index)
+print(type(tmp[1]))
+exit(1)
 # [9338 rows x 98 columns]
 df1["cpu_avg"] = tmp.T.mean().T  # 转置,求均值,再转置回来,这样求得一行的均值。
 

+ 1 - 1
code/cpu_opt.py

@@ -5,7 +5,7 @@
 @Time :2018/7/9 7:54
 @File :cpu_opt.py
 '''
-
+import pandas as pd
 
 
 def getWij():

+ 6 - 1
code/instance_deploy.py

@@ -29,5 +29,10 @@ df3 = pd.read_csv(instance_deploy, header=None,
 # print(df3.head())
 
 print(pd.isna(df3["machineid"]).value_counts())
+print(pd.isna(df3.machineid[1]))
+print(pd.isna(df3["machineid"][2]))
+print(df3.machineid[1]=="nan")#错误
+print(df3.machineid[1]=="NaN")
+
 # True     38223
-# False    29996 还有一半没有部署
+# False    29996 还有一半没有部署

+ 206 - 19
code/main.py

@@ -6,29 +6,117 @@
 @File :main.py
 '''
 
-# 数据预览
-import pandas as pd
+import time
 from configparser import ConfigParser
 
+import pandas as pd
+
 import libs.save_conf
 
+
 class Scheduling():
-    sb=[]
-    df3=None
-    
-    def init(self,name,gender,birth,**kw):
-        self.name = name
-        self.gender = gender
-        self.birth = birth
-        for k,v in kw.iteritems() :
-            setattr(self,k,v)
-        self.import_data()
-
-    def import_data():
-        df3=pd.read_csv("app")
-
-# step1: 数据参数初始化
+    '''
+    调度
+    '''
+
+    # 参数
+    alpha = 10
+    beta = 0.5
+    T = 98
+    EXEC_LIMIT = 100000
+
+    # 静态数据 n:app数 N:inst数 m:machine数 k:资源种类
+    n = N = m = 0
+    k = 200
+    cpuIter = list()
+    appIndex = {}
+    machineIndex = {}
+    inst2AppIndex = {}
+    appIndexference = {}
+
+    apps = list()
+    machines = list()
+    appResources = list()
+    machineResources = list()
+
+    # 动态数据
+    inst2Machine = {}
+    machineResourcesUsed = list()
+    machineHasApp = pd.DataFrame(columns=list(["instanceid", "machineid"]), data=list())
+
+    def __init__(self, **kw):
+        for k, v in kw.items():
+            setattr(self, k, v)
+
+    def loadData(self):
+        '''   n
+               app_resources.csv
+               m
+               machine_resources.csv
+               N
+               instance_deploy.csv
+               iterference_cnt
+               app_interference.csv
+            judge framework
+        '''
+        app_interference, app_resources, instance_deploy, machine_resources = self.getConfig()
+        # 1.app_resources 9338*201
+        self.appResources = pd.read_csv(app_resources, header=None,
+                                        names=list(["appid", "cpu", "mem", "disk", "P", "M", "PM"]), encoding="utf-8")
+
+        tmp_cpu = self.appResources["cpu"].str.split('|', expand=True).astype('float')
+        tmp_mem = self.appResources["mem"].str.split('|', expand=True).astype('float')
+
+        for i in range(self.T):
+            # 新添加98列CPU限制
+            # self.appResources["cpu_" + str(i)] = None
+            # self.appResources["mem_" + str(i)] = None
+            # 赋值
+            self.appResources["cpu_" + str(i)] = tmp_cpu[i]
+            self.appResources["mem_" + str(i)] = tmp_mem[i]
+
+        # 去掉cpu/men两列
+        self.appResources.pop("cpu")
+        self.appResources.pop("mem")
+        self.n, col = self.appResources.shape  # 9338*201 201列:appid,cpu_1,cpu_2,...mem_1,men_2....,P,M,PM
+
+        # 2.machine_resources 6000*201
+        self.machineResources = pd.read_csv(machine_resources, header=None, names=list(
+            ["machineid", "cpu", "mem", "disk", "P", "M", "PM"]), encoding="utf-8")
+
+        for i in range(self.T):
+            self.machineResources["cpu_" + str(i)] = self.machineResources["cpu"]
+            self.machineResources["mem_" + str(i)] = self.machineResources["mem"]
+        self.machineResources.pop("cpu")
+        self.machineResources.pop("mem")
+
+        self.machineResourcesUsed = self.machineResources.copy()
+
+        for i in range(200):
+            self.machineResourcesUsed.iloc[:, i + 1] = 0
+
+        # 3.instance_deploy
+        self.inst2Machine = pd.read_csv(instance_deploy, header=None,
+                                        names=list(["instanceid", "appid", "machineid"]), encoding="utf-8")
+        # 增加一个字段标注是否部署
+        self.inst2Machine["isdeploy"] = False
+        # # 4.app_interference 冲突表
+        self.appIndexference = pd.read_csv(app_interference, header=None,
+                                           names=list(["appid1", "appid2", "max_interference"]), encoding="utf-8")
+
+        # instance按照磁盘消耗排序
+        self.n, col = self.appResources.shape
+        self.N, col = self.machineResources.shape
+        self.m, col = self.inst2Machine.shape
+
     def getConfig(self):
+        '''
+        step1: 数据参数初始化
+        :return:
+        '''
+        # 生成配置文件
+        self.init_conf()
+        # 读取配置文件
         cf = ConfigParser()
         config_path = "../conf/config.ini"
         section_name = "data_file_name"
@@ -38,7 +126,7 @@ class Scheduling():
         app_resources = cf.get(section_name, "app_resources")
         instance_deploy = cf.get(section_name, "instance_deploy")
         machine_resources = cf.get(section_name, "machine_resources")
-
+        return app_interference, app_resources, instance_deploy, machine_resources
 
     def init_conf(self):
         '''
@@ -48,7 +136,106 @@ class Scheduling():
         libs.save_conf.write()
 
     def sort_dynamic(self):
+        print("ss")
+
+    def pickInstance(self, instanceid):
+        '''
+        先将instance从部署的主机中删除,删除一行,释放资源
+        :return:
+        '''
+        self.inst2Machine.pop(instanceid)
+
+    def toMachine(self, instanceid, machineid, doCheck=True):
+        '''
+        检查互斥条件,然后把instance放入主机
+        :param instanceid: 实例id
+        :param machineid: 主机id
+        :param doCheck: 是否检测资源限制
+        :return: True和False
+        '''
+        appid = self.inst2Machine[self.inst2Machine["instanceid"] == instanceid]["appid"].values[0]
+        if doCheck:
+            # 检查互斥
+
+
+            # 检查资源限制
+            for i in range(self.k):
+                if (
+                                self.machineResourcesUsed[self.machineResourcesUsed["machineid"] == machineid].iloc[:,
+                                i + 1].values[0] +
+                                self.appResources[self.appResources["appid"] == appid].iloc[:, i + 1].values[0]
+                            >
+                            self.machineResources[self.machineResources["machineid"] == machineid].iloc[:,
+                            i + 1].values[0]):
+                    print("Resource Limit: instance: ", instanceid, ",", "machine:", machineid,
+                          self.machineResourcesUsed[self.machineResourcesUsed["machineid"] == machineid].iloc[:,
+                          i + 1].values[0], "+",
+                          self.appResources[self.appResources["appid"] == appid].iloc[:, i + 1].values[0], " >",
+                          self.machineResources[self.machineResources["machineid"] == machineid].iloc[:,
+                          i + 1].values[0])
+                    # 如果不符合则 return False
+                    return False
+        # instance占用资源
+        for i in range(self.k):
+            self.machineResourcesUsed[self.machineResourcesUsed["machineid"] == machineid].iloc[:, i + 1].values[0] += \
+                self.appResources[self.appResources["appid"] == appid].iloc[:, i + 1].values[0]
+        return True
+
+    def run(self):
+        '''
+
+        :return:
+        '''
+        # 已经部署的instance
+        deployed_Instance = self.inst2Machine.loc[pd.isna(self.inst2Machine["machineid"]) == False]
+        count_deployed_Instance, col = deployed_Instance.shape
+        deployed_Instance = deployed_Instance.reset_index(drop=True)
+
+        # 将已经部署的instance放置到对应主机中,占用相应资源,这一块代码比java慢了太多
+        for i in range(count_deployed_Instance):
+            print(i)
+            instanceid = deployed_Instance["instanceid"][i]
+            machineid = deployed_Instance["machineid"][i]
+            self.toMachine(instanceid, machineid, doCheck=False)
+
+        # 先对已经部署的主机列表按照资源消耗进行排序
+
+        # 先使用大主机,磁盘优先计算限制条件
+        row1, col = self.inst2Machine.shape
+        while row1 > 0:
+            # 每部署一次,消耗一个主机
+            for row2 in self.inst2Machine.itertuples():
+                if row2.
+                self.toMachine(row2)
+
+            # 筛选未部署的
+            self.inst2Machine = self.inst2Machine[self.inst2Machine["isdeploy"] == False]
+            row, col = self.inst2Machine.shape
+            self.inst2Machine = self.inst2Machine.reset_index(drop=True)
+            j = j + 1
+
+            print("已经部署:", 68219 - row, "剩余部署Instance数据:", row)
+            print("已经消耗Machine主机数据:", j)
+        print("部署方案前几条示意:", self.machineHasApp.head())
+        libs.save_result.save_result(self.machineHasApp)
+
+    def deployInstance(self):
+        '''
+        部署逻辑
+        :return:
+        '''
+        pass
 
 
 if __name__ == '__main__':
-    init_conf()
+    print("------------开始部署啦--------------")
+    start = time.time()
+    scheduling = Scheduling()
+    # 加载数据
+    scheduling.loadData()
+    # 开始调度
+    scheduling.run()
+    # 部署完事
+    print("------------部署完啦--------------")
+    end = time.time()
+    print("总共耗时:", end - start, "秒")

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

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

+ 300 - 2
java/me/yoqi/servermanager/Main.java

@@ -1,9 +1,307 @@
 package me.yoqi.servermanager;
 
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+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;
+
+/**
+ * Created by mou.sunm on 2018/07/02.
+ */
 public class Main {
+	// 参数
+	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 n; // app数
+	private int N; // inst数
+	private int m; // machine数
+	private int k; // 资源种类
+	private List<Integer> cpuIter; // T个时刻的cpu资源
+	private Map<String, Integer> appIndex;
+	private Map<String, Integer> machineIndex;
+	private String[] apps;
+	private String[] machines;
+	private Map<String, Integer> inst2AppIndex;
+
+	private double[][] appResources;// app
+	private double[][] machineResources;// 主机
+	private Map<Integer, Integer>[] appInterference;// 限制条件
+
+	// 动态数据
+	private Map<String, Integer> inst2Machine;// 部署
+	private double[][] machineResourcesUsed;
+	private Map<Integer, Integer>[] machineHasApp;
+
+	/**
+	 * 先对disk排序,然后first fit
+	 */
+	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 sort(ArrayList<Integer> list) {
+		
+	}
+
+	// 读取数据
+	protected void init(BufferedReader bufferedReader) throws IOException {
+		/*
+		 * Preprocessing: cat *.csv to one file as: n app_resources.csv m
+		 * machine_resources.csv N instance_deploy.csv iterference_cnt
+		 * app_interference.csv judge framework
+		 */
+		/** cpuIter */
+		cpuIter = new ArrayList<Integer>();// 1,2,3....98
+		for (int i = 0; i < T; i++)
+			cpuIter.add(i);
+		/** Read app_resources */
+		n = Integer.parseInt(bufferedReader.readLine());// 9338
+		apps = new String[n];
+		for (int i = 0; i < n; 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) {
+				k = resources.size();
+				appIndex = new HashMap<String, Integer>();
+				appResources = new double[n][k];
+			}
+			if (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);// {app_5269=5268, app_5267=5266,
+										// app_6598=6597}
+			apps[i] = parts[0];// [app_1, app_2, app_3, app_4]
+			for (int j = 0; j < k; j++)
+				appResources[i][j] = resources.get(j);
+		}
+		/** Read machine_resources */
+		m = Integer.parseInt(bufferedReader.readLine());// 6000
+		machineResources = new double[m][k];
+		machineResourcesUsed = new double[m][k];
+		machineIndex = new HashMap<String, Integer>();// {machine_3791=3790,
+														// machine_3792=3791}
+		machineHasApp = new Map[m];
+		machines = new String[m];
+		for (int i = 0; i < m; 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 < k; j++)
+				machineResourcesUsed[i][j] = 0.;
+		}
+		/** Read app_interference */
+		int icnt = Integer.parseInt(bufferedReader.readLine());// 35242
+		appInterference = new Map[n];
+		for (int i = 0; i < n; 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 */
+		N = 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}
+		for (int i = 0; i < N; 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");
+				toMachine(parts[0], machineIndex.get(parts[2]), true);
+			}
+		}
+	}
+
+	private String toMachine(String inst, int machineIt) {
+		return toMachine(inst, machineIt, true);
+	}
+
+	private String toMachine(String inst, int machineIt, boolean doCheck) {
+		int appIt = inst2AppIndex.get(inst);
+		Map<Integer, Integer> hasApp = machineHasApp[machineIt];
+		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);
+				}
+			}
+			//初始数据这里有冲突
+			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 < 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
+		inst2Machine.put(inst, machineIt);
+		if (!hasApp.containsKey(appIt))
+			hasApp.put(appIt, 0);
+		hasApp.put(appIt, hasApp.get(appIt) + 1);
+		for (int i = 0; i < 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 < 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;
+	}
+
+	public static void main(String[] args) throws Exception {
+		if (args.length != 5 && args.length != 2) {
+			System.err.println(
+					"传入参数有误,使用方式为:java -cp xxx.jar  app_resources.csv machine_resources.csv instance_deploy.csv app_interference.csv result.csv");
+			return;
+		}
+
+		System.out.println("-------开始部署啦--------");
+		long startTime = System.currentTimeMillis(); // 获取开始时间
+
+		InputStream problem;
+		InputStream result;
+		// app_resources.csv
+		// machine_resources.csv
+		// instance_deploy.csv
+		// app_interference.csv
+		// result.csv
+		if (args.length == 5) {
+			// 将赛题拼成评测数据
+			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());
+			result = new FileInputStream(args[4]);
+		} else {
+			problem = new FileInputStream(args[0]);
+			result = new FileInputStream(args[1]);
+		}
+
+		// 评测
+		Main evaluator = new Main();
+		evaluator.init(new BufferedReader(new InputStreamReader(problem, Charsets.UTF_8)));
+		evaluator.run();
 
-	public static void main(String[] args) {
-		// TODO 自动生成的方法存根
+		long endTime = System.currentTimeMillis(); // 获取结束时间
+		System.out.println("程序运行时间:" + (endTime - startTime) / 1000 + "s"); // 输出程序运行时间
+		System.out.println("-------部署结束啦--------");
 
 	}
 

+ 29 - 0
java/me/yoqi/test/CSVOperation.java

@@ -0,0 +1,29 @@
+package me.yoqi.test;
+
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVRecord;
+
+/**
+ * 官方文档:http://commons.apache.org/proper/commons-csv/user-guide.html
+ * 还有一个javacsv包更人性化。
+ * @author liuyuqi
+ *
+ */
+public class CSVOperation {
+
+	public void write() {
+		
+	}
+	public void read() throws IOException {
+		Reader in = new FileReader("path/to/file.csv");
+		Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
+		for (CSVRecord record : records) {
+		    String lastName = record.get("Last Name");
+		    String firstName = record.get("First Name");
+		}
+	}
+}

+ 7 - 2
pom.xml

@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>me.yoqi.ServerManager</groupId>
@@ -39,7 +40,11 @@
 			<artifactId>guava</artifactId>
 			<version>r05</version>
 		</dependency>
-
+		<dependency>
+			<groupId>org.apache.commons</groupId>
+			<artifactId>commons-csv</artifactId>
+			<version>1.4</version>
+		</dependency>
 		<dependency>
 			<groupId>org.apache.commons</groupId>
 			<artifactId>commons-lang3</artifactId>

+ 45 - 5
test/test_pandas.py

@@ -57,16 +57,27 @@ def t5():
 
 
 def t6():
+    '''
+    切片:
+    :return:
+    '''
     df = pd.DataFrame([{"A": "11", "B": "12"}, {"A": "111", "B": "121"}, {"A": "1111", "B": "1211"}])
 
     print(df)
     print(df.columns.size)  # 列数 2
     h, l = df.shape
-    print(h, l)
+    print(h, l)  # 3,2
     print(df.iloc[:, 0].size)  # 行数 3
     print(df.ix[[0]].index.values[0])  # 索引值 0
     print(df.ix[[0]].values[0][0])  # 第一行第一列的值 11
     print(df.ix[[1]].values[0][1])  # 第二行第二列的值 121
+    print(df.A, df.B)
+    print(df["A"], df["B"])
+    print(df.loc["A"])
+    print(df.loc[df["A"] > 1])
+    print(df.loc[pd.isna(df["A"])] == False)
+    print(df[df.isna["A"]] == False)  # .loc可以省略
+    # iloc和loc:iloc按0,1,2,3等索引每行;loc按每列的列名索引
 
 
 def t7():
@@ -98,13 +109,42 @@ def t8():
 
 # DataFrame循环遍历
 def t9():
-    df = pd.DataFrame({'a': [1, 2, 3], 'b': [3, 4, 5]})
+    df = pd.DataFrame({'a': [1, 2, 3], 'b': [3, 4, "C"]})
     for row in df.itertuples():
-        print("the index", row.Index)
-    print("sum of row", row.a + row.b)
+        print(row.a, row.b)
+
+    for row in df.items():
+        print(row[1][0], row[1][1], row[1][2])
+
+    # 不推荐
+    for row in df.iteritems():
+        print(row[1][0], row[1][1], row[1][2])
+
+    # 不推荐
+    for row in df:
+        print(df[row][0], df[row][1], df[row][2])
+
+
+def t10():
+    for i in range(10):
+        print(i)
+
+
+def t11():
+    '''
+
+    :return:
+    '''
+    df = pd.DataFrame({'a': [1, 2, 3], 'b': [3, 4, "C"]})
+    print(df)
+    df1 = df
+    df2 = df.copy() #正确
+    df1.a = [2, 2, 2]  # 直接使用=只传址,df,df1任何更改,两个变量都更改
+    df.b = [3, 3, 3]
+    print(df1)
 
 
-t9()
+t11()
 
 # result = pd.DataFrame(columns=list(["instanceid", "machineid"]), data=list())
 

+ 0 - 0
libs/__init__.py → utils/__init__.py


+ 42 - 0
utils/config.py

@@ -0,0 +1,42 @@
+class DefaultConfig(dict):
+	def __init__(self):
+		# ------------ 数据路径 ------------
+		
+		self['data_dir'] = '../../MOBIKE_CUP_2017'
+		self['train_csv'] = self['data_dir'] + '/train.csv'
+		self['test_csv'] = self['data_dir'] + '/test.csv'
+		self['cache_dir'] = '../cache'
+		self['model_dir'] = '../snapshot'
+		self['result_dir'] = '../result'
+
+		# ------------ 训练参数 --------
+
+		self['startday'] = 23
+		self['endday'] = 25
+		self['lgb_leaves'] = 96
+		self['lgb_lr'] = 0.05
+
+		# ------------ 测试参数 --------
+
+		self['test_startday'] = 25
+		self['test_endday'] = 26
+		self['model_name'] = None
+        
+		# -------- 是否有用户 --------
+        
+		self['user'] = True
+
+	def update(self, **kwargs):
+		for key in kwargs:
+		    self[key] = kwargs[key]
+		self['time_prefix'] = '2017-05-'
+		self['time_suffix'] = ' 00:00:00'
+		self['starttime'] = '2017-05-' + str(self['startday']) + ' 00:00:00'
+		self['endtime'] = '2017-05-' + str(self['endday']) + ' 00:00:00'
+		self['test_starttime'] = '2017-05-' + str(self['test_startday']) + ' 00:00:00'
+		self['test_endtime'] = '2017-05-' + str(self['test_endday']) + ' 00:00:00'
+		
+	def printf(self):
+		print('Current Config:')
+		for key in self:
+			print('{}: {}'.format(key, self[key]))

+ 0 - 0
libs/mlogging.py → utils/mlogging.py


+ 1 - 1
libs/save_conf.py → utils/save_conf.py

@@ -47,4 +47,4 @@ def write():
 
 def read():
     cf.read(config_file)
-    print(cf.get(section_name, "app_interference"))
+    print(cf.get(section_name, "app_interference"))

+ 0 - 0
libs/save_result.py → utils/save_result.py


+ 0 - 0
libs/score.py → utils/score.py