Skip to content

Instantly share code, notes, and snippets.

@ianfiske
Created May 24, 2019 13:30
Show Gist options
  • Save ianfiske/acbc4f39216b753bfea01f7b66a95f64 to your computer and use it in GitHub Desktop.
Save ianfiske/acbc4f39216b753bfea01f7b66a95f64 to your computer and use it in GitHub Desktop.
Test Module for Parametron
module TestParametron
# create a MathOptInterface optimizer instance
using OSQP
optimizer = OSQP.Optimizer()
# create a Parametron.Model, which holds problem information
using Parametron
using Random, LinearAlgebra
function model()
model = Model(optimizer)
# create decision variables and parameters
n = 8; m = 2
x = [Variable(model) for _ = 1 : n]
A = Parameter(rand!, zeros(n, n), model)
b = Parameter(rand!, zeros(n), model)
C = Parameter(rand!, zeros(m, n), model)
d = Parameter(zeros(m), model) do d
# do syntax makes it easy to create custom Parameters
rand!(d)
d .*= 2
end
# the @expression macro can be used to create 'lazy' expressions,
# which can be used in constraints or the objective function, and
# can be evaluated at a later time, automatically updating the
# Parameters in the process (if needed).
residual = @expression A * x - b
# set the objective function
@objective(model, Minimize, residual ⋅ residual)
# add the constraints. You could have multiple @constraint calls
# as well. ==, <=, and >= are supported.
@constraint(model, C * x == d)
model
end
end
include("parametron_test_module.jl")
using BenchmarkTools
using Parametron
model = TestParametron.model()
Parametron.solve!(model)
@btime Parametron.solve!(model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment