text:
function generate_data(n, seed, M, density, constraint_uncertainty)
  function random_conflicts()
    E = Vector{Set}()
    D = 0
    while D < density
      D = 2*length(E)/(n*(n-1))
      new_edge = Set(rand(1:n,2))
      if !∈(new_edge,E) && length(new_edge) == 2
        push!(E,new_edge)
      end
    end
    return E
  end

  function diamond_uniform(n)
      v = rand(n-1)
      v = [0; sort(v); 1]
      w = [v[i]-v[i-1] for i in 2:(n+1)]
      invert = rand(n)
      for i in 1:n if invert[i] ≤ 0.5 w[i] = -w[i] end end
      return w
  end

  Random.seed!(seed)
  to_print = []
  push!(to_print,"InstanceID")
  push!(to_print,"$(seed)_$(n)_$M")
  push!(to_print,"NumObjects")
  push!(to_print,n)
  push!(to_print,"NumFactors")
  push!(to_print,M)
  push!(to_print,"Weights")
  c = 100*rand(n)
  for i in 1:n
    push!(to_print,c[i])
  end
  push!(to_print,"Budget")
  push!(to_print,sum(c)/2)
  push!(to_print,"Profits")
  for i in 1:n
    push!(to_print,-c[i]/5)
  end
  push!(to_print,"FactorLoading_Φ")
  Φ = Matrix{Float64}(undef, 0, M)
  for i in 1:n
    Φ = [Φ; diamond_uniform(M)']
  end
  for i in 1:n
    push!(to_print,Φ[i,:])
  end
  if constraint_uncertainty
    push!(to_print,"FactorLoading_Ψ")
    Ψ = Matrix{Float64}(undef, 0, M)
    for i in 1:n
      Ψ = [Ψ; diamond_uniform(M)']
    end
    for i in 1:n
      push!(to_print,Φ[i,:])
    end
  end
  push!(to_print,"Conflicts")
  conflicts = random_conflicts()
  for (i,j) in conflicts
    push!(to_print,"$i $j")
  end
  if constraint_uncertainty writedlm(string(dirname(@__FILE__),"/../data/KPneg_weightuncertainty/$(seed)_$(n)_$(M)_$(density).txt"),to_print)
  else writedlm(string(dirname(@__FILE__),"/../data/KPneg/$(seed)_$(n)_$(M)_$(density).txt"),to_print)
  end
end

#-----------------------------------------------------------------------------------

function read_data(instance::AbstractString, constraint_uncertainty::Bool)
  if constraint_uncertainty file = readdlm(string(dirname(@__FILE__),"/../data/KPneg_weightuncertainty/",instance))
  else file = readdlm(string(dirname(@__FILE__),"/../data/KP/",instance))
  end
  @info "read instance $(file[2,1])"
  data = Data()
  data.n = file[4,1]
  data.M = file[6,1]
  data.w = file[8:8+data.n-1,1]
  data.B = Vector{Float64}(undef,1)
  data.B[1] = file[8+data.n+1,1]
  data.hc = file[11+data.n:11+2*data.n-1,1]
  data.Φ = file[12+2*data.n:11+3*data.n,1:4]
  if constraint_uncertainty
    data.ncons = 1
    data.Ψ = file[13+3*data.n:12+4*data.n,1:4]
    data.constraint_uncertainty = true
  else
    data.ncons = 0
    data.constraint_uncertainty = false
  end
  line = findfirst(file.=="Conflicts")[1]
  data.conflicts = Set.([file[i,1:2] for i in (line+1):size(file)[1]])
  data.compatibility = trues(data.n,data.n)
  for (i,j) in data.conflicts
    data.compatibility[i,j] = false
    data.compatibility[j,i] = false
  end
  data.E = Vector{Vector{Int64}}(undef, data.n)
  for i in 1:data.n
    data.E[i] = findall(x->x!=1,data.compatibility[i,:])
  end
  return data
end

#-----------------------------------------------------------------------------------

mutable struct Data
  n::Int
  M::Int
  B::Vector{Float64} # vector of uncertain rhs. contains one element in this application.
  E::Vector{Vector{Int64}}
  w::Vector{Float64}
  hc::Vector{Float64}
  Φ::Matrix{Float64}
  Ψ::Matrix{Float64}
  conflicts::Vector{Set}
  compatibility::Matrix{Bool}
  ncons::Int #number of uncertain constraints. equal to 1 in this application.
  constraint_uncertainty::Bool
  heuristic_ub::Float64
  Data() = new()
end

#-----------------------------------------------------------------------------------

function h(x)
  return 1
end

#-----------------------------------------------------------------------------------

function generate_solutions(ub)
  function gen_sol(x::BitArray,i::Int64,w::Float64,c::Float64,tΦ::Array{Float64,1},tΨ::Array{Float64,1},admissible::BitArray)
    time_elapsed = time() - starting_time
    if time_elapsed < TIME_LIM
      if i == data.n
        if c - sum(abs.(tΦ)) < ub # filter by optimality
          push!(X,copy(x))
          @timeit to "save data for dominance" begin
            push!(all_tΦ,copy(tΦ))
            push!(all_c,copy(c))
            if data.constraint_uncertainty
              push!(all_tΨ,copy(tΨ))
              push!(all_w,copy(w))
            end
          end
        end
      else
        i = i+1
        x[i] = false
        gen_sol(x,i,w,c,tΦ,tΨ,admissible)
        x[i] = true
        if admissible[i] # filter by conflicts constraints
          if w - sum(abs.(tΨ)) ≤ data.B[1] # filter by capacity constraint
            tΦ += 0.5 * data.Φ[i,:] .* data.hc[i]
            if data.constraint_uncertainty
              tΨ += 0.5 * data.Ψ[i,:] .* data.w[i]
            end
            c += data.hc[i]
            w += data.w[i]
            next_admissible = copy(admissible)
            next_admissible[i+1:end] = admissible[i+1:end].* data.compatibility[i,i+1:end]
            gen_sol(x,i,w,c,tΦ,tΨ,next_admissible)
          end
        end
      end
    end
  end
  starting_time = time()
  X = Vector{BitArray{1}}() # set of all feasible vectors for the knapsack
  # The following values are used for dominance below
  all_tΨ = Vector{Vector{Float64}}()
  all_tΦ = Vector{Vector{Float64}}()
  all_c = Vector{Float64}()
  all_w = Vector{Float64}()
  x = falses(data.n)
  i = 0
  w = 0.0
  c = 0.0
  tΦ = zeros(data.M)
  tΨ = zeros(data.M)
  admissible = trues(data.n)
  println("Computing X")
  gen_sol(x,i,w,c,tΦ,tΨ,admissible)
  println("|X|=$(length(X))")

  non_dom = trues(length(X)) # given x ∈ X, if ∃ y ∈ X : y ⊃ x, then x is dominated by y

  # Next we perform dominance
  @timeit to "dominance" begin
    if data.constraint_uncertainty
      for i in 1:length(X)
        tΨ = all_tΨ[i]
        tΦ = all_tΦ[i]
        c = all_c[i]
        w = all_w[i]
        for j in 1:length(X)
          # We remove solution i if there exists a solution j that is always feasible and chaper than solution i
          # this is only a sufficient condition for dominance. A less restrictive condition would only check that the subset of Ξ
          # on which j is feasible is contained in that corresponding for i
          if all_c[j] - c + sum(abs.(all_tΦ[j] - tΦ)) < 0 && all_w[j] + sum(abs.(all_tΨ[j])) ≤ data.B[1]
            non_dom[i] = false
            break
          end
        end
      end
    else
      for i in 1:length(X)
        tΦ = all_tΦ[i]
        c = all_c[i]
        for j in 1:length(X)
          # We remove solution i if there exists a solution j that is always chaper than solution i. This is exatly what strong dominance means
          if all_c[j] - c + sum(abs.(all_tΦ[j] - tΦ)) < 0
            non_dom[i] = false
            break
          end
        end
      end
    end
  end
  X = X[findall(non_dom)]
  println("After dominance check: |X|=$(length(X))")
  return X
end

#-----------------------------------------------------------------------------------

function generate_neighbors(sol,aa)
  X = Vector{BitArray{1}}()
  for k in 1:size(sol,1)
    x = falses(data.n)
    for j in N
      if sol[k,j]>0 x[j]=true end
    end
    push!(X,x)
    sol_items=findall(x)
    for i in sol_items
      admissible=trues(data.n)
      others = setdiff(sol_items,i)
      for o in others
        admissible[findall(x->x==false,data.compatibility[o,:])].=false
      end
      for iprime in setdiff(findall(admissible),sol_items)
        y = copy(x)
        if !data.constraint_uncertainty
          # check the knapsack constraint is satisfied
          if data.B[1] - sum(data.w[s] for s in sol_items)+data.w[i]-data.w[iprime]>0
            y[i]=false
            y[iprime]=true
            push!(X,y)
          end
        else
          # add only solutions that satisfy the knapsack constraint for all scenarios
          # generated so far
          if data.B[1] - maximum(sum(aa[1][:,s] for s in sol_items) + aa[1][:,i] - aa[1][:,iprime])>0
            y[i]=false
            y[iprime]=true
            push!(X,y)
          end
        end
      end
    end
  end
  return X
end

#-----------------------------------------------------------------------------------

"Add the constraints forming X"
function add_constraints_X(model::Model, x::Array{VariableRef,1})
  if !data.constraint_uncertainty  # the knapsack constraint is in X
    @constraint(model, sum(data.w[i]*x[i] for i in N) ≤ data.B[1])
  end
  if formulation == "AGG"
    @constraint(model,[i in 1:data.n],length(data.E[i])*x[i]+sum(x[j] for j in data.E[i])≤ length(data.E[i]))
  elseif formulation == "CONF"
    @constraint(model,[i in 1:length(data.conflicts)],x[collect(data.conflicts[i])[1]]+x[collect(data.conflicts[i])[2]] <= 1)
  end
end

#-----------------------------------------------------------------------------------

"Create aa and cc with k rows"
function populate_scenarios()
  # Initialize the data structures that will contain the effects
  # of uncertainty on the objective function (cc) and constraints (aa)
  cc = Array{Float64,2}(undef,1,data.n)
  aa = Vector{Array{Float64,2}}(undef,1)
  aa[1] = Array{Float64,2}(undef,1,data.n)
  for l in N cc[1,l] = data.hc[l] end
  for l in N aa[1][1,l] = data.w[l] end
  return aa, cc
end

#-----------------------------------------------------------------------------------

function add_scenario(δ)
  new_cost = [(1+sum(data.Φ[i,j]*δ[j] for j in 1:data.M)/2)data.hc[i] for i in N]
  if data.constraint_uncertainty
    new_weights = [ [(1+sum(data.Ψ[i,j]*δ[j] for j in 1:data.M)/2)data.w[i] for i in N] for cons in 1:1 ] # only one constraint is considered
  else
    new_weights = undef
  end
  return new_cost, new_weights
end

#-----------------------------------------------------------------------------------

function build_and_solve_separation(x, v)
  if !data.constraint_uncertainty
    # Compute the worst-case cost:
    # max{η : ξ \in Ξ, η ≤ f(ξ,x^k), ∀ k in [k]}
    time_remaining = TIME_LIM - (time()-STARTTIME)
    S = create_model(max(0,time_remaining))
    @variable(S, η)
    @variable(S, -1 ≤ ξ[1:data.M] ≤ 1)
    @constraint(S, sep_cost[k in K], η <= sum((1+sum(data.Φ[i,j]*ξ[j] for j in 1:data.M)/2)data.hc[i]*x[k,i] for i in N))
    @objective(S, Max, η)
    @timeit to "objective" optimize!(S)
  else
    # Compute S(v,x) as described in Appendix A
    time_remaining = TIME_LIM - (time()-STARTTIME)
    L = data.ncons
    S = create_model(max(0,time_remaining))
    @variable(S, z[k in K,0:L], Bin)
    @variable(S, -1 ≤ ξ[1:data.M] ≤ 1)
    @variable(S, η)
    @constraint(S, [k in K], sum(z[k,ℓ] for ℓ in 0:L) == 1)
    @constraint(S, [k in K], z[k,0] => { η ≤ sum((1+sum(data.Φ[i,j]*ξ[j] for j in 1:data.M)/2)data.hc[i]*x[k,i] for i in N) - v } )
    @constraint(S, [k in K, ℓ in 1:L], z[k,ℓ] => { η ≤ sum((1+sum(data.Ψ[i,j]*ξ[j] for j in 1:data.M)/2)data.w[i]*x[k,i] for i in N) - data.B[ℓ] } )
    @objective(S, Max, η)
    @timeit to "constraint" optimize!(S)
  end
  if termination_status(S) == MOI.OPTIMAL
    # we return the scenario generated by S, namely ξ
    # its objective value will be used as new UB only if feasible = true
    return value.(ξ),objective_value(S)
  else
    return [],-Inf
  end
end

#-----------------------------------------------------------------------------------

function compute_basic_ub(x)
  if data.constraint_uncertainty
    return 0
  else
    return 0.5*sum(data.hc[i]*x[i] for i in N) + 0.5*sum(abs(sum(data.Φ[i,j]*data.hc[i]*x[i] for i in N)) for j in 1:data.M)
  end
end

#-----------------------------------------------------------------------------------

"This is the local search heuristic proposed in André B. Chassein, Marc Goerigk, Jannis Kurtz, Michael Poss:
Faster algorithms for min-max-min robustness for combinatorial problems with budgeted uncertainty. Eur. J. Oper. Res. 279(2): 308-319 (2019)"

function heuristic_dualization()
  N = 1:data.n
  heursol = +Inf
  alphas = zeros(k)
  for j in K
    alphas[j] = (2*j)/((k)*(k+1))
  end
  timexstep = 0
  timealphastep = 0
  while true
    xstep = create_model(max(0,TIME_LIM-timexstep-timealphastep))
    @variable(xstep, 0 ≤ u[j in 1:data.M])
    @variable(xstep, 0 ≤ v[j in 1:data.M])
    @variable(xstep, x[k in K, i in N], Bin, container=Array)
    @objective(xstep,  Min, sum(sum(data.hc[i]*x[k,i]*alphas[k] for i in N) for k in K) + sum(v[j]+u[j] for j in 1:data.M))
    @constraint(xstep, dualconst[j in 1:data.M], u[j] - v[j] == sum(sum(data.Φ[i,j]*data.hc[i]/2*x[k,i] for i in N)*alphas[k] for k in K))
    for k in K add_constraints_X(xstep,x[k,:]) end
    timexstep += @elapsed optimize!(xstep)
    if primal_status(xstep) == MOI.FEASIBLE_POINT
      global xsol = value.(x)
      if heursol <= objective_value(xstep) break end
      heursol = objective_value(xstep)
    end
    αstep = create_model(max(0,TIME_LIM-timexstep-timealphastep))
    @variable(αstep, 0 ≤ u[j in 1:data.M])
    @variable(αstep, 0 ≤ v[j in 1:data.M])
    @variable(αstep, 0 ≤ α[k in K])
    @objective(αstep, Min, sum(sum(data.hc[i]*xsol[k,i]*α[k] for i in N) for k in K) + sum(v[j]+u[j] for j in 1:data.M))
    @constraint(αstep, dualconst[j in 1:data.M], u[j] - v[j] == sum(sum(data.Φ[i,j]*data.hc[i]/2*xsol[k,i] for i in N)*α[k] for k in K))
    @constraint(αstep, sum(α[j] for j in K) == 1)
    timealphastep += @elapsed optimize!(αstep)
    if primal_status(αstep) == MOI.FEASIBLE_POINT
      alphas=value.(α)
    end
    if timexstep+timealphastep > TIME_LIM break end
  end
  return heursol,xsol
end

#-----------------------------------------------------------------------------------

"This is the monolithic reformulation from Grani Adiwena Hanasusanto, Daniel Kuhn, Wolfram Wiesemann:
K-Adaptability in Two-Stage Robust Binary Programming. Oper. Res. 63(4): 877-891 (2015)"

function exact_dualization()
  dualized = create_model(TIME_LIM)
  @variable(dualized, 0 ≤ u[j in 1:data.M])
  @variable(dualized, 0 ≤ v[j in 1:data.M])
  @variable(dualized, 0 ≤ α[k in K] ≤ 1)
  @variable(dualized, 0 ≤ ρ[k in K,i in N] ≤ 1)
  @variable(dualized, x[k in K, i in N], Bin,container=Array)
  for k in K  add_constraints_X(dualized,x[k,:]) end
  @constraint(dualized, sum(α[k] for k in K) == 1)
  @constraint(dualized,[j in 1:data.M], u[j] - v[j] == sum(sum(data.Φ[i,j]*data.hc[i]/2*ρ[k,i] for i in N) for k in K))
  @constraint(dualized,[i in N,k in K], ρ[k,i] ≤ x[k,i])
  @constraint(dualized,[i in N,k in K], ρ[k,i] ≤ α[k])
  @constraint(dualized,[i in N,k in K], ρ[k,i] ≥ x[k,i]+α[k]-1)
  @objective(dualized,Min, sum(sum(data.hc[i]*ρ[k,i] for i in N) for k in K) + sum(u[j]+v[j] for j in 1:data.M))
  optimize!(dualized)
  return objective_value(dualized),MOI.get(dualized, MOI.RelativeGap()), value.(x)
end

#-----------------------------------------------------------------------------------

"The heuristic constraint generation requires a starting upper bound, obtained through a classical
dualized model."

function solve_static()
  dualized = create_model(TIME_LIM)
  @variable(dualized, 0 ≤ u[j in 1:data.M])
  @variable(dualized, 0 ≤ v[j in 1:data.M])
  @variable(dualized, x[i in N], Bin,container=Array)
  add_constraints_X(dualized,x)
  @constraint(dualized,[j in 1:data.M], u[j] - v[j] == sum(data.Φ[i,j]*data.hc[i]/2*x[i] for i in N))
  if data.constraint_uncertainty
    @variable(dualized, 0 ≤ u_weight[j in 1:data.M])
    @variable(dualized, 0 ≤ v_weight[j in 1:data.M])
    @constraint(dualized,[j in 1:data.M], u_weight[j] - v_weight[j] == sum(data.Ψ[i,j]*data.w[i]/2*x[i] for i in N))
    @constraint(dualized, sum(data.w[i]*x[i] for i in N) + sum(u_weight[j]+v_weight[j] for j in 1:data.M) ≤ data.B[1])
  end
  @objective(dualized,Min, sum(data.hc[i]*x[i] for i in N) + sum(u[j]+v[j] for j in 1:data.M))
  optimize!(dualized)
  return objective_value(dualized)
end

max_stars_repo_name: mjposs/min-max-min

max_issues_repo_name: mjposs/min-max-min

max_forks_repo_name: mjposs/min-max-min


text:
using Documenter, Fracciones
makedocs(sitename="Fracciones.jl")

ext: jl


text:
using PtFEM

data = Dict(
  # GenericSolid(ndim, nst, nels, nn, nip, finite_element(nod, nodof), axisymmentrix)
  :struc_el => GenericSolid(3, 6, 6, 8, 1, Tetrahedron(3), false),
  :properties => [100.0 0.3 0.0;],
  :g_coord => [
    0.0 0.0 0.0; 1.0 0.0 0.0; 0.0 0.0 -1.0; 1.0 0.0 -1.0;
    0.0 1.0 0.0; 1.0 1.0 0.0; 0.0 1.0 -1.0; 1.0 1.0 -1.0],
  :g_num => [
    1, 3, 4, 7,
    1, 4, 2, 7,
    1, 2, 5, 7,
    6, 4, 8, 7,
    6, 2, 4, 7,
    6, 5, 2, 7
  ],
  :support => [
    (1, [0 0 1]), (2, [1 0 1]), (3, [0 0 0]), (4, [1 0 0]),
    (5, [0 1 1]), (7, [0 1 0]), (8, [1 1 0])
  ],
  :loaded_nodes => [
    ( 1, [0.0 0.0 -1/6]),
    ( 2, [0.0 0.0 -1/3]),
    ( 5, [0.0 0.0 -1/3]),
    ( 6, [0.0 0.0 -1/6])
  ]
)

@time m = p54(data)
println()


text:
module Interfaces

using BinaryTraits
using BinaryTraits: SyntaxError, extract_type
using BinaryTraits.Prefix: Can, Is, Has
using Test

const SUPPORT_KWARGS = VERSION >= v"1.2"
const mod = @__MODULE__

struct Bird end

# Fly trait requires multiple contracts with variety of func signatures
@trait Fly
@assign Bird with Can{Fly}
bird_check = @check(Bird)
@test bird_check.result == true # everything ok without interface contracts

@implement Can{Fly} by liftoff(_)
@implement Can{Fly} by speed(_, resistence::Float64)::Float64
@implement Can{Fly} by flyto(::Float64, ::Float64, _)::String  # fly to (x,y)

# Pretty trait requires a single contract
@trait Pretty
@implement Is{Pretty} by look_at_the_mirror_daily(_)::Bool

# Bird satisfies all contracts from Fly trait by concrete type
liftoff(::Bird) = "hi ho!"
speed(::Bird, resistence::Float64) = 100 - resistence
flyto(x::Float64, y::Float64, ::Bird) = "Arrvied at ($x, $y)"

# Duck satisfies partial contracts from Fly trait by concrete type
struct Duck end
@assign Duck with Can{Fly}
liftoff(::Duck) = "hi ho!"

# Chicken does not satisfy any contract from Fly trait
struct Chicken end
@assign Chicken with Can{Fly}

# Flamingo exhibits both Fly and Pretty traits
struct Flamingo end
@assign Flamingo with Can{Fly}, Is{Pretty}  # composite trait
liftoff(::Flamingo) = "wee!"
speed(::Flamingo, resistence::Float64) = 150 - resistence
flyto(x::Float64, y::Float64, ::Flamingo) = "Arrvied at ($x, $y)"
look_at_the_mirror_daily(::Flamingo) = true

# Test composite traits - total underlying 4 contracts required for this!
@trait FlyPretty with Can{Fly}, Is{Pretty}

# Crane partially satisfies Fly trait and fully satisfies Pretty trait
struct Crane end
@assign Crane with Is{FlyPretty}
speed(::Crane, resistence::Float64) = 150 - resistence
flyto(x::Float64, y::Float64, ::Crane) = "Arrvied at ($x, $y)"
look_at_the_mirror_daily(::Crane) = true

struct Penguin end
@trait Dive
@assign Penguin with Can{Dive}
@implement Can{Dive} by dive1(_, ::Integer)           # no argument name
@implement Can{Dive} by dive2(_, ::Vector{<:Integer}) # parameterized type
if SUPPORT_KWARGS
    @implement Can{Dive} by dive31(_, x::Real;)            # keyword arguments
    @implement Can{Dive} by dive32(_, x::Real; kw::Real)   # keyword arguments
    @implement Can{Dive} by dive33(_, y; kw1::Real, kw2)   # keyword arguments
    @implement Can{Dive} by dive34(_, x; kw1)
end
@implement Can{Dive} by dive4(_, ::Base.Bottom)
@implement Can{Dive} by dive5(_, x)
@implement Can{Dive} by dive6(_, ::Number)

dive1(::Penguin, ::Real) = 1                # Real >: Integer
dive2(::Penguin, ::Vector) = 2              # Vector >: Vector{<:Integer}
dive31(::Penguin, ::Number) = 31            # no kw argument
dive32(::Penguin, ::Number; kw::Complex) = 32 # kw argument type is ignored!
dive33(::Penguin, ::Int; kw...) = 33        # any number of kw arguments
dive34(::Penguin, ::Float64) = 34           # keyword argument missing
dive4(::Penguin, ::Integer) = 4             # Integer >: Base.Bottom
dive5(::Penguin, ::Int) = 5                 # Int >: Bottom
dive6(::Penguin, ::Int) = 6                 # not Int >: Number

# Issue #30 - propagate interfaces according to type inheritance
abstract type Animal end
struct Rabbit <: Animal end
@trait Eat
@assign Animal with Can{Eat}
@implement Can{Eat} by eat(_)
eat(::Animal) = 1

# no contract requirements (code coverage)
struct Kiwi end

# weird argument types in contract specs
@trait Creep
@implement Can{Creep} by creep1(_, a::Int=5) # argument assignment

struct Snake end
@assign Snake with Can{Creep}
creep1(::Snake, ::Integer) = 1

@trait PlayNice
@implement Can{PlayNice} by play(_,_)
struct Dog end
struct Cat end
play(::Dog, ::Dog) = 1
play(::Cat, ::Dog) = 2
@assign Dog with Can{PlayNice}
@assign Cat with Can{PlayNice}

# ------------------------------------------------------------------------
# interface method return type check

# 1. good case
struct HummingBird end
@assign HummingBird with Can{Fly}
liftoff(::HummingBird) = "woo hoo!"                   # good, because the contract didn't specify return type
speed(::HummingBird, ::Float64) = 64.0                # good, because contract says Float64
flyto(::Float64, ::Float64, ::HummingBird) = "Paris"  # good, because contract says String

function test_hummingbird_return_type()
    result = @check(HummingBird)
    @show result
    @test result.result == true
    @test result.implemented |> length == 3
    @test result.misses |> length == 0
end

# 2. bad case
struct Hawk end
@assign Hawk with Can{Fly}
liftoff(::Hawk) = "woo hoo!"                   # good, because the contract didn't specify return type
speed(::Hawk, ::Float64) = 64                  # bad, because contract says Float64 but we're returning Int
flyto(::Float64, ::Float64, ::Hawk) = "Paris"  # good, because contract says String

function test_hawk_return_type()
    VERSION < v"1.1" && return                 # skip test for v1.0
    result = @check(Hawk)
    @test result.result == false
    @test result.implemented |> length == 2
    @test result.misses |> length == 1
    @test result.miss_reasons[1] == "Improper return type"
end

# 3. abstract return type
@trait Wings
@implement Has{Wings} by nwings(_)::Integer

struct Pigeon end
@assign Pigeon with Has{Wings}
nwings(::Pigeon) = 2

struct Parrot end
@assign Parrot with Has{Wings}
nwings(::Parrot) = UInt8(2)

function test_abstract_return_type()
    VERSION < v"1.1" && return                 # skip test for v1.0
    for animal in (Pigeon, Parrot)
        result = @check(animal)
        @test result.result == true
        @test result.misses |> length == 0
    end
end

# ------------------------------------------------------------------------

function test()

    bird_check = @check(Bird)
    @test bird_check.result == true
    @test bird_check.implemented |> length == 3
    @test bird_check.misses |> length == 0

    chicken_check = @check(Chicken)
    @test chicken_check.result == false
    @test chicken_check.implemented |> length == 0
    @test chicken_check.misses |> length == 3

    duck_check = @check(Duck)
    @test duck_check.result == false
    @test duck_check.implemented |> length == 1
    @test duck_check.misses |> length == 2

    flamingo_check = @check(Flamingo)
    @test flamingo_check.result == true
    @test flamingo_check.implemented |> length == 4
    @test flamingo_check.misses |> length == 0

    crane_check = @check(Crane)
    @test crane_check.result == false
    @test crane_check.implemented |> length == 3
    @test crane_check.misses |> length == 1

    penguin_check = @check(Penguin)
    @test penguin_check.result == false
    @test penguin_check.implemented |> length == (SUPPORT_KWARGS ? 7 : 4)
    @test penguin_check.misses |> length == (SUPPORT_KWARGS ? 2 : 1)

    rabbit_check = @check(Rabbit)
    @test rabbit_check.result == true

    # test `show` function
    buf = IOBuffer()
    contains(s) = x -> occursin(s, x)

    show(buf, flamingo_check)
    @test buf |> take! |> String |> contains("has implemented")

    show(buf, bird_check)
    @test buf |> take! |> String |> contains("has implemented")

    show(buf, duck_check)
    @test buf |> take! |> String |> contains("is missing")

    # Bird is assigned with 1 Fly trait and that requires 3 contracts
    @test required_contracts(mod, Bird) |> length == 3

    # Crane requires 4 contracts because it has both Fly and Pretty traits
    @test required_contracts(mod, Crane) |> length == 4

    # Penguin
    if SUPPORT_KWARGS
        show(buf, penguin_check.misses)
        @test buf |> take! |> String |> contains("dive34")
    end
    show(buf, penguin_check.misses)
    @test buf |> take! |> String |> contains("dive6")

    # has no interface requirements
    check_kiwi = @check(Kiwi)
    @test check_kiwi.result
    show(buf, check_kiwi)
    @test buf |> take! |> String |> contains("has no interface contract")

    # strange argument types are both accepted
    check_snake = @check(Snake)
    @test check_snake.result
    @test check_snake.implemented |> length == 1

    # Multiple underscores in the contract
    check_dog = @check(Dog)
    check_cat = @check(Cat)
    @test check_dog.result === true    # Dog plays nice with Dog
    @test check_cat.result === false   # Cat does not play nice with Cat

    # code coverage
    @test_throws SyntaxError extract_type(:([]), :(Can{Fly}), nothing)
end

end # module

using Test
@testset "Interface validation" begin
    import .Interfaces
    Interfaces.test()
    Interfaces.test_hummingbird_return_type()
    Interfaces.test_hawk_return_type()
    Interfaces.test_abstract_return_type()
end


text:
using Serialization

macro gctime(ex)
    fc = isdefined(Base.Experimental, Symbol("@force_compile")) ? :(Base.Experimental.@force_compile) : :()
    quote
        $fc
        n = isempty(ARGS) ? 1 : parse(Int, ARGS[1])
        local times = Vector{Float64}(undef, n)
        local vals = Vector{Any}(undef, n)
        local stats = Vector{Base.GC_Diff}(undef, n)
        for i in 1:n
            start_time = time_ns()
            start_gc_num = Base.gc_num()
            vals[i] = $(esc(ex))
            times[i] = time_ns() - start_time
            stats[i] = Base.GC_Diff(Base.gc_num(), start_gc_num)
        end
        result = (value=vals, times=times, stasts=stats)
        ARGS[end] == "SERIALIZE" ? serialize(stdout, result) : display(result)
    end
end

max_stars_repo_name: d-netto/GCBenchmarks

max_issues_repo_name: d-netto/GCBenchmarks

max_forks_repo_name: d-netto/GCBenchmarks


text:
module Modifiers

export ModifiersDescriptor

using JavaCall.Conversions
using JavaCall.Core
using JavaCall.JNI

using JavaCall.Reflection: Classes

#=
Struct to hold information about class and member
access modifiers, gathered by processing the constants
that represent this constants with the
java.lang.reflect.Modifier class

static: Boolean flag set to true if the modifier is static
        and false otherwise

public: Boolean flag set to true if the modifier is public
        and false otherwise
=#
struct ModifiersDescriptor
    static::Bool
    public::Bool
end

function Base.show(io::IO, m::ModifiersDescriptor)
    print(
        io,
        "ModifiersDescriptor{static: ", m.static,
        ", public: ", m.public,
        "}");
end

function Base.:(==)(x::ModifiersDescriptor, y::ModifiersDescriptor)
    x.static == y.static &&
    x.public == y.public
end

function methodmodifiers(method::jobject)
    modifiersclass = Classes.findclass(Symbol("java.lang.reflect.Modifier"))
    modifiers = callinstancemethod(method, :getModifiers, jint, [])
    staticmod = convert_to_julia(
        Bool,
        callstaticmethod(modifiersclass.jniclass, :isStatic, jboolean, Any[jint], modifiers)
    )
    publicmod = convert_to_julia(
        Bool,
        callstaticmethod(modifiersclass.jniclass, :isPublic, jboolean, Any[jint], modifiers)
    )
    ModifiersDescriptor(staticmod, publicmod)
end

end


text:
"""
    julia_worker_regex

Regex for parsing the output printed by Julia worker processes.
"""
const julia_worker_regex = r"julia_worker:([\d]*?)#([\w\d.]*?)\n"

"""
    parse_julia_worker_output_to_hosts_and_ports(; regex = julia_worker_regex)

Parse the output printed by Julia workers and return a list of hosts and
ports.

## Example

```jldoctest; setup = :(using ExistingProcessManagers; using Distributed)
julia> worker_output = \"""
       julia_worker:9960#192.168.1.151
       julia_worker:9962#192.168.1.153
       julia_worker:9964#192.168.1.155
       julia_worker:9966#192.168.1.157
       julia_worker:9968#192.168.1.159
       \""";

julia> hosts_and_ports = parse_julia_worker_output_to_hosts_and_ports(worker_output);
```

Now you can do either of the following two options, which are equivalent:

Option 1:
```julia
julia> addprocs(ExistingProcessManager(hosts_and_ports))
```

Option 2:
```julia
julia> addprocs_existing(hosts_and_ports)
```
"""
@inline function parse_julia_worker_output_to_hosts_and_ports(worker_ouput::AbstractString;
                                                              regex = julia_worker_regex)
    occurrences = findall(regex, worker_ouput)
    num_occurences = length(occurrences)
    hosts_and_ports = Vector{Tuple{String, Int}}(undef, num_occurences)
    for i in 1:num_occurences
        occurrence = occurrences[i]
        m = match(regex, worker_ouput[occurrence])
        host = convert(String, m[2])::String
        port_string = m[1]
        port = parse(Int, port_string)::Int
        host_and_port = (host, port)
        hosts_and_ports[i] = host_and_port
    end
    unique!(hosts_and_ports)
    return hosts_and_ports
end


text:
const RC_SIZE = 20*8

immutable RegisterContext
    data::Array{UInt8}
end

ccall(:jl_load_object_file,Void,(Ptr{UInt8},),"/Users/kfischer/.julia/v0.5/Hooking/src/machojump.o")

using Base.llvmcall
function resume(RC::RegisterContext)
    llvmcall(
    (""" declare void @hooking_jl_jumpto(i8*)""",
    """
    call void @hooking_jl_jumpto(i8* %0)
    ret void
    """),Void,Tuple{Ptr{UInt8}},pointer(RC.data))
end

const RSP_IDX =  8
const RIP_IDX = 17

function callback(x::Ptr{Void})
    RC = RegisterContext(copy(pointer_to_array(convert(Ptr{UInt8}, x), (RC_SIZE,), false)))
    @show reinterpret(UInt64,RC.data)
    @show reinterpret(UInt64,RC.data)[RSP_IDX]
    @show reinterpret(UInt64,RC.data)[RIP_IDX]
    resume(RC)
    nothing
end
Base.ccallable(callback,Void,Tuple{Ptr{Void}},:hooking_jl_callback)
ccall(:jl_load_object_file,Void,(Ptr{UInt8},),"/Users/kfischer/.julia/v0.5/Hooking/src/machohook.o")
function test()
       llvmcall(
       (""" declare void @hooking_jl_savecontext()""",
       """
       call void @hooking_jl_savecontext()
       ret void
       """),Void,Tuple{})
end
test()


text:
using LogParser, Test, GZip, CSV, DataFrames

#Read in gzipped file
gzipfile = gzopen(joinpath(dirname(@__FILE__), "data", "juliabloggers-apachecombined.gz"))
jbapachecombined = CSV.read(gzipfile, delim='\t', datarow=1) |> DataFrame

#Parse file
jbparsed = parseapachecombined.(jbapachecombined[!, :Column1])

#Test that array is 122,143 elements long
@test size(jbparsed)[1] == 122143

#Test that array is of type Array{ApacheLog,1}
@test typeof(jbparsed) == Vector{ApacheLog}

#Test DataFrame method
jbparsed_df = DataFrame(jbparsed)

#Test that a DataFrame was returned
@test typeof(jbparsed_df) <: DataFrame

#Test that DataFrame is 122143x9
@test size(jbparsed_df) == (122143,9)


text:
"""
# module SystemModules

# Examples

```jldoctest
julia>
```
"""
module SystemModules

export SystemModule

struct SystemModule{T <: AbstractString}
    name::T
end

function Base.string(m::SystemModule)
    string(m.name)
end

end


text:
### A Pluto.jl notebook ###
# v0.16.0

using Markdown
using InteractiveUtils

# ╔═╡ 8024e766-21b4-11ec-1f3e-5b4a2851886b
md"""
# Visual Analytics

This Data to Knowledge module of the course is focused on learning/gaining insight from data. In this notebook we will talk about using visualization to gain insight from our data. This is often termed __visual analytics__. This is distinct from visualization for the purpose of convcining

>Visual analytics is a science that involves the integration of interactive visual interfaces with analytical techniques to develop systems that facilitate reasoning over, and interpreta- tion of, complex data. ("Visual Analytics for Healthcare", Gotz, Caban, and Chen)

Visualizations can be divided into two broad categories

- Scientific Visualization
  - Visualization of data that has a natural spatial coordinate system. Examples of this would be medical imaging or meterological imaging.
- Information Visualization
  - Visualization of data that does not have a natural spatial coordiante system.

For this project we will be limited to information visualization.

### Benefits and Challenges of Data Visualization

Data visualization benefits from the human brain's richly developed pattern recognition system. Data visualization is challenged by the sheer size of modern data sets, the two-dimensional and relatively small computer screen.

In order to effectively leverage the human visual system, it is important that visualizations leverage the inherent strengths of our system and work around our perceptual weaknesses. One area that many visualization packages have done poorly in this regards is with default color schemes. For many years the default color map  (colors selected to display different values) was the so-called "rainbow" color map, despite decades of evidence that it was not __perceptually uniform.__ For more information about color, please read this article: ["Color in a Perceptual Uniform Way"](https://medium.com/nightingale/color-in-a-perceptual-uniform-way-1eebd4bf2692).

## 5 Principles for Visualization

In ["Selected Human Factors Issues in Information Visualization"](https://journals.sagepub.com/doi/abs/10.1518/155723409x448017), George Robertson, Mary Czerwinski, Danyel Fisher, and Bongshin Lee provide five principles for visualizaion

1. __Preattentive visualization__ (the use of preattentive perception in visual representations)
1. __Spatial stability__ (the use of spatially stable layouts to take advan- tage of human spatial memory)
1. __Proximity__ (related items being near each other),
1. __Visual information-seeking mantra__ (the basic framework used by many visualization sys- tems), and (
1. __Sensemaking__ (the detailed cognitive processes humans use when trying to make sense of complex information).

The following sections provide summary excerpts from their chapter for each principle.

### Preattentive Visualization

>Research has shown that users are excellent at comparing some types of data and very bad at others. It is very easy to compare the lengths of two lines that share a baseline (as is usually done in a bar chart) and slightly harder to compare the angle of a pair of lines (as in a pie chart). Perceiving area is still harder, and perceiving volume is even more so. Last, comparing color hues quantitatively is the hardest processing task of all. This sug- gests that data graphics should be arranged to encourage users to compare simpler meas- ures rather than harder ones (Wickens & Hollands, 2000, p. 123). Thus, for example, it should be easier to compare bar chart lines than pie chart slices.

### Spatial Stability

>Humans have an automatic capability to store spatial memories; we are good at remem- bering where information is located in the world and on a screen. As such, information visualization design is enhanced by stability, all other things being equal. A stable visu- alization means that data will continue to be in the same place it had been, even as time changes. Maps, for example, are dramatic examples of stability: On most maps, Chicago will continue to be in the same relative place as it had been before. (This may not be true for a map that distorts based on the data, and these maps therefore force users to search for information). In contrast, a force-directed graph layout may be entirely differ- ent the next time a user looks at it.

### Proximity

>In addition to recognizing their own information, users are very quick to recognize groupings in visualizations. The gestalt laws of perception (Ware, 2004, p. 189) describe how users automatically group together components of diagrams, whether or not the designer intended those sections to be linked. The gestalt rules include notions of proximity, similarity, connectedness, continuity, and others.
>
>Spatial proximity means that users tend to assume that objects that are near each other are more closely related than they are to other objects. This allows users to interpret clus-ters on a scatter plot, for example.
>
>Connectedness suggests that users are likely to group together parts of diagrams that are connected to each other. Two points that are connected to each other with a line are likely to be seen as part of a whole, which can be useful for suggesting a temporal continuum. In graph drawing, graph nodes are connected by links; however, the stronger effect is disconnected islands within the graph (which cause all elements within each island to seem more closely related). Spatial proximity can contest with connectedness for dominance....
>
>Thus, the spatial proximity principle suggests that visualization designers should use space as an organizing principle by placing related items near each other. However, Wickens and Carswell (1995) discussed a broader concept of perceptual proximity in their prox- imity compatibility principle. They suggested that humans perceive proximity not only in space but also in color, time, intensity, and shape. A broader proximity principle, then, would suggest that visualization designers should use various properties of objects (their location in space, color, shape, intensity, and movement) to organize related objects to be near each other in perceptual space. So, for example, even if they are in different places on the graphic, two similar-looking objects (either similar colors or shapes) will be understood to be part of a group.

### Visual information-seeking mantra

>Shneiderman (1996) defined a mantra for visual information seeking that has been used as a framework for designing many information visualization applications: “overview first, zoom and filter, then details-on-demand” (p. 337).

### Sensemaking

>Information visualization is usually part of an exploratory process whereby the user looks for patterns and exceptions and generates or refines hypotheses to discover unknown phe- nomena. The process of interpreting a visualization and acting upon it is referred to as sensemaking (Card, Mackinlay, & Shneiderman, 1999, p. 10). In general, sensemaking is the process of collecting data, interpreting them, and using them to figure out what the next step in data collection will be. Various authors have suggested different sensemaking processes (Card et al., 1999; Isenberg, Tang, & Carpendale, 2008; Klein, Moon, & Hoffman, 2006; Pirolli & Card, 2005; Russell, Stefik, Pirolli, & Card, 1993; Thomas & Cook, 2005), a general cycle usually follows a sequence of gathering data, choosing a structure in which to represent the data, representing them, and collecting insights. As a result of the insights, the analyst chooses either new data or a new schema or acts on the data. This cycle has been articulated by Card et al. (1999) to describe how an analyst chooses a representation and interacts with it.
A set of insights is the primary result of the sensemaking process, so one way of eval- uating the effectiveness of visualization is to measure the number and quality of insights. This is known as insight-based evaluation and is discussed later.
Figure 2.6 illustrates a sensemaking cycle for some types of intelligence analysts (Thomas & Cook, 2005). It is an adaptation from Pirolli and Card (2005). Notice that there are two main cycles: a foraging loop, in which data are examined for relevant evidence, and a sensemaking loop, in which different schemas and hypotheses are tested. The con- cepts for the foraging loop were developed earlier by Pirolli and Card (1999) in informa- tion foraging theory, which was motivated by biological foraging theories. Klein et al. (2006) described a data/frame model of sensemaking similar to that in Figure 2.6, with frames instead of schemas.
"""

# ╔═╡ 96218539-2747-44d0-b854-c9e197acc047
html"""

Designing Visualizations

"""

# ╔═╡ Cell order:
# ╠═8024e766-21b4-11ec-1f3e-5b4a2851886b
# ╟─96218539-2747-44d0-b854-c9e197acc047


text:
# # Singular Integrals

# This example shows how to use the `Integration` module for computing
# quadrature rules for functions with point singularities (such as those
# appearing in the numerical discretization of boundary integral equations).

# ## Change of variables

# The first set of *tricks* revolve a round a simple change of variables. We
# focus first on the one-dimensional case, where we wish to integrate
```math
    \int_0^1 f(x) dx,
```
# and where the function ``f`` can have an integrable singularity at

using WaveProp.Geometry
using WaveProp.Integration
using WaveProp.Integration
using QuadGK

f        = (x) -> x==0 ? 0.0 : log(abs(x))*cos(x)
I,_        = quadgk(f,0,1,rtol=1e-16)

rows = GaussLegendre.([5,10,20,40,80])
cols = [identity,IMT{1,2}(),Kress{8}(), Window{1,1,7}()]
ee   = []
for qstd in rows
    for shandler in cols
        q        = SingularQuadratureRule(qstd,shandler)
        Is       = integrate(f,q)
        er       = abs(I-Is)
        push!(ee,er)
    end
end

using NamedArrays
ee = reshape(ee,length(cols),length(rows)) |> transpose |> NamedArray
setnames!(ee,string.(rows),1)
setnames!(ee,string.(cols),2)
setdimnames!(ee,["Base quadrature","Singularity handler"])
show(ee)

# ##
# using Plots
# qstd = GaussLegendre(10)
# x̂,ŵ  = qstd()
# cols = [IMT{1,2}(),Kress{8}(), Window{0.5,1,7}()]
# fig = plot()
# for shandler in cols
#     phi       = shandler.(x̂)
#     phip      = [jacobian(shandler,x)[1] for x in x̂]
#     plot!(fig,x̂,f.(phi) .* phip,label=string(shandler),m=:x)
# end
# display(fig)

max_stars_repo_name: WaveProp/WaveProp

max_issues_repo_name: WaveProp/WaveProp

max_forks_repo_name: WaveProp/WaveProp


text:
using NiBundleAdjustment
using NiBundleAdjustment: i_cross!, vec2cam, compute_reproj_err,
    project, rand_camera, P2, P3
using NiLang, NiLang.AD
using LinearAlgebra, StaticArrays
using Test

function load(b, n, m, p)
    dir_in = joinpath(dirname(dirname(@__FILE__)), "data", "ba")
    fn = "ba$(b)_n$(n)_m$(m)_p$(p)"
    fn_in = joinpath(dir_in, fn)
    NiBundleAdjustment.read_ba_instance(string(fn_in,".txt"))
end

@testset "cross" begin
    x = randn(3)
    y = randn(3)
    c = zeros(3)
    @instr i_cross!(c, x, y)
    @test c ≈ cross(x, y)
end

@testset "check grad" begin
    cams, X, w, obs, feats = load(4, 372, 47423, 204472)
    XX = [P3(X[:,i]...) for i=1:size(X, 2)]
    FEATS = [P2(feats[:,i]...) for i=1:size(feats, 2)]
    CAMS = [vec2cam(cams[:,i]) for i = 1:size(cams,2)]
    j1, j2 = compute_ba_J(Val(:NiLang), CAMS, XX, w, obs, FEATS)
    @test j1[1] ≈ -461.44632100159936
    j1_, j2_ = compute_ba_J(Val(:ForwardDiff), cams, X, w, obs, feats)
    @test j1 ≈ j1_
    @test j2 ≈ j2_
end

@testset "project" begin
    cam = rand_camera()
    scam = Camera(SVector(cam.r), SVector(cam.c), cam.f, SVector(cam.x0), SVector(cam.κ))
    X = P3(randn(3)...)
    @test SVector(project(zero(P2{Float64}), cam, X)[1]) ≈ project(scam, SVector(X))
    @test check_inv(project,(zero(P2{Float64}), cam, X))

    w = randn()
    feat = MVector{2}(randn(2))
    cam = rand_camera()
    scam = Camera(SVector(cam.r), SVector(cam.c), cam.f, SVector(cam.x0), SVector(cam.κ))
    X = MVector{3}(randn(3))
    res = compute_reproj_err(zero(cam.x0), zero(cam.x0), cam, P3(X...), w, P2(feat...))[1]

    @test compute_reproj_err(scam, X, w, feat) ≈ SVector(res)
    @test check_inv(compute_reproj_err, (zero(cam.x0), zero(cam.x0), cam, P3(X...), w, P2(feat...)))
end

@testset "P2, P3" begin
    p = P2(2.0, 4.0)
    @test (~GVar)(GVar(p)) == p
    @test GVar(P2(2.0, 3.0), P2(1.0, 0.5)) == P2(GVar(2.0, 1.0), GVar(3.0, 0.5))
    p = P3(2.0, 4.0, 8.0)
    @test (~GVar)(GVar(p)) == p
    @test GVar(P3(2.0, 3.0, 4.0), P3(1.0, 0.5, 0.25)) == P3(GVar(2.0, 1.0), GVar(3.0, 0.5), GVar(4.0, 0.25))
end

@testset "objectives" begin
    cams, X, w, obs, feats = load(4, 372, 47423, 204472)
    XX = [P3(X[:,i]...) for i=1:size(X, 2)]
    FEATS = [P2(feats[:,i]...) for i=1:size(feats, 2)]
    SCAMS = [NiBundleAdjustment.vec2scam(cams[:,i]) for i = 1:size(cams,2)]
    CAMS = [vec2cam(cams[:,i]) for i = 1:size(cams,2)]
    reproj_err! = zeros(P2{Float64}, size(feats, 2))
    w_err! = zero(w)
    reproj_err_cache! = zeros(P2{Float64}, size(feats, 2))
    NiBundleAdjustment.ba_objective!(reproj_err!, w_err!, reproj_err_cache!, CAMS, XX, w, obs, FEATS)
    reproj_err, w_err = NiBundleAdjustment.ba_objective(SCAMS, X, w, obs, feats)
    correct = true
    for j=1:size(feats, 2)
        correct = correct && reproj_err[1,j] ≈ reproj_err![j].x
        correct = correct && reproj_err[2,j] ≈ reproj_err![j].y
    end
    @test correct
    @test w_err ≈ w_err!
end


text:
function thermo_state_pθq(param_set::APS, p::FT, θ_liq_ice::FT, q_tot::FT) where {FT}
    # config = (50, 1e-3, RootSolvers.RegulaFalsiMethod)
    # config = (50, 1e-3, RootSolvers.NewtonMethodAD)
    config = ()
    return TD.PhaseEquil_pθq(param_set, p, θ_liq_ice, q_tot, config...)
end


text:
using BinaryBuilder

name = "LibUnwind"
version = v"1.3.2"

# Collection of sources required to build libffi
sources = [
    ArchiveSource("https://github.com/libunwind/libunwind/releases/download/v$(version)/libunwind-$(version).tar.gz",
                  "0a4b5a78d8c0418dfa610245f75fa03ad45d8e5e4cc091915d2dbed34c01178e"),
    DirectorySource("./bundled"),
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/libunwind*/

# We need to massage configure script to convince it to build the shared library
# for PowerPC.
if [[ "${target}" == powerpc64le-* ]]; then
    autoreconf -vi
fi

atomic_patch -p1 ${WORKSPACE}/srcdir/patches/libunwind-prefer-extbl.patch
atomic_patch -p1 ${WORKSPACE}/srcdir/patches/libunwind-static-arm.patch
atomic_patch -p0 ${WORKSPACE}/srcdir/patches/libunwind-configure-ppc64le.patch
atomic_patch -p0 ${WORKSPACE}/srcdir/patches/libunwind-configure-static-lzma.patch
atomic_patch -p1 ${WORKSPACE}/srcdir/patches/libunwind-cfa-rsp.patch

CFLAGS="${CFLAGS} -DPI -fPIC -I${prefix}/include"
./configure --prefix=${prefix} --build=${MACHTYPE} --host=${target} CFLAGS="${CFLAGS}" --libdir=${libdir} --enable-minidebuginfo --disable-tests
make -j${nproc}
make install

# Shoe-horn liblzma.a into libunwind.a
mkdir -p unpacked/{liblzma,libunwind}
(cd unpacked/liblzma; ar -x ${prefix}/lib/liblzma.a)
(cd unpacked/libunwind; ar -x ${prefix}/lib/libunwind.a)
rm -f ${prefix}/lib/libunwind.a
ar -qc ${prefix}/lib/libunwind.a unpacked/**/*
"""

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line.  libunwind is only used
# on Linux or FreeBSD (e.g. ELF systems)
platforms = filter(p -> Sys.islinux(p) || Sys.isfreebsd(p), supported_platforms(;experimental=true))

# The products that we will ensure are always built
products = [
    LibraryProduct("libunwind", :libunwind),
]

# Dependencies that must be installed before this package can be built
dependencies = [
    BuildDependency("XZ_jll"),
]

# Build the tarballs.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies; julia_compat="1.6")

max_stars_repo_name: plut/Yggdrasil

max_issues_repo_name: plut/Yggdrasil

max_forks_repo_name: plut/Yggdrasil


text:
using PkgUtils
using Pkg, Test, UUIDs

@elapsed begin
    @time @testset "Dependencies" begin include("dependencies.jl") end
    @time @testset "Environments" begin include("environments.jl") end
end


text:
#Peak result type
type JLabPeakset
    summits::Array{Int64,1}
    lbnds::Array{Int64,1}
    rbnds::Array{Int64,1}

    JLabPeakset() = new(Array(Int64,0),Array(Int64,0),Array(Int64,0))
end

# #############################################################################
#Naive peak finding methods

#Find peak locations and bounds. Include terraces in bounds
function getmountains{T}(y::Array{T,1};thresh::Union(Real,Array{Real,1})=0)

    #Get crude variation
    dy::Array{T,1} = [0,y[2:end]-y[1:end-1]]
    small = (abs(dy) .<= thresh)
    dy[small] = 0; dy[!small] = sign(dy[!small])

    #Initiate
    pks = JLabPeakset()

    #Initiate
    push!(pks.rbnds,0) #add dummy value
    ascending::Bool = false #start descending
    for i=1:length(y)-1
        if ascending
            #Wait for next drop
            if dy[i+1] == -1
                #Start of descend
                push!(pks.rbnds,i+1)
                ascending = false
            elseif y[i+1] > peakval
                #Ascend continues
                pks.summits[end] = i+1
                peakval = y[i+1]
            end
        else
            #Wait for next rise
            if dy[i+1] == 1
                #Start of new peak
                push!(pks.lbnds,i)
                push!(pks.summits,i+1)
                peakval = y[i+1]
                ascending = true
            elseif dy[i+1] == -1
                #Descend continues
                pks.rbnds[end] = i+1
            end
        end
    end

    if ascending
        #Add right bound of last peak
        push!(pks.rbnds,length(y))
    end

    #Remove dummy value
    shift!(pks.rbnds)

    return pks
end

#Find peak locations and bounds. Exclude terraces in bounds
function gethills{T}(y::Array{T,1};thresh::Union(Real,Array{Real,1})=0)

    #Get crude variation
    dy::Array{T,1} = [0,y[2:end]-y[1:end-1]]
    small = (abs(dy) .<= thresh)
    dy[small] = 0; dy[!small] = sign(dy[!small])

    #Initiate
    pks = JLabPeakset()

    #Initiate
    ascending::Bool = false #start descending
    peakval::T = 0
    for i=1:length(y)-1
        if ascending
            if dy[i+1] == 1 && dy[i] == 0
                #Correct starting foot of new hill
                pks.lbnds[end] = i
            end

            #Wait for next drop
            if y[i+1] > peakval
                #Ascend continues
                pks.summits[end] = i+1
                peakval = y[i+1]
            elseif dy[i+1] == -1
                #Start of descend
                ascending = false
            end
        else
            if dy[i+1] >= 0 && length(pks.rbnds) < length(pks.lbnds)
                #Ending foot of hill
                push!(pks.rbnds,i)
            end

            #Wait for next rise
            if dy[i+1] == 1
                #Starting foot of new hill
                push!(pks.lbnds,i)
                push!(pks.summits,i+1)
                peakval = y[i+1]
                ascending = true
            end
        end
    end

    if length(pks.rbnds) < length(pks.lbnds)
        #Add right bound of last peak
        push!(pks.rbnds,length(y))
    end

    return pks
end

# #############################################################################
#Peak processing

#Filter peaks
function filterpeaks{T}(pks::JLabPeakset,y::Array{T,1},ys::Array{Float64,1})
    pks2 = JLabPeakset()
    for i=1:length(pks.summits)
        #Get smoothed height
        altitude = ys[pks.summits[i]]
        leftht = altitude-ys[pks.lbnds[i]]
        rightht = altitude-ys[pks.rbnds[i]]
        ht = 0.5*(leftht+rightht)
        baseht = 0.5*(ys[pks.lbnds[i]]+ys[pks.rbnds[i]])

        #Collect isolated pks.summits
        thresh1 = sqrt(maximum([baseht,10]))
        if (maximum([leftht,rightht]) > 4*thresh1) && (minimum([leftht,rightht]) > 2*thresh1)
            #Get unsmoothed height
            altitude = y[pks.summits[i]]
            leftht = altitude-y[pks.lbnds[i]]
            rightht = altitude-y[pks.rbnds[i]]
            ht = ((pks.summits[i]-pks.lbnds[i])*rightht + (pks.rbnds[i]-pks.summits[i])*leftht)/(pks.rbnds[i]-pks.lbnds[i])

            if ht > 0
                push!(pks2.summits,pks.summits[i])
                push!(pks2.lbnds,pks.lbnds[i])
                push!(pks2.rbnds,pks.rbnds[i])
            end
        end
    end

    return pks2
end

#Fit function to peaks
function fitpeaks{T}(pks::JLabPeakset,x::Array{T,1},y::Array{T,1},ys::Array{Float64,1};peakshape::String="gaussian")

    #Initiate
    pkgrps = Array(Array{Int64,1},0)
    fitdata = Array(Array{Float64,1},0)
    fiterrdata = Array(Array{Float64,1},0)

    #Fitting functions
    #Individual peak functions take amplitude,width,location as parameters
    if peakshape == "gaussian"
        f_single(x,p) = p[1]*exp(-0.5*(x-p[3]).^2/p[2]^2)
    elseif peakshape == "lorentzian"
        f_single(x,p) = p[1]./(1+(x-p[3]).^2/p[2]^2)
    end
    #Peak group function takes individual peak parameters, then optionally the baseline
    function f_total(x,p)
        if length(p)%3 == 2
            ans = p[end-1]+p[end]*x
        elseif length(p)%3 == 1
            ans = p[end]
        else
            ans = 0
        end
        for k=1:div(length(p),3)
            ans += f_single(x,p[3*k-2:3*k])
        end
        return ans
    end

    #Begin
    i = 1
    while i <= length(pks.summits)
        #Collect nearby peaks
        pkgrp = [i]
        while i+1 <= length(pks.summits) && pks.lbnds[i+1]-pks.rbnds[i] < 3
            #next peak is near enough
            i += 1
            push!(pkgrp,i)
        end

        #Setup initial parameters
        p0 = Array(Float64,0)
        for j=1:length(pkgrp)
            pkalt = y[pks.summits[pkgrp[j]]]
            leftalt = y[pks.lbnds[pkgrp[j]]]
            rightalt = y[pks.rbnds[pkgrp[j]]]
            pkht = pkalt - 0.5*(leftalt+rightalt)
            push!(p0,pkht) #Estimate peak height
            push!(p0,0.09*(x[pks.rbnds[pkgrp[j]]]-x[pks.lbnds[pkgrp[j]]])) #Estimate peak width
            push!(p0,x[pks.summits[pkgrp[j]]]) #Estimate peak location
        end

        push!(pkgrps,pkgrp)
        grprng = [pks.lbnds[pkgrp[1]]:pks.rbnds[pkgrp[end]]]

        #Fitting function for multiple peaks
        dof = length(grprng) - (2+3*length(pkgrp))
        if dof > 0
            # grad = (y[grprng[end]]-y[grprng[1]])/(x[grprng[end]]-x[grprng[1]])
            # push!(p0,y[grprng[1]] - grad*x[grprng[1]])
            # push!(p0,grad)
            push!(p0,minimum([y[grprng[1]],y[grprng[end]]]))
            push!(p0,0.0)
        elseif dof == 0
            push!(p0,0.5*(y[grprng[1]]+y[grprng[end]]))
        elseif dof <= -1
            #Reject peak
            push!(fitdata,nans(2*(3*length(pkgrp)+2)))
            i += 1
            continue
        end

        #Show parameter guess
        xs = linspace(x[grprng[1]],x[grprng[end]],200)
        #plt.plot(xs,f_total(xs,p0),"k")

        #Conduct fit
        nnz = find(ys[grprng] .> 0)
        #yerr = ones(Float64,length(nnz))
        yerr = sqrt(ys[grprng[nnz]])
        fit = (
            try
                getfit(f_total,float64(x[grprng[nnz]]),float64(y[grprng[nnz]]),yerr,p0)
            catch e
                #Reject peak
                show(e)
                println("rejected")
                push!(fitdata,nans(2*(3*length(pkgrp)+2)))
                i += 1
                continue
            end)
        plt.plot(xs,f_total(xs,fit.params),"r")

        #Record result
        push!(fitdata,[fit.params,zeros(Float64,2-(length(fit.params)%3))])
        push!(fiterrdata,[fit.perrors,zeros(Float64,2-(length(fit.perrors)%3))])

        i += 1
    end
    #plt.show()

    return pkgrps,fitdata,fiterrdata
end


text:
module TestDiffeqJacobian
include("preamble.jl")

using Bifurcations.Continuations: get_prob_cache, get_u0, residual_jacobian
using Bifurcations.Examples.Calcium

@testset "ODEFunction (inplace)" begin
    prob0 = Calcium.make_prob()
    prob1 = Calcium.make_prob(ode=Calcium.make_ode_jac())
    cache0 = get_prob_cache(prob0)
    cache1 = get_prob_cache(prob1)

    @test prob0.p.de_prob.f.jac === nothing
    @test prob1.p.de_prob.f.jac === Calcium.jac

    u0 = get_u0(prob0)
    rng = MersenneTwister(0)
    for _ in 1:5
        u = typeof(u0)(randn(rng, eltype(u0), length(u0)))

        H0, J0 = residual_jacobian(u, cache0)
        H1, J1 = residual_jacobian(Array(u), cache1)

        @test H1 ≈ H0
        @test J1 ≈ J0
    end
end

end  # module


text:
@testset "Interval tests" begin

    # Closed interval
    h = HalfSpace([-1], 3.) ∩ HalfSpace([1.], 8)
    v = convexhull([-3.], [8.])

    v0 = [[1.], [8], [-2], [4], [-1], [-3], [5]]
    pp = polyhedron(vrep(v0), SimplePolyhedraLibrary{Float64}())
    @test !hrepiscomputed(pp)
    @test vrepiscomputed(pp)
    for p in (Interval{Float64, SVector{1, Float64}}(pp),
              Interval{Float64, SVector{1, Float64}}(Polyhedra.vreps(pp)...))
        @test similar_library(pp, FullDim{1}()) == IntervalLibrary{Float64}()
        @test getlibrary(p) == IntervalLibrary{Float64}()
        @test getlibraryfor(p, FullDim{2}()) == SimplePolyhedraLibrary{Float64}()
        @test hrepiscomputed(p)
        @test vrepiscomputed(p)
        @test p isa Interval{Float64}
        @test !isempty(p)
        @test volume(p) == 11
        @test dim(p) == 1
        inequality_fulltest(p, h)
        generator_fulltest(p, v)
    end

    pp = polyhedron(h, SimplePolyhedraLibrary{Float64}())
    @test hrepiscomputed(pp)
    @test !vrepiscomputed(pp)
    for p in (Interval{Float64, SVector{1, Float64}}(pp),
              Interval{Float64, SVector{1, Float64}}(Polyhedra.hreps(pp)...))
        @test hrepiscomputed(p)
        @test vrepiscomputed(p)
        @test p isa Interval{Float64}
        @test !isempty(p)
        @test iszero(surface(p))
        @test volume(p) == 11
        @test dim(p) == 1
        generator_fulltest(p, v)
        inequality_fulltest(p, h)
    end

    # Singleton
    h = intersect(HyperPlane([1.], 2))
    v = convexhull([2.])

    v0 = reshape([2., 2., 2.], 3, 1)
    p = polyhedron(vrep(v0))
    @test p isa Interval{Float64}
    @test !isempty(p)
    @test iszero(surface(p))
    @test iszero(volume(p))
    @test dim(p) == 0
    inequality_fulltest(p, h)
    generator_fulltest(p, v)

    p = polyhedron(h)
    @test p isa Interval{Float64}
    @test !isempty(p)
    @test iszero(surface(p))
    @test iszero(volume(p))
    @test dim(p) == 0
    generator_fulltest(p, v)
    inequality_fulltest(p, h)

    # Left-open interval
    h = intersect(HalfSpace([1.], 2))
    v = v + conichull([-1.])

    r0 = reshape([-1., -1], 2, 1)
    p = polyhedron(vrep(v0, r0))
    @test p isa Interval{Float64}
    @test !isempty(p)
    @test iszero(surface(p))
    @test dim(p) == 1
    inequality_fulltest(p, h)
    generator_fulltest(p, v)

    p = polyhedron(HalfSpace([1.], 3) ∩ HalfSpace([5.], 10) ∩ HalfSpace([2.], 6))
    @test p isa Interval{Float64}
    @test !isempty(p)
    @test iszero(surface(p))
    @test dim(p) == 1
    generator_fulltest(p, v)
    inequality_fulltest(p, h)

    # Right-open interval
    h = intersect(HalfSpace([-1.], -2))
    v = convexhull([2.]) + conichull([1.])

    p = polyhedron(convexhull([2.], [3.]) + conichull([1.], [3.]))
    @test p isa Interval{Float64}
    @test !isempty(p)
    @test iszero(surface(p))
    @test dim(p) == 1
    inequality_fulltest(p, h)
    generator_fulltest(p, v)

    p = polyhedron(HalfSpace([-3.], -6) ∩ HalfSpace([-2.], -2))
    p = polyhedron(h)
    @test p isa Interval{Float64}
    @test !isempty(p)
    @test iszero(surface(p))
    @test dim(p) == 1
    generator_fulltest(p, v)
    inequality_fulltest(p, h)

    # Empty
    h = HyperPlane([0], 1) ∩ HyperPlane([1], 0)
    v = vrep(Line{1, Int, Vector{Int}}[])

    p = polyhedron(v)
    @test p isa Interval{Int}
    @test isempty(p)
    @test iszero(surface(p))
    @test iszero(volume(p))
    @test dim(p) == -1
    inequality_fulltest(p, h)
    generator_fulltest(p, v)

    p = polyhedron(HalfSpace([1], 1) ∩ HalfSpace([-1], -2))
    @test p isa Interval{Int}
    @test isempty(p)
    @test iszero(surface(p))
    @test iszero(volume(p))
    @test dim(p) == -1
    generator_fulltest(p, v)
    inequality_fulltest(p, h)

    p = polyhedron(intersect(HyperPlane([0], 1)))
    @test p isa Interval{Int}
    @test isempty(p)
    @test iszero(surface(p))
    @test iszero(volume(p))
    @test dim(p) == -1
    generator_fulltest(p, v)
    inequality_fulltest(p, h)

    p = polyhedron(intersect(HalfSpace([0], -1)))
    @test p isa Interval{Int}
    @test isempty(p)
    @test iszero(surface(p))
    @test iszero(volume(p))
    @test dim(p) == -1
    generator_fulltest(p, v)
    inequality_fulltest(p, h)

    # Line
    h = hrep(HyperPlane{1, Int, Vector{Int}}[])
    v = conichull(Line([1]))

    p = polyhedron(v)
    @test p isa Interval{Int}
    @test !isempty(p)
    @test iszero(surface(p))
    @test dim(p) == 1
    inequality_fulltest(p, h)
    generator_fulltest(p, v)

    p = polyhedron(h)
    @test p isa Interval{Int}
    @test !isempty(p)
    @test iszero(surface(p))
    @test dim(p) == 1
    generator_fulltest(p, v)
    inequality_fulltest(p, h)

    # Symmetric interval
    h = HalfSpace([1], 1) ∩ HalfSpace([-1], 1)
    v = convexhull([1], [-1])

    p = polyhedron(convexhull([-1], [1], [0]))
    @test p isa Interval{Int}
    @test !isempty(p)
    @test iszero(surface(p))
    @test volume(p) == 2
    @test dim(p) == 1
    inequality_fulltest(p, h)
    generator_fulltest(p, v)

    p = polyhedron(h)
    @test p isa Interval{Int}
    @test !isempty(p)
    @test iszero(surface(p))
    @test volume(p) == 2
    @test dim(p) == 1
    generator_fulltest(p, v)
    inequality_fulltest(p, h)
end


text:
@testset "Position functions" begin
    inst = Instrument("TEST")
    dt1 = DateTime(2000, 1, 1)
    ba1 = BidAsk(dt1, 500.0, 501.1)

    dt2 = DateTime(2000, 1, 2)
    ba2 = BidAsk(dt2, 505.0, 506.5)

    begin
        # long
        pos = Position(inst, 500.0, Long, ba1, dt1, ba1.ask, ba2, dt2, ba2.bid, 0.0, 0.0, Unspecified::CloseReason, 0.0, nothing)

        @test is_long(pos)
        @test !is_short(pos)

        @test pos.open_price == 501.1
        @test open_price(pos.dir, ba1) == 501.1
        @test pos.last_price == 505.0
        @test close_price(pos.dir, ba2) == 505.0

        @test pnl_net(pos) == 500.0 * (pos.last_price - pos.open_price)
        @test pnl_gross(pos) == 500.0 * (midprice(pos.last_quote) - midprice(pos.open_quote))

        @test return_net(pos) == (pos.last_price - pos.open_price) / pos.open_price
        @test return_gross(pos) == (midprice(pos.last_quote) - midprice(pos.open_quote)) / midprice(pos.open_quote)
    end

    begin
        # short
        pos = Position(inst, -500.0, Short, ba1, dt1, ba1.bid, ba2, dt2, ba2.ask, 0.0, 0.0, Unspecified::CloseReason, 0.0, nothing)

        @test !is_long(pos)
        @test is_short(pos)

        @test pos.open_price == 500.0
        @test open_price(pos.dir, ba1) == 500.0
        @test pos.last_price == 506.5
        @test close_price(pos.dir, ba2) == 506.5

        @test pnl_net(pos) == -500.0 * (pos.last_price - pos.open_price)
        @test pnl_gross(pos) == -500.0 * (midprice(pos.last_quote) - midprice(pos.open_quote))

        @test return_net(pos) == -(pos.last_price - pos.open_price) / pos.open_price
        @test return_gross(pos) == -(midprice(pos.last_quote) - midprice(pos.open_quote)) / midprice(pos.open_quote)
    end
end


text:
using Lorenzo
using Base.Test

# write your own tests here
function test()
tests = ["scraper.jl"]

    for t in tests
        include("$(t).jl")
    end

end

include("scraper.jl")