Skip to content

Instantly share code, notes, and snippets.

Avatar
🚲

Dirk Schumacher dirkschumacher

🚲
View GitHub Profile
View optim_torch.md
library(torch)
optim_torch <- function(params, fn, method, iterations = 1000, ...) {
  optimizer <- do.call(paste0("optim_", method), list(params, ...))
  for (i in seq_len(iterations)) {
    obj_val <- fn(params)
    if (i %% 100 == 0) message(as.numeric(obj_val))
    optimizer$zero_grad()
    obj_val$backward()
    optimizer$step()
View dependencies.R
# investigate your project or package dependencies
# using the {tidyverse}, {renv} and {tools}.
# since this code is wrapped in {reprex}, the below library calls
# will be identified as dependent packages although only {tidyverse}, {renv}
# and {tools} are actually used.
library(tidyverse, warn.conflicts = FALSE) # ironically the most deps :)
# add some more dependencies as an example
suppressPackageStartupMessages(library(caret, warn.conflicts = FALSE))
suppressPackageStartupMessages(library(tidymodels, warn.conflicts = FALSE))
View role_based_access_logician.md

Role based access control as a logic program

Inspired by this article

# remotes::install_github("dirkschumacher/logician")
# a role based access control
library(logician)
role_database <- logician_database(
  role(user),
View tower_of_hanoi.R
library(logician)
# Towers of Hanoi
# https://en.wikipedia.org/wiki/Tower_of_Hanoi
# Original Prolog Implementation:
# https://www.cs.toronto.edu/~sheila/384/w11/simple-prolog-examples.html
database <- logician_database(
# no `is` operator yet, so we have to define all valid numbers
num(1),num(2),num(3),num(4),num(5),
num(6),num(7),num(8),num(9),
View test.py
from l0bnb import fit_path
from l0bnb import gen_synthetic
X, y, b = gen_synthetic(n=10, p=10, supp_size=10)
sols = fit_path(X, y, lambda_2 = 0.01, max_nonzeros = 1)
print(sols)
View group_by_summarise.md
local({
  # a quick, just for fun base R implementation of group_by/summarise/`%>%`
  # many edge cases not covered
  # also group_by does not produce a data.frame with the same shape
  # as the input
  group_by <- function(data, ...) {
    exprs <- substitute(list(...))
    grouping_cols <- vapply(exprs[-1], as.character, character(1))
View blake3.md
library(blake3)
library(digest)
input <- charToRaw(paste0(sample(LETTERS, 1e6, replace = TRUE), collapse = ""))
microbenchmark::microbenchmark(
  blake3 = sodium::bin2hex(blake3_hash_raw(input)),
  sha1 = digest(input, "sha1", serialize = FALSE),
  md5 = digest(input, "md5", serialize = FALSE),
  sha256 = digest(input, "sha256", serialize = FALSE),
  osha1 = openssl::sha1(input),
View large.R
``` r
partition <- function(groups_vector, n_shards) {
stopifnot(is.integer(groups_vector))
group_sizes <- sort(table(groups_vector), decreasing = TRUE)
n_groups <- length(group_sizes)
stopifnot(n_groups > n_shards)
View sparse_model.md
# Build sparse models with filter guards
# this problem arises in network models where you have a variable for each
# pair of nodes. However if your graph is not fully connected, you end up
# creating a lot of useless variables if you have all combinations in your MIP Model
# and then set the invalid edges to 0.
# Below is a toy example with 1 millionen edges, but only 36 are actually being used.
library(rmpk)
is_adjacent <- function(i, j) {
  i < j & j < 10 # just a dummy function indicating when two nodes are adjacent
View subtour_tsp_r.md
# an example of the TSP solved through solver callbacks
# follows the formulation of the Gurobi example
# http://examples.gurobi.com/traveling-salesman-problem/
# and from the TSP vignette for the MTZ formulation

# all experimental

library(ggplot2)
suppressPackageStartupMessages(library(dplyr))
You can’t perform that action at this time.