예제 #1
0
def load_output(clicks):
    if clicks is None:
        return go.Figure()
    else:
        tabla_distancias, tabla_capitales = importar_tablas()
        mejor_cromosoma = [5, 21, 15, 14, 16, 3, 2, 11, 9, 0, 4, 8, 19, 1, 23, 20, 10, 22, 12, 7, 18, 6, 13, 17, 5]
        cap = formatear(tabla_capitales, mejor_cromosoma)

        # --------------------------- dibujado del mapa
        frames = []
        for k in range(len(cap)):
            frames.append(go.Frame(data=[
                go.Scattermapbox(
                    mode='markers+lines', 
                    lat=cap['latitud'][:k+1],  
                    lon=cap['longitud'][:k+1],
                    marker={'size': 8, 'color': 'red'},
                    line={'color': 'blue', 'width':2})
                ], name=f'frame{k}'))
     
        # dibujo la figura, y le asigno los cuadros
        fig = go.Figure(
            data=go.Scattermapbox(
                lat=cap['latitud'], 
                lon=cap['longitud'],
                text=cap['capital'],
                hoverinfo='text'
            ),
            layout=go.Layout(        
                title_text=f'Recorrido Mínimo: 12817 km    |   Tiempo Ejecución: 84.782 segundos', 
                hovermode="closest",
                font={'size': 18}
            ),
            frames=frames
        )

        updatemenus = [dict(
                buttons = [
                    dict(
                        args = [None, {"frame": {"duration": 1000, "redraw": True},
                                        "fromcurrent": True, 
                                        "transition": {"duration": 500, 'easing': 'cubic-in-out'}
                                    }],
                        label = "Recorrer",
                        method = "animate"
                        ),
                    dict(
                        args = [[None], {"frame": {"duration": 0, "redraw": True},
                                        "mode": "immediate",
                                        "transition": {"duration": 0}}],
                        label = "Pausar",
                        method = "animate"
                        )
                ],
                direction = "left",
                pad = {"r": 10, "t": 87},
                showactive = False,
                type = "buttons",
                x = 0.14,
                xanchor = "right",
                y = 0.16,
                yanchor = "top"
            )]  

        sliders = [dict(steps = [dict(method= 'animate',
                                    args= [[f'frame{k}'],                           
                                    dict(mode= 'immediate',
                                        frame=dict(duration=400, redraw=True),
                                        transition=dict(duration= 0))
                                        ],
                                    label=f'{cap.iloc[k]["capital"]}'
                                    ) for k in range(len(cap))], 
                        active=0,
                        transition={'duration':500 , 'easing': 'cubic-in-out'},
                        x=0, # slider starting position  
                        y=0, 
                        currentvalue=dict(
                            font=dict(size=25), 
                            visible=True, 
                            xanchor= 'center'
                        ),  
                        borderwidth=2,
                        len=1) #slider length
                ]

        fig.update_layout(
            sliders = sliders,
            updatemenus = updatemenus,
            margin={"r":50,"t":50,"l":50,"b":50},
            mapbox_style="open-street-map",
            autosize=True,
            hovermode='closest',
            mapbox=dict(
                center=dict(
                    lat=-35.876958,
                    lon=-65.293389
                ),
                zoom=3
            ),
            height=600
        )
        return fig
예제 #2
0
def load_output(clicks, mantener_trazo, elitismo, ciclos, p_crossover,
                p_mutacion, tamaño_poblacion):
    if clicks is None:
        return go.Figure()
    else:
        tabla_distancias, tabla_capitales = importar_tablas()
        resultados, tiempo_ejecucion = main_genetico(tabla_distancias,
                                                     p_crossover,
                                                     p_mutacion,
                                                     ciclos,
                                                     tamaño_poblacion,
                                                     25,
                                                     elitismo=elitismo)
        cap = formatear(tabla_capitales, resultados['Mejor cromosoma'])

        # --------------------------- dibujado del mapa
        frames = []
        if mantener_trazo:
            for k in range(len(cap)):
                frames.append(
                    go.Frame(data=[
                        go.Scattermapbox(mode='markers+lines',
                                         lat=cap['latitud'][:k + 1],
                                         lon=cap['longitud'][:k + 1],
                                         marker={
                                             'size': 8,
                                             'color': 'red'
                                         },
                                         line={
                                             'color': 'blue',
                                             'width': 2
                                         })
                    ],
                             name=f'frame{k}'))
        else:
            for k in range(len(cap) - 1):
                frames.append(
                    go.Frame(data=[
                        go.Scattermapbox(mode='markers+lines',
                                         lat=[
                                             cap.iloc[k]['latitud'],
                                             cap.iloc[k + 1]['latitud']
                                         ],
                                         lon=[
                                             cap.iloc[k]['longitud'],
                                             cap.iloc[k + 1]['longitud']
                                         ],
                                         marker={
                                             'size': 8,
                                             'color': 'red'
                                         },
                                         line={
                                             'color': 'blue',
                                             'width': 3
                                         })
                    ],
                             name=f'frame{k}'))
        # dibujo la figura, y le asigno los cuadros
        fig = go.Figure(
            data=go.Scattermapbox(lat=cap['latitud'],
                                  lon=cap['longitud'],
                                  text=cap['capital'],
                                  hoverinfo='text'),
            layout=go.Layout(
                title_text=
                f'Recorrido Mínimo:{resultados["Recorrido Mínimo"]:8.0f} km    |   Tiempo Ejecución: {tiempo_ejecucion:5.5f} s',
                hovermode="closest",
                font={'size': 18}),
            frames=frames)

        updatemenus = [
            dict(buttons=[
                dict(args=[
                    None, {
                        "frame": {
                            "duration": 1000,
                            "redraw": True
                        },
                        "fromcurrent": True,
                        "transition": {
                            "duration": 500,
                            'easing': 'cubic-in-out'
                        }
                    }
                ],
                     label="Recorrer",
                     method="animate"),
                dict(args=[[None], {
                    "frame": {
                        "duration": 0,
                        "redraw": True
                    },
                    "mode": "immediate",
                    "transition": {
                        "duration": 0
                    }
                }],
                     label="Pausar",
                     method="animate")
            ],
                 direction="left",
                 pad={
                     "r": 10,
                     "t": 87
                 },
                 showactive=False,
                 type="buttons",
                 x=0.14,
                 xanchor="right",
                 y=0.16,
                 yanchor="top")
        ]

        sliders = [
            dict(
                steps=[
                    dict(method='animate',
                         args=[[f'frame{k}'],
                               dict(mode='immediate',
                                    frame=dict(duration=400, redraw=True),
                                    transition=dict(duration=0))],
                         label=f'{cap.iloc[k]["capital"]}')
                    for k in range(len(cap))
                ],
                active=0,
                transition={
                    'duration': 500,
                    'easing': 'cubic-in-out'
                },
                x=0,  # slider starting position  
                y=0,
                currentvalue=dict(font=dict(size=25),
                                  visible=True,
                                  xanchor='center'),
                borderwidth=2,
                len=1)  #slider length
        ]

        fig.update_layout(sliders=sliders,
                          updatemenus=updatemenus,
                          margin={
                              "r": 50,
                              "t": 50,
                              "l": 50,
                              "b": 50
                          },
                          mapbox_style="open-street-map",
                          autosize=True,
                          hovermode='closest',
                          mapbox=dict(center=dict(lat=-35.876958,
                                                  lon=-65.293389),
                                      zoom=3),
                          height=600)
        return fig