如何绘制混合整数问题结果?

时间:2019-05-01 08:10:32

标签: julia julia-jump julia-studio

我解决了线性双目标混合整数问题,并想绘制结果。结果包括线和点。例如

list=[([0.0; 583000.0], 0), ([190670.0; 149600.0], 0), ([69686.0, 385000.0], 1), ([33296.0, 484000.0], 1), ([136554.0, 2.38075e5], 1), ([24556.0, 503800.0], 0), ([47462.0, 437800.0], 1), ([129686.0, 253000.0], 1), ([164278.0, 178200.0], 1)]

在此列表中,第三点([69686.0, 385000.0], 1)的第二个元素1被确定为通过一条线连接到先前点([190670.0; 149600.0], 0)的点通过一条线连接到第二点。

我将其编码如下:

using  JuMP,Plots

list=[([0.0, 583000.0], 0), ([24556.0, 503800.0], 0), ([33296.0, 484000.0],1), ([47462.0, 437800.0], 1), ([69686.0, 385000.0], 1), ([129686.0, 253000.0], 1), ([136554.0, 23805.0], 1), ([164278.0, 178200.0], 1), ([190670.0, 149600.0], 0)]

x=zeros(1,1)
for i=1:size(list,1)
    x=[x;list[i][1][1]]
end
row=1
x = x[setdiff(1:end, row), :]

y=zeros(1,1)
for i=1:size(list,1)
    y=[y;list[i][1][2]]
end
row=1
y = y[setdiff(1:end, row), :]

for i=2:size(list,1)
    if list[i][2]==0
        plot(Int(x[i]),Int(y[i]),seriestype=:scatter)
        plot(Int(x[i+1]),Int(y[i+1]),seriestype=:scatter)
    end
    if list[i][2]==1
        plot(Int(x[i]),Int(y[i]))
        plot(Int(x[i+1]),Int(y[i+1]))
    end
end

,但不起作用。请你帮我一下。 谢谢

1 个答案:

答案 0 :(得分:1)

您只需在下面的代码中将每个线段的x和y值推入两个单独的数组xy中即可。在每个线段的值(即x1和x2或y1和y2)之后,将NaN放入数组中。如果不存在连接,这将防止将线段连接到下一个线段。 (例如,看到1,然后看到0的情况)。最后是plot(x, y)

以下代码段可以实现。请注意,allxally用于保存所有点,而不管连接状态如何。您可能要从它们中排除连接的点。 xy保持相连的线段。

using Plots
x, y, allx, ally = Float64[], Float64[], Float64[], Float64[]

# iterate through list
for i = 1:length(list)-1
    if list[i+1][2] == 1
        # push x1 from the first point, x2 from the second and a `NaN`
        push!(x, list[i][1][1], list[i+1][1][1], NaN)
        # push y1, y2, `NaN`
        push!(y, list[i][1][2], list[i+1][1][2], NaN)
    end
    push!(allx, list[i][1][1])
    push!(ally, list[i][1][2])
end
push!(allx, list[end][1][1])
push!(ally, list[end][1][2])
# scatter all points
scatter(allx, ally)
# plot connections with markers
plot!(x, y, linewidth=2, color=:red, marker=:circle)

这有望给您想要的情节。


如果您碰巧使用Gadfly.jl而不是Plots.jl,则可以使用

获得类似的图
using Gadfly
connectedpoints = layer(x=x, y=y, Geom.path, Geom.point, Theme(default_color="red"))
allpoints = layer(x=allx, y=ally, Geom.point)

plot(connectedpoints, allpoints)

作为旁注,如果您打算在已创建的绘图对象之上绘制另一个系列,则应使用plot!而不是plot