Skip to content

WIP: add some problems from vOptLib #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

WIP: add some problems from vOptLib #115

wants to merge 4 commits into from

Conversation

odow
Copy link
Member

@odow odow commented Jun 12, 2025

x-ref #113

module UKP

using JuMP
import JSON

const ROOT = joinpath(@__DIR__, "UKP")

function get_next_line(io)
    while !eof(io)
        line = readline(io)
        if isempty(line) || startswith(line, "#")
            continue
        end
        return line
    end
    return nothing
end

function build_model(filename)
    C, w, W = open(filename, "r") do io
        N = parse(Int, get_next_line(io))
        P = parse(Int, get_next_line(io))
        K = parse(Int, get_next_line(io))
        @assert K == 1
        C = zeros(P, N)
        for p in 1:P
            for i in 1:N
                C[p, i] = parse(Float64, get_next_line(io))
            end
        end
        w = zeros(N)
        for i in 1:N
            w[i] = parse(Float64, get_next_line(io))
        end
        W = parse(Float64, get_next_line(io))
        return (C, w, W)
    end
    P, N = size(C)
    model = Model()
    @variable(model, x[1:N], Bin)
    @constraint(model, w' * x <= W)
    @objective(model, Max, C * x)
    return model
end

function main()
    instances = ["2KP50-11", "2KP50-50", "2KP50-92", "2KP100-50"]
    for instance in instances
        dat_filename = joinpath(ROOT, "instances", "1A", instance * ".dat")
        model = build_model(dat_filename)
        write_to_file(model, joinpath("instances/models", instance * ".mof.json"))
        x_filename = joinpath(ROOT, "X", instance * ".max")
        solutions = Dict{String,Any}[]
        open(x_filename, "r") do io
            for line in readlines(io)
                items = split(line)
                X = ifelse.(codeunits(items[end]) .== UInt8('1'), 0, 1)
                Y = parse.(Float64, items[1:end-1])
                push!(solutions, Dict("X" => X, "Y" => Y))
            end
        end
        solution_filename = joinpath("instances/solutions", instance * ".json")
        write(solution_filename, JSON.json(solutions))
    end
    return
end

end  # module UKP

UKP.main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant