Skip to content
Snippets Groups Projects
Select Git revision
  • main
1 result

graph_plotting.jl

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    graph_plotting.jl 2.11 KiB
    using TSSOS, DynamicPolynomials, LinearAlgebra, Graphs, GLMakie, GraphMakie
    function cs_graph(pop, x, d; numeq, cs_alg="NC")
        opt,sol,data = cs_tssos_first(pop, x, d, numeq=numeq, CS=cs_alg, solve=false, solution=false);
    
        cs_graph(data), data
    end
    
    function cs_graph(data::TSSOS.mcpop_data)
        GCSP = SimpleGraph(length(data.x))
        for l = 1:data.cql
            TSSOS.add_clique!(GCSP, data.cliques[l])
        end
        GCSP
    end
    ## compute & plot CS graph of problem (after chordal extension)
    function node_drag_action_p(p, state, idx, event, axis)
        p[:node_pos][][idx] = event.data
        p[:node_pos][] = p[:node_pos][]
    end
    
    function plot_draggable(G; kwargs...)
        f, ax, p = graphplot(G; kwargs...)
    
        deregister_interaction!(ax, :rectanglezoom)
    
        ndrag = NodeDragHandler((state, idx, event, axis) -> node_drag_action_p(p, state, idx, event, axis))
        register_interaction!(ax, :ndrag, ndrag)
        f
    end
    
    function plot_csp(data::TSSOS.mcpop_data; layout = GraphMakie.NetworkLayout.Align(GraphMakie.NetworkLayout.Stress()))
        GCSP = cs_graph(data)
    
        plot_draggable(
            GCSP,
            ilabels=data.x,
            layout=layout,
        )
    end
    
    #show cliques?
    function plot_csp(pop, x, d; numeq, cs_alg, show_chordal_extension=true, layout = GraphMakie.NetworkLayout.Align(GraphMakie.NetworkLayout.Stress()))
        GCSP_alg, data_alg = cs_graph(pop, x, d, numeq=numeq, cs_alg=cs_alg)
    
        cliques_of_node = [findall(clique -> (v in clique), data_alg.cliques) for v in vertices(GCSP_alg)]
    
        node_labels = join.(cliques_of_node, ", ") #fix label positioning
    
        if show_chordal_extension
            GCSP_nce, data_nce = cs_graph(pop, x, d, numeq=numeq)
            G_diff = difference(GCSP_alg, GCSP_nce)
            edge_color = Dict(e => (has_edge(G_diff, e)) ? :red : :black for e in edges(GCSP_alg))
    
            plot_draggable(GCSP_alg, 
                ilabels=data_alg.x, 
                layout=layout,
                edge_color=edge_color,
                nlabels = node_labels,
                nlabels_distance=30
            )
        else
            plot_draggable(GCSP_alg, 
                ilabels=data_alg.x, 
                layout=layout,
                nlabels = node_labels
            )
        end
    end