Skip to content

Instantly share code, notes, and snippets.

Dirk Schumacher dirkschumacher

Block or report user

Report or block dirkschumacher

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View splice.md
splice_df <- function(x, ...) {
  expr <- rlang::enquo(x)
  cols <- lapply(rlang::ensyms(..., .named = TRUE), as.character)
  lapply(cols, function(col_name) {
    rlang::quo(`[[`(!!expr, !!col_name))
  })
}
View rhxl-hdx.R
# this is just a script to test the rhxl package, I just quickly looked at the data
# Ethiopia Who is doing What Where - 3W December 2017
# source: https://data.humdata.org/dataset/3w-december-2017
url <- "https://data.humdata.org/dataset/615416d2-457b-461a-8155-090f0ced0bf8/resource/f71bf111-8706-42f4-ba46-4ce3c8c949dc/download/3w_hxl.xlsx"
# load the rhxl package
# https://github.com/dirkschumacher/rhxl
library(rhxl)
download.file(url, "file.xlsx")
View lm_tf.R
# Linear regression with tensorflow and R
library(tensorflow)
# Y = X * beta + epsilon
# =>
# beta = (X'X)^-1X'y
# first we build the computational graph
X <- tf$placeholder(tf$float64, name = "X")
View benchmark-tsp.R
# There are essential two prominent ways to model a TSP as a MILP. One is to formulate the full model using the Miller–Tucker–Zemlin (MTZ) formulation and the other option is to use the so-called sub-tour elimination constraints .[1](https://www.unc.edu/~pataki/papers/teachtsp.pdf)
#
# The first formulation is fairly compact (quadratic many constraints and variables) but is not suitable anymore when n gets larger. The second formulation has exponential many constraints at most, but can solve larger TSPs due to the better LP relaxation. The idea of the latter approach is add constraints to the model *during* the solution process as soon as a solution was found that contains a sub-tour. For solution strategies like this solvers usually offer callbacks that let's you modify the model during the the branch-and-cut process - this is however not currently supported by `ompr`.
#
# Therefor we will use the MTZ formulation and solve a fairly small TSP.
library(ompr)
library(magrittr)
View 4-color-world.R
# based on a article from here https://dirkschumacher.github.io/ompr/articles/problem-graph-coloring.html
library(maptools)
library(dplyr)
# devtools::install_github("dirkschumacher/ompr@milp")
# CC by
map_data <- rgdal::readOGR("https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_50m_admin_0_countries.geojson", "OGRGeoJSON")
View ompr_milp.md

This vignettes discribes the modelling techniques available in ompr to make your life easier when developing a mixed integer programming model.

A MILP Model

You can think of a MIP Model as a big constraint maxtrix and a set of vectors. But you can also think of it as a set of decision variables, an objective function and a number of constraints as equations/inequalities. ompr implements the latter approach.

View ompr-benchmark.R
# based on the formulation from here
# http://wwwhome.math.utwente.nl/~uetzm/do/IP-FKS.pdf
library(magrittr)
# devtools::install_github("dirkschumacher/ompr@milp")
library(ompr)
max_colors <- 10
n <- 100
# variables n * max_colors
View 4-color-berlin.R
# based on a article from here https://dirkschumacher.github.io/ompr/articles/problem-graph-coloring.html
library(maptools)
library(dplyr)
# CC by
# license here https://github.com/berlinermorgenpost/Berlin-Geodaten
map_data <- rgdal::readOGR("https://github.com/berlinermorgenpost/Berlin-Geodaten/raw/master/berlin_bezirke.geojson", "OGRGeoJSON")
# this gives as an adjancy list
View disease_video.R
# LICENSE MIT
# Data from RKI with special Terms
library(dplyr)
library(readr)
library(tidyr)
# go to https://survstat.rki.de/Content/Query/Create.aspx
# Selected calendar weeks for rows and diseases for columns.
# had to manually edit, because not a valid csv
@dirkschumacher
dirkschumacher / 20170509_bug_roi.R
Created May 9, 2017
Bounds on binary variables seem to be ignored in ROI (ROI_0.2-1)
View 20170509_bug_roi.R
# Bug report
# It seems variable bounds are ignored when using binary variables "B".
# It works as expected if variables of type "I" are used
library(ROI)
library(ROI.plugin.glpk)
# max: x1 + x2 + x3 + x4
# s.t. x1 + x2 + x3 + x4 <= 4
You can’t perform that action at this time.