def main():

    database, username, password = pp.dbConnectionCheck()

    pp.read_CNCs('./장비정보.xlsx', CNCs)
    JOB_POOL = list()
    start = str(input("delivery date from: "))
    end = str(input("delivery date till: "))
    standard = input("schedule starts on : ")
    NGEN = int(input("# of gen: "))
    POP_SIZE = MU = int(input("MU : "))
    LAMBDA = int(input("LAMBDA : "))
    segmentation_cutoff = int(input("segmentation cut off: "))
    partition_size = int(input("the size of a job partition : "))

    IND_SIZE, no_cycle_time = pp.make_job_pool(JOB_POOL, start, end, database,
                                               username, password,
                                               segmentation_cutoff,
                                               partition_size)
    LEFT_OVER = pp.getLeftOver(database, username, password)

    for j in JOB_POOL:
        print(j.getGoodCd(), j.getGoodNo(), j.getCycletime(), j.getType(),
              j.getQuantity())

    # end = str(input("delivery date until: "))

    hof = tools.HallOfFame(MU)
    #hof = tools.ParetoFront()
    standard_in_datetime = standard
    standard = (lambda x: int(time.time()) if (x == 'now') else time.mktime(
        (int(x[0:4]), int(x[4:6]), int(x[6:8]), 0, 0, 0, 0, 0, 0)))(standard)
    standard = int(standard)

    toolbox.register("schedule", random.sample, range(IND_SIZE), IND_SIZE)
    toolbox.register("Individual", tools.initIterate, creator.individual,
                     toolbox.schedule)
    toolbox.register("evaluate", evaluate2, standard, CNCs, JOB_POOL,
                     VALVE_PRE_CNCs, LOK_FORGING_CNCs, LOK_HEX_CNCs, LEFT_OVER)
    toolbox.register("population", tools.initRepeat, list, toolbox.Individual)
    toolbox.register("mate", tools.cxPartialyMatched)
    #toolbox.register("mate", tools.cxOrdered)
    #toolbox.register("mate", genetic_operators.cycle_crossover)
    #toolbox.register("mutate", tools.mutInversion)
    toolbox.register("mutate",
                     genetic_operators.inversion_with_displacement_mutation)
    toolbox.register("select", tools.selNSGA2)

    pop = toolbox.population(n=POP_SIZE)

    start_point = time.time()

    for ith, indiv in enumerate(pop):
        indiv.sort(key=lambda job_number:
                   (JOB_POOL[job_number].getDue(),
                    (-1) * JOB_POOL[job_number].getTime()),
                   reverse=False)
        f1 = evaluate(indiv)
        print(f1)
        indiv.sort(key=lambda job_number:
                   (JOB_POOL[job_number].getDue(),
                    (1) * JOB_POOL[job_number].getTime()),
                   reverse=False)
        f2 = toolbox.evaluate(indiv)
        print(f2)
        if f1[1][0] < f2[1][0]:
            indiv.sort(key=lambda job_number:
                       (JOB_POOL[job_number].getDue(),
                        (-1) * JOB_POOL[job_number].getTime()),
                       reverse=False)

    result = algorithms.eaMuPlusLambda(pop,
                                       toolbox,
                                       mu=MU,
                                       lambda_=LAMBDA,
                                       cxpb=CXPB,
                                       mutpb=MUTPB,
                                       ngen=NGEN,
                                       stats=None,
                                       halloffame=hof,
                                       verbose=None)

    m, s = divmod((time.time() - start_point), 60)
    h, m = divmod(m, 60)

    print(
        "------------------------------------------Hall of fame------------------------------------------------"
    )
    for i in range(min(MU, len(hof))):
        print(i + 1, hof[i].raw, end=" ")
        print(i + 1, len(hof[i].assignment))
    print(
        "------------------------------------------Hall of fame------------------------------------------------"
    )

    print("NGEN : %d\nMu : %d\nLambda : %d\nMUTPB : %f\nCXPB : %f\n" %
          (NGEN, MU, LAMBDA, MUTPB, CXPB))
    print("delivery date from %s to %s, starting date %s" %
          (start, end, standard_in_datetime))
    print("Weights : ", WEIGHTS)
    print("MU : %d, LAMBDA : %d" % (MU, LAMBDA))
    print("%s hours %s minutes and %s seconds" % (h, m, s))

    indiv_optimized = SA.simulated_annealing(
        hof[0],
        100,
        0.97,
        standard,
        CNCs,
        JOB_POOL,
        VALVE_PRE_CNCs,
        LOK_FORGING_CNCs,
        LOK_HEX_CNCs,
        LEFT_OVER,
    )

    result = toolbox.evaluate(indiv_optimized)
    indiv_optimized.fitness.values = result[0]

    print(indiv_optimized.fitness.values)

    while (1):
        printed = []
        schedules_selected = input(
            "Choose the schedules you want to print out(press 'q' to quit) : ")
        if schedules_selected == 'q':
            print("quit")
            break
        selected = re.findall("\d+", schedules_selected)
        selected = list(map(int, selected))
        try:
            for i in selected:
                if i in printed:
                    print("%d already printed" % i)
                    continue
                dr.print_job_schedule(
                    assignment=hof[i - 1].assignment,
                    scores=hof[i - 1].fitness.values,
                    start=start,
                    end=end,
                    standard=standard_in_datetime,
                    total_number=len(hof[i - 1]),
                    total_number_unassgiend=hof[i - 1].unassigned,
                    schedule_type="optimized",
                    endsAt=standard + hof[i - 1].raw[2],
                    numDelayed=hof[i - 1].raw[0],
                    no_cycle_time=no_cycle_time,
                    mu=MU,
                    Lambda=LAMBDA,
                    cx=CXPB,
                    mut=MUTPB,
                    rank=i)
                print("%d successfully printed" % i)
                printed.append(i)
        except Exception as ex:
            print("an error occured! : ", ex)
            continue
Esempio n. 2
0
    standard = (lambda x: int(time.time()) if (x == 'now') else time.mktime(
        (int(x[0:4]), int(x[4:6]), int(x[6:8]), 12, 0, 0, 0, 0, 0)))(standard)
    standard = int(standard)

    creator.create("FitnessMul", base.Fitness, weights=(-1.3, -1.0, -1.0))
    creator.create("Individual",
                   list,
                   metrics=list,
                   fitness=creator.FitnessMul,
                   individual_number=int,
                   assignment=dict)

    toolbox = base.Toolbox()
    toolbox.register("schedule", random.sample, range(IND_SIZE), IND_SIZE)
    toolbox.register("individual", tools.initIterate, creator.Individual,
                     toolbox.schedule)

    indiv = toolbox.individual()
    indiv.sort(key=lambda job_number: (JOB_POOL[job_number].getDue(),
                                       (-1) * JOB_POOL[job_number].getTime()),
               reverse=False)
    for j in indiv:
        print(JOB_POOL[j].getDue())
        print(JOB_POOL[j].getTime())

    pre_evaluate(standard, machines, CNCs, JOB_POOL, indiv)

    print(indiv.fitness.values)

    dr.print_job_schedule(indiv, start, end, standard_in_datetime, "greedy")
Esempio n. 3
0
                                       cxpb=CXPB,
                                       mutpb=MUTPB,
                                       ngen=NGEN,
                                       stats=None,
                                       halloffame=hof,
                                       verbose=None)
    print(
        "------------------------------------------Hall of fame------------------------------------------------"
    )
    for i in range(len(hof)):
        print(i + 1, hof[i].fitness.values)
    print(
        "------------------------------------------Hall of fame------------------------------------------------"
    )

    m, s = divmod((time.time() - start_point), 60)
    h, m = divmod(m, 60)
    print("%s hours %s minutes and %s seconds" % (h, m, s))
    schedules_selected = input("Choose the schedules you want to print out : ")
    selected = schedules_selected.split(" ")
    selected = list(map(int, selected))
    while (1):
        try:
            for i in selected:
                dr.print_job_schedule(hof[i - 1], start, end,
                                      standard_in_datetime, "optimized", i)
            break
        except Exception as ex:
            print("an error occured! : ", ex)
            continue
    machines = {}
    for cnc in CNCs:
        machines[float(cnc.getNumber())] = list()
    standard = input("schedule starts on : ")
    standard_in_datetime = standard
    standard = (lambda x: int(time.time()) if (x == 'now') else time.mktime(
        (int(x[0:4]), int(x[4:6]), int(x[6:8]), 12, 0, 0, 0, 0, 0)))(standard)
    standard = int(standard)

    creator.create("FitnessMul", base.Fitness, weights=(-1.0, -1.0, -1.0))
    creator.create("Individual", list, metrics=list, fitness=creator.FitnessMul, individual_number=int, assignment=dict)

    toolbox = base.Toolbox()
    toolbox.register("schedule", random.sample, range(IND_SIZE), IND_SIZE)
    toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.schedule)

    best = toolbox.individual()
    best.fitness.values = [10000,100000000,100000000]
    for i in range(0,10000000):
        indiv = toolbox.individual()
        pre_evaluate(standard, machines, CNCs, JOB_POOL, indiv)

        if indiv.fitness.values[0] < best.fitness.values[0]:
            best = copy.deepcopy(indiv)

        print("best so far: ", end='')
        print(best.fitness.values)


    dr.print_job_schedule(best, start, end, standard_in_datetime, "shuffle")
Esempio n. 5
0
        LOK_FORGING_CNCs,
        LOK_HEX_CNCs,
        LEFT_OVER,
    )

    m, s = divmod((time.time() - start_point), 60)
    h, m = divmod(m, 60)
    print("%s hours %s minutes and %s seconds" % (h, m, s))

    result = toolbox.evaluate(indiv_optimized)
    indiv_optimized.fitness.values = result[0]

    print(indiv_optimized.fitness.values)

    dr.print_job_schedule(assignment=indiv_optimized.assignment,
                          scores=indiv_optimized.fitness.values,
                          start=start,
                          end=end,
                          standard=standard_in_datetime,
                          total_number=len(indiv_optimized),
                          total_number_unassgiend=indiv_optimized.unassigned,
                          schedule_type="greedy",
                          endsAt=standard + indiv_optimized.raw[2],
                          numDelayed=indiv_optimized.raw[0],
                          no_cycle_time=no_cycle_time,
                          mu=0,
                          Lambda=0,
                          cx=0,
                          mut=0,
                          rank=0)