# ==============================================================================
# Xavier Gandibleux - November 2021
#   Implemented in Julia 1.6

# ==============================================================================
# Parser of MO-MKP instances generated by E. Zitzler and M. Laumanns available at
# https://sop.tik.ee.ethz.ch/download/supplementary/testProblemSuite/?page=testProblem.php#source
#
#   NB1: Number of objectives = number of constraints
#   NB2: All coefficients P, W are integer, all variables are binary
#   NB3: All coefficients ω are floored to the integer

# ==============================================================================
# Datastructure of a multi-objective multi-dimensionnal KP with 0/1 variables
struct _MOMKP
    P  :: Matrix{Int} # profit of items for the objectives, k=1..p, j=1..n
    W  :: Matrix{Int} # weight of items for the constraints, i=1..m, j=1..n
    ω  :: Vector{Int} # capacity of knapsacks, i=1..m
end

# ==============================================================================
function readInstanceMOMKPformatZL(verbose::Bool, fname::String)

    f=open(fname)
        lines = readlines(f)
    close(f)

    # Extract the problem dimensions from the file name ------------------------
    line=(split(fname,"."))
    #line = split(lines[1]," ")
    m = parse(Int, line[3])
    p = m
    n = parse(Int, line[2])
    momkp = _MOMKP(zeros(Int,p,n),zeros(Int,m,n),zeros(Int,m))
    !verbose ? nothing : println("Instance of MKP : ",m," couples objective/constraint and ",n," variables")

    # Extract the data of the MKP from the following lines ---------------------
    i=0; j=0
    for l = 2:length(lines)
        if occursin("knapsack ", lines[l])
            i = i + 1
            j = 0
            !verbose ? nothing : println("Couple objective/constraint n°",i)
        elseif occursin("capacity", lines[l])
            line = split(lines[l],"+")
            momkp.ω[i] = floor(Int, parse(Float64, line[2]))
            !verbose ? nothing : println("ω[",i,"]=",momkp.ω[i])
        elseif occursin("item", lines[l])
            j = j + 1
        elseif occursin("weight", lines[l])
            line = split(lines[l],"+")
            momkp.W[i,j] = parse(Int, line[2])
            !verbose ? nothing : println("w[",i,",",j,"]=",momkp.W[i,j])
        elseif occursin("profit", lines[l])
            line = split(lines[l],"+")
            momkp.P[i,j] = parse(Int, line[2])
            !verbose ? nothing : println("p[",i,",",j,"]=",momkp.P[i,j])
        end
    end

    return momkp
end

# ==============================================================================

# Example on how to use it :
# fname="knapsack.100.3"; verbose = true
# instanceZitzler = readInstanceMOMKPformatZL(verbose,fname)
