def otimizar(malha_producao, horizon):
    print('Iniciando a otimização')
    print(malha_producao)
    print(horizon)
    print('Criando do objeto de otimização')  
    Cervejaria = pyschedule.Scenario("Cervejaria", horizon=horizon)
    print('Carregando atividades de produção')
    atividade = _LerJson_Atividades()    
    recursos = Fabrica()
    recursos.gerarRecursos(Cervejaria)      
    print('Gerando as atividades (tasks)')
    gerarTodasAtividades(Cervejaria, atividade, malha_producao)    
    print('Gerandos as restrições (constraints)')
    gerarRestricoes(Cervejaria, atividade, recursos)
    print('Definindo o objetivo de otimização')
    Cervejaria.use_flowtime_objective()
    print('Executando a otimização')    
    pyschedule.solvers.mip.solve(Cervejaria, msg=1, kind='CBC', ratio_gap=.0)
    print('Otimização concluída')    
    print('Salvando a otimização, caso queira ser explorada no script exploring_last_optimization')
    import pickle
    with open('last_optimization.pickle', 'wb') as handle:
        pickle.dump(Cervejaria, handle, protocol=pickle.HIGHEST_PROTOCOL)
    print('Processo de otimização finalizado')
    print('Extraindo as tarefas e recursos da solução')
    df_solution = plot_get_solution(Cervejaria,  fig_size=(30, 10))            
    print('Obtendo estatísticas da solução')
    stats = {
        'makespan' : 0
    }
    print('Retornando resposta para o front end')
    return df_solution, stats
# Taillards 20x5 flow-shop instance downloaded from
# http://mistic.heig-vd.ch/taillard/problemes.dir/ordonnancement.dir/flowshop.dir/tai20_5.txt
# columns = jobs, rows = machines
proc ='\
54 83 15 71 77 36 53 38 27 87 76 91 14 29 12 77 32 87 68 94\n\
79  3 11 99 56 70 99 60  5 56  3 61 73 75 47 14 21 86  5 77\n\
16 89 49 15 89 45 60 23 57 64  7  1 63 41 63 47 26 75 77 40\n\
66 58 31 68 78 91 13 59 49 85 85  9 39 41 56 40 54 77 51 31\n\
58 56 20 85 53 35 53 41 69 13 86 72  8 49 47 87 58 18 68 28'

#proc_table = [ [ int(x) for x in row.replace('  ',' ').strip().split(' ') ] for row in proc.split('\n') ]
proc_table = [ [ int(math.ceil( int(x)/10.0 )) for x in row.replace('  ',' ').strip().split(' ') ] for row in proc.split('\n') ]
n = len(proc_table[0])
m = len(proc_table)
n = 6

S = pyschedule.Scenario('Taillards_Flow_Shop_20x5')
T = { (i,j) : S.Task('T_%i_%i'%(i,j),length=proc_table[j][i]) for i in range(n) for j in range(m) }
R = { j : S.Resource('R_%i'%j) for j in range(m) }

S += [ T[i,j] < T[i,j+1] for i in range(n) for j in range(m-1) ]
for i in range(n) :
	for j in range(m) :
		T[i,j] += R[j]

S.use_makespan_objective()
if pyschedule.solvers.mip.solve_bigm(S,time_limit=120,msg=1):
	pyschedule.plotters.matplotlib.plot(S,resource_height=100.0,hide_tasks=[S._tasks['MakeSpan']])
else:
	print('no solution found')
Exemple #3
0
#! /usr/bin/python
import sys
sys.path.append('../src')
import pyschedule

# get input size
n = 15

S = pyschedule.Scenario('n_queens_type_scheduling',horizon=n)
R = { i : S.Resource('R_%i'%i) for i in range(n) } #resources
T = { (i,j) : S.Task('T_%i_%i'%(i,j)) for i in range(n) for j in range(n) } #tasks

# precedence constrains
S += [ T[i,j-1] < T[i,j] for i in range(n) for j in range(1,n) ]

# resource assignment modulo n
for j in range(n):
	for i in range(n):
		T[(i+j) % n,j] += R[i]

if pyschedule.solvers.mip.solve(S,msg=1,kind='CBC'):
	pyschedule.plotters.matplotlib.plot(S,color_prec_groups=False)
else:
	print('no solution found')
 3 15  1 13  7 11  8  6  9 10 14  2  4 12  5\n\
 6  9 11  3  4  7 10  1 14  5  2 12 13  8 15\n\
 9 15  5 14  6  7 10  2 13  8 12 11  4  3  1\n\
11  9 13  7  5  2 14 15 12  1  8  4  3 10  6'

proc_table = [[int(x) for x in row.replace('  ', ' ').strip().split(' ')]
              for row in proc.split('\n')]
proc_table = [[
    int(math.ceil(int(x) / 10.0))
    for x in row.replace('  ', ' ').strip().split(' ')
] for row in proc.split('\n')]
mach_table = [[int(x) for x in row.replace('  ', ' ').strip().split(' ')]
              for row in mach.split('\n')]
n = 6  #len(proc_table)

S = pyschedule.Scenario('Taillards_Job_Shop_15x15')
T = {(i, j): S.Task('T_%i_%i' % (i, j), length=proc_table[i][j])
     for i in range(n) for j in range(n)}
R = {j: S.Resource('R_%i' % j) for j in range(n)}

S += [T[i, j] < T[i, j + 1] for i in range(n) for j in range(n - 1)]
for i in range(n):
    for j in range(n):
        T[i, j] += R[mach_table[i][j] % n]

S.use_makespan_objective()
if pyschedule.solvers.mip_bigm.solve(S, kind='CBC', msg=1):
    pyschedule.plotters.matplotlib.plot(S,
                                        resource_height=100.0,
                                        hide_tasks=[S._tasks['MakeSpan']])
else:
Exemple #5
0
cities_table = [(city, float(lon), float(lat))
                for city, lon, lat in cities_table]
n = 10  # use only few cities to test, more cities take a lone time #len(cities_table)
coords = {
    cities_table[i][0]: (cities_table[i][2], cities_table[i][1])
    for i in range(n)
}
cities = list(coords)

# add coordinates of vitual start and end at the first city in list
start_city = cities_table[0][0]
coords['start'] = coords[start_city]
coords['end'] = coords[start_city]

# scenario and city tasks
S = pyschedule.Scenario('TSP_Germany')
T = {city: S.Task(city) for city in coords}
Car = S.Resource('Car')

# the car has to pass every city
for city in coords:
    T[city] += Car

# make sure that the tour start and ends at start_city
S += T['start'] < {T[city] for city in coords if city != 'start'}
S += T['end'] > {T[city] for city in coords if city != 'end'}

# add euclidean distances as conditional precedences
S += [ T[city] + int(eucl_dist(coords[city],coords[city_])) << T[city_] \
       for city in coords for city_ in coords if city != city_ ]
# get cities table
cities_table = [ row.split(' ') for row in cities.split('\n') ]
cities_table = [ (city,float(lon),float(lat)) for city,lon,lat in cities_table ]
n = 10 # use only few cities to test, more cities take a lone time #len(cities_table)
capacity = int(n/2) # each vehicle can visit half of the cities
coords = { cities_table[i][0] : (cities_table[i][2],cities_table[i][1]) for i in range(n) }
cities = list(coords)

# add coordinates of vitual start and end at the first city in list
start_city = cities_table[0][0]
coords['start'] = coords[start_city]
coords['end'] = coords[start_city]

# create scenario, city visit tasks, and start and end tasks of blue and red vehicle
S = pyschedule.Scenario('VRP_Germany',horizon=30)
T = { city : S.Task(city) for city in cities }

# resources
# capacity + 2 to include start and end task
R_blue, R_red = S.Resource('blue'), S.Resource('red')

# tasks
T['start'] = S.Task('start')
T['end'] = S.Task('end')

# precedences
S += T['start'] < { T[city] for city in cities if city != 'start' }
S += T['end'] > { T[city] for city in cities if city != 'end' }

# resource assignments