def make_containers(self):  #workload
        #assumption is that a service is equivalent to a set of containers
        #all tasks in a service have common requirements because they are replicas

        #service specifications
        tasks_per_service = [
            np.random.randint(1, 4) for i in range(self.total_services)
        ]  #add randomness
        for i in range(self.total_services):
            self.service_cpu.append(
                np.random.randint(self.service_cpu_low, self.service_cpu_high))
            self.service_mem.append(
                np.random.randint(self.service_mem_low, self.service_mem_high))

        #make containers
        containers_kub = []  #Instantiated as objects of kubernetes.Container
        containers_ga = []  #Instantiated as objects of ga.Container
        id = 0
        for i in range(len(tasks_per_service)):
            for j in range(tasks_per_service[i]):
                containers_kub.append(
                    kubernetes.Container(self.service_cpu[i],
                                         self.service_mem[i], str(id)))
                containers_ga.append(
                    ga.Container(self.service_cpu[i], self.service_mem[i],
                                 str(id)))
            id += 1
        return (containers_kub, containers_ga)
def off_1_test():
    n1 = ga.Node (4, 16000, 4, 16000, 140, 80)
    n2 = ga.Node (4, 8000, 4, 8000, 160, 90)
    n3 = ga.Node (2, 1024, 2, 1024, 180, 100)
    n4 = ga.Node (6, 6512, 6, 6512, 200, 110)
    nodes_info = [n1,n2,n3,n4]
    node_ids = [0, 0, 0, 0, 2, 2, 3, None]
    c1 = ga.Container(1, 128, "A")
    c2 = ga.Container(1, 128, "B")
    c3 = ga.Container(1, 128, "C")
    c4 = ga.Container(1, 128, "D")
    c5 = ga.Container(1, 128, "E")
    c6 = ga.Container(1, 128, "F")
    c7 = ga.Container(1, 128, "G")
    c8 = ga.Container(1, 128, "H")
    containers = [c1, c2, c3, c4, c5, c6, c7, c8]
    ex1 = ga.Chromosome(node_ids, containers, nodes_info)
    n1.containers_list = [c1, c2, c3, c4]
    n3.containers_list = [c5, c6]
    n4.containers_list = [c7]
    return ex1.get_fitness()
def off_3_test():
    n1 = ga.Node (4, 16000, 4, 16000, 140, 80)
    n2 = ga.Node (4, 8000, 4, 8000, 160, 90)
    n3 = ga.Node (6, 10024, 2, 1024, 180, 100)
    n4 = ga.Node (6, 6512, 6, 6512, 200, 110)
    nodes_info = [n1,n2,n3,n4]
    node_ids = [0, 0, 0, 3, 2, 2, 2, 2, 1, 3, 1, 1, 3]
    c1 = ga.Container(1, 128, "A")
    c2 = ga.Container(1, 128, "A")
    c3 = ga.Container(1, 128, "A")
    c4 = ga.Container(1, 128, "A")
    c5 = ga.Container(1, 128, "D")
    c6 = ga.Container(1, 128, "D")
    c7 = ga.Container(1, 128, "D")
    c8 = ga.Container(1, 128, "D")
    c9 = ga.Container(1, 128, "C")
    c10 = ga.Container(1, 128, "C")
    c11 = ga.Container(1, 128, "B")
    c12 = ga.Container(1, 128, "B")
    c13 = ga.Container(1, 128, "B")
    containers = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13]
    ex1 = ga.Chromosome(node_ids, containers, nodes_info)
    n1.containers_list = [c1, c2, c3]
    n2.containers_list = [c9, c11, c12]
    n3.containers_list = [c5, c6, c7, c8]
    n4.containers_list = [c4, c10, c13]
    return ex1.get_fitness()
def main():
    #1)make physical configs
    #2)decide workload
    #3)get allocation from kub
    #4)get allocation from nsga
    #5)measure quality of kub allocation vs nsga allocation
    #6)show graphs of kub vs final nsga
    #7)look at history of nsga and make graphs of front

    start = timeit.timeit()
    #AWS A1
    per_type_A1 = 20
    cpu_A1 = [1, 2, 4, 8, 16]
    mem_A1 = [2048, 4096, 8192, 16384, 32768]
    #AWS M4
    per_type_M4 = 40
    cpu_M4 = [2, 4, 8, 16, 40, 64]
    mem_M4 = [8192, 16384, 32768, 65536, 163840, 262144]
    #AWS M5a
    per_type_M5a = 80
    cpu_M5a = [2, 4, 8, 16, 32, 48, 64, 96]
    mem_M5a = [8192, 16384, 32768, 65536, 131072, 196608, 262144, 393216]

    #Parameters for instantiating an Experiment are node_cpu,node_mem,per_type,service_cpu,service_mem,total_services,service_cpu_low,service_cpu_high,service_mem_low,service_mem_high
    Exp_1 = Experiment(cpu_A1, mem_A1, per_type_A1, [], [], 100, 0, 2, 25,
                       1000)
    Exp_2 = Experiment(cpu_M4, mem_M4, per_type_M4, [], [], 200, 1, 2, 50,
                       2000)
    Exp_3 = Experiment(cpu_M5a, mem_M5a, per_type_M5a, [], [], 400, 2, 4, 200,
                       4000)
    Exp_4 = Experiment(cpu_M5a, mem_M5a, per_type_M5a, [], [], 100, 0, 1, 25,
                       1000)

    Experiments = [Exp_1]  #[Exp_1, Exp_2, Exp_3, Exp_4]
    for experiment in Experiments:

        (nodes_kub,
         nodes_ga) = experiment.make_nodes()  #list of nodes of Node class
        (containers_kub, containers_ga) = experiment.make_containers(
        )  #list of containers of Container class

        kub_alloc = get_kub_allocations(nodes_kub, containers_kub)
        chr = ga.Chromosome(find_assigned(kub_alloc), containers_kub,
                            kub_alloc, False, None)
        (fa, fb, fc, fd,
         fe) = chr.get_fitness()  #fitness of kubernetes allocation

        nsga_alloc, history = get_nsga_allocations(nodes_ga, containers_ga)
        visualize_history(history, "5_obj.png")
        (f1, f2, f3, f4,
         f5) = nsga_alloc.get_fitness()  #fitness of selected nsga solution

        x_names = ['Obj1', 'Obj2', 'Obj3', 'Obj4', 'Obj5']
        kub = [fa, fb, fc, fd, fe]
        nsga = [f1, f2, f3, f4, f5]

        visualise.obj_over_configs(
            x_names, kub, nsga, "Objective Values",
            "Perfomance of Kubernetes vs NSGA-3 on the 5 fitness objectives")

        end = timeit.timeit()
        print("Execution Time: %s" % (end - start))

        #Rescheduling Experiment
        #Adding dummy container, so that rescheduling is needed
        nodes = nsga_alloc.nodes_info
        min_cpu = 0
        min_mem = 0
        for node in nodes:
            if node.remaining_cpu < experiment.service_cpu_high and node.remaining_memory < experiment.service_mem_high:
                min_cpu = experiment.service_cpu_high
                min_mem = experiment.service_mem_high
                dummy_container = ga.Container(min_cpu, min_mem,
                                               len(containers_ga))
                containers_ga.append(dummy_container)
                #nsga_alloc.node_ids.append(node.id)
                #node.containers_list.append(dummy_container) #maybe then need to compute obj6 except last element
                break

        nsga_rescheduling, history = get_nsga_rescheduling(
            nodes_ga, containers_ga, nsga_alloc)
        visualize_history(history, '6_obj.png')
        (off1, off2, off3, off4, off5, off6) = nsga_rescheduling.get_fitness()

        #Rescheduling impemented as Scheduling
        nsga_re_scheduling, _ = get_nsga_allocations(nodes_ga, containers_ga)
        off_6 = nsga_re_scheduling.off_6(nsga_alloc)
        (off_1, off_2, off_3, off_4, off_5) = nsga_re_scheduling.get_fitness()

        x_names = ['Obj1', 'Obj2', 'Obj3', 'Obj4', 'Obj5', 'Obj6']
        resch = [off1, off2, off3, off4, off5, off6]
        re_sch = [off_1, off_2, off_3, off_4, off_5, off_6]
        visualise.obj_over_configs_rescheduling(
            x_names, resch, re_sch, "Objective Values",
            "Perfomance of Rescheduling on the 6 fitness objectives")