njtierney / maxcovr

Tools in R to make it easier to solve the Maximal Coverage Location Problem
http://maxcovr.njtierney.com/
GNU General Public License v3.0
42 stars 11 forks source link

Implement the gurobi solver: current progress #25

Open njtierney opened 7 years ago

njtierney commented 7 years ago

else if(solver == "gurobi"){

    # In bar.R
        if (!requireNamespace("gurobi", quietly = TRUE)) {
            stop("Make sure that you have installed the Gurobi software and accompanying Gurobi R package, more details at https://www.gurobi.com/documentation/7.0/refman/r_api_overview.html")

    }
    # model <- list()
    # model$A          <- matrix(c(1,1,0,0,1,1), nrow=2, byrow=T)
    # model$obj        <- c(1,1,2)
    # model$modelsense <- "max"
    # model$rhs        <- c(1,1)
    # model$sense      <- c('<=', '<=')

    # J <- nrow(A)
    # I <- ncol(A)

    model <- list()

    # set A matrix
    model$A <- A

        Nx <- nrow(A)
        Ny <- ncol(A)
        N <- n_added

        # d <- [ones(1,Ny) zeros(1,Nx)];
        d <- c(rep(1, Ny), rep(0,Nx))
    # c <- -[zeros(Ny,1); ones(Nx,1)];
        # c <- c(rep(0, Ny), rep(1,Nx))
    model$obj <- c(rep(0, Ny), rep(1,Nx))

    model$modelsense <- "max"

        Aeq <- d

        beq <- N

        Ain <- cbind(-A, diag(Nx))

        bin <- matrix(rep(0,Nx), ncol = 1)

    # matrix of numeric constraint coefficients,
    # one row per constraint
    # one column per variable
        # constraint matrix??
    # constraint_matrix <- rbind(Ain, Aeq)
    # rhs_matrix <- rbind(bin, beq)
        model$rhs <- rbind(bin, beq)

    # model$sense      <- c('<=', '<=')
    model$sense <- c(rep("<=", Nx), "==")

    result <- gurobi::gurobi(model)

    return(result)

} # end gurobi
njtierney commented 7 years ago

Gurobi now works, however there are some remaining issues in #39, but these can be addressed there directly.