text:
"""
$(SIGNATURES)

TrackingState that holds the tracking state.
"""
struct TrackingState{
        S <: AbstractGNSSSystem,
        C <: AbstractCorrelator,
        CALF <: AbstractLoopFilter,
        COLF <: AbstractLoopFilter,
        CN <: AbstractCN0Estimator
    }
    init_carrier_doppler::typeof(1.0Hz)
    init_code_doppler::typeof(1.0Hz)
    carrier_doppler::typeof(1.0Hz)
    code_doppler::typeof(1.0Hz)
    carrier_phase::Float64
    code_phase::Float64
    correlator::C
    carrier_loop_filter::CALF
    code_loop_filter::COLF
    sc_bit_detector::SecondaryCodeOrBitDetector
    integrated_samples::Int
    prompt_accumulator::ComplexF64
    cn0_estimator::CN
end

"""
$(SIGNATURES)

Convenient TrackingState constructor. Mandatory parameters are the GNSS system, the
carrier doppler `carrier_doppler` and the code phase `code_phase`. Optional parameters are
- Code doppler `code_doppler`, that defaults to carrier doppler multiplied with the code /
  center frequency ratio
- Carrier phase `carrier_phase`, that defaults to 0
- Carrier loop filter `carrier_loop_filter`, that defaults to the `ThirdOrderBilinarLF()`
- Code loop filter `code_loop_filter`, that defaults to the `SecondOrderBilinearLF()`
- Number of antenna elemants `num_ants`, that defaults to `NumAnts(1)`
- Correlator `correlator`, that defaults to `get_default_correlator(...)`
- Integrated samples `integrated_samples`, that defaults to 0
- Prompt accumulator for bit detection `prompt_accumulator`, that defaults to 0
- CN0 estimator `cn0_estimator`, that defaults to `MomentsCN0Estimator(20)`
"""
function TrackingState(
    ::Type{S},
    carrier_doppler,
    code_phase;
    code_doppler = carrier_doppler * get_code_center_frequency_ratio(S),
    carrier_phase = 0.0,
    carrier_loop_filter::CALF = ThirdOrderBilinearLF(),
    code_loop_filter::COLF = SecondOrderBilinearLF(),
    sc_bit_detector = SecondaryCodeOrBitDetector(),
    num_ants = NumAnts(1),
    correlator::C = get_default_correlator(S, num_ants),
    integrated_samples = 0,
    prompt_accumulator = zero(ComplexF64),
    cn0_estimator::CN = MomentsCN0Estimator(20)
) where {
    S <: AbstractGNSSSystem,
    C <: AbstractCorrelator,
    CALF <: AbstractLoopFilter,
    COLF <: AbstractLoopFilter,
    CN <: AbstractCN0Estimator
}
    if found(sc_bit_detector)
        code_phase = mod(code_phase, get_code_length(S) *
            get_secondary_code_length(S))
    else
        code_phase = mod(code_phase, get_code_length(S))
    end
    TrackingState{S, C, CALF, COLF, CN}(
        carrier_doppler,
        code_doppler,
        carrier_doppler,
        code_doppler,
        carrier_phase / 2π,
        code_phase,
        correlator,
        carrier_loop_filter,
        code_loop_filter,
        sc_bit_detector,
        integrated_samples,
        prompt_accumulator,
        cn0_estimator
    )
end

@inline get_code_phase(state::TrackingState) = state.code_phase
@inline get_carrier_phase(state::TrackingState) = state.carrier_phase
@inline get_init_code_doppler(state::TrackingState) = state.init_code_doppler
@inline get_init_carrier_doppler(state::TrackingState) = state.init_carrier_doppler
@inline get_code_doppler(state::TrackingState) = state.code_doppler
@inline get_carrier_doppler(state::TrackingState) = state.carrier_doppler
@inline get_correlator(state::TrackingState) = state.correlator
@inline get_sc_bit_detector(state::TrackingState) = state.sc_bit_detector
@inline get_carrier_loop_filter(state::TrackingState) = state.carrier_loop_filter
@inline get_code_loop_filter(state::TrackingState) = state.code_loop_filter
@inline get_prompt_accumulator(state::TrackingState) = state.prompt_accumulator
@inline get_integrated_samples(state::TrackingState) = state.integrated_samples
@inline get_cn0_estimator(state::TrackingState) = state.cn0_estimator


text:
"Load data from file."
function loaddata(file::String)
    return readlines(file)
end

"Take toboggan down the slope on map."
function toboggan(map::Array, go::Array)::Int
    pos = [1, 1]
    n_trees = 0
    while pos[1] < length(map)
        pos += go
        pos[2] -= div(pos[2], length(map[1]) + 1)*length(map[1])
        n_trees += (map[pos[1]][pos[2]] == '#')
    end
    return n_trees
end

function main()
    map = loaddata("../assets/data/03.txt")

    # part one
    n_trees = toboggan(map, [1, 3])
    println("Encountered ", n_trees, " trees while tobogganing.")

    # part two
    n_trees = 1
    for go in [[1, 1], [1, 3], [1, 5], [1, 7], [2, 1]]
        n_trees *= toboggan(map, go)
    end
    println("Product of trees while tobogganing is ", n_trees, ".")
end

main()

ext: jl


text:
# For dealing with functions as input variables
_maybe_apply(f::Function, args...) = f(args...)
_maybe_apply(f, args...) = f


text:
module AnisotropicDiffusion

using QuickTypes: @qstruct
using ArgCheck: @argcheck
using TiledIteration: EdgeIterator
using ImageFiltering: padindices, Pad
using Base.Cartesian
using LinearAlgebra

include("core.jl")
include("hand.jl")

end # module


text:
"""
ZipCode package

Copyright 2017 Matthew Amos, portions copyright 2021 Gandalf Software, Inc., Scott P. Jones

Licensed under MIT License, see LICENSE
"""
module ZipCode

using CSV, DataFrames, Unitful

export  ZIPCODES,                     # Constant DataFrame

        cleanzipcode,                 # Applies multiple cleaning functions
             removewhitespace,        # Trailing and leading whitespace
             removezipsuffix,         # ZIP+4 suffix
             padleftzeros,            # Pads leading zeros

        coord_distance,               # Calculate distance between lat/lon
        haversine,                    # Haversine distance calculation
        vincenty,                     # Vincenty distance calculation
        flatpythagorean,              # Flat Earth distance calculation
        EARTH_RADIUS_EQUATORIAL,      # Equatorial radius (meters)
        EARTH_RADIUS_POLAR,           # Polar radius (meters)

        rowcoord                      # (lat, long) for dataframe row

const ZIPCODES = CSV.File(joinpath(dirname(@__FILE__), "..", "data", "zipcode.csv")) |> DataFrame

const PATTERN_ZIPCODE= r"^[0-9]{5}$"
# const PATTERN_CITY = r"^[A-Za-z]{3,}"
# const PATTERN_STATE = r"^[A-Z]{2}"

# Type unions
const StringNA = Union{String, Missing}
const Coordinate = Tuple{Float64, Float64}

include("cleanzips.jl")
include("distance.jl")
include("utils.jl")

end


text:
# Objective function & sense.
#
# SCIP only supports affine objectives. For quadratic or nonlinear objectives,
# the solver will depend on bridges with auxiliary variables. Single variable
# objectives are also accepted, but the type is not correctly remembered.

MOI.supports(::Optimizer, ::MOI.ObjectiveSense) = true
MOI.supports(::Optimizer, ::MOI.ObjectiveFunction{SAF}) = true

function MOI.set(o::Optimizer, ::MOI.ObjectiveFunction{SAF}, obj::SAF)
    allow_modification(o)

    # reset objective coefficient of all variables first
    for v in values(o.inner.vars)
        @SCIP_CALL SCIPchgVarObj(o, v[], 0.0)
    end

    # set new objective coefficients, summing coefficients
    for t in obj.terms
        v = var(o, t.variable)
        oldcoef = SCIPvarGetObj(v)
        newcoef = oldcoef + t.coefficient
        @SCIP_CALL SCIPchgVarObj(o, v, newcoef)
    end

    @SCIP_CALL SCIPaddOrigObjoffset(o, obj.constant - SCIPgetOrigObjoffset(o))

    return nothing
end

function MOI.get(o::Optimizer, ::MOI.ObjectiveFunction{SAF})
    terms = AFF_TERM[]
    for vr = keys(o.inner.vars)
        vi = VI(vr.val)
        coef = SCIPvarGetObj(var(o, vi))
        coef == 0.0 || push!(terms, AFF_TERM(coef, vi))
    end
    constant = SCIPgetOrigObjoffset(o)
    return SAF(terms, constant)
end

function MOI.set(o::Optimizer, ::MOI.ObjectiveSense, sense::MOI.OptimizationSense)
    allow_modification(o)
    if sense == MOI.MIN_SENSE
        @SCIP_CALL SCIPsetObjsense(o, SCIP_OBJSENSE_MINIMIZE)
    elseif sense == MOI.MAX_SENSE
        @SCIP_CALL SCIPsetObjsense(o, SCIP_OBJSENSE_MAXIMIZE)
    else
        # erasing objective
        MOI.set(o, MOI.ObjectiveFunction{SAF}(), SAF([], 0.0))
    end
    o.objective_sense = sense
    return nothing
end

function MOI.get(o::Optimizer, ::MOI.ObjectiveSense)
    return o.objective_sense
end

function MOI.modify(o::Optimizer, ::MOI.ObjectiveFunction{SAF},
                    change::MOI.ScalarCoefficientChange{Float64})
    allow_modification(o)
    @SCIP_CALL SCIPchgVarObj(o, var(o, change.variable), change.new_coefficient)
    return nothing
end

MOI.get(::Optimizer, ::MOI.ObjectiveFunctionType) = SAF


text:
using LightGraphs, MetaGraphs, Compose, LinearAlgebra
using DataFrames, GraphPlot, Colors,Combinatorics
# include("weights_for_graph.jl")
gr()
"""
    base_adjacency_grid

Creates a basic grid of m x n size (m and n must be ints)
"""
base_grid(m, n) = CartesianIndices((m, n))
base_grid(N) = base_grid(N...)
"""
    find_neighbors

Finds neighbors for given node given full environment
"""
function find_neighbors(node, all_nodes)
    dirs = [CartesianIndex(i, j) for i in -1:1 for j in -1:1 if !(i==j==0)]
    N = []
    for dir in dirs
        n_p = dir+node
        if n_p in all_nodes
            push!(N, n_p)
        end
    end
    return N
end

"""
    create_graph

Given size m and n, will create a grid adjacency matrix
"""
function create_graph(m, n)
    V = CartesianIndices((m, n))
    G = MetaGraph(m * n)
    LI = LinearIndices(V)
    for i in V
        Nᵢ = find_neighbors(i, V)
        for j in Nᵢ
            add_edge!(G, LI[i], LI[j])
            set_prop!(G, LI[i], :pos, i)
            dist = norm(Tuple(i - j))
            set_prop!(G, LI[i], LI[j], :weight, dist)
        end
    end
    set_indexing_prop!(G, :pos)
    return G
end

function remove_obstacles(G, obs)
    obs_coord = []
    for ob in obs
        push!(obs_coord, set2grid(ob))
    end
    for coord in obs_coord
        corners = [coord[1, 1], coord[1, end], coord[end, 1], coord[end, end]]
        borders = [coord[1, :], coord[:, 1], coord[end, :], coord[:, end]]
        for pos in coord
            for neighbori in collect(neighbors(G, G[pos, :pos]))
                rem_edge!(G, G[pos, :pos], neighbori)
            end
        end
    end
    return G
end

function graph2gridplot(G, obs=[])
    finalplot = scatter(legend=false)
    for i in 1:nv(G)
        scatter!(Tuple(G[i, :pos]), color="gray22", markersize=2)
        for j in neighbors(G, i)
            xpos0, ypos0 = Tuple(G[i, :pos])
            xn, yn = Tuple(G[j, :pos])
            plot!([xpos0, xn], [ypos0, yn], color="gray56")
        end
    end
    [plot!(finalplot, ob, color="grey22") for ob in obs if !isempty(obs)]
    png(finalplot, "test.png")
    finalplot
end
"""
    create_weighted_graph

Reads an adjacency matrix and outputs the MetaGraph associated with the optimal
weights.
"""
function create_weighted_graph(G)
    A = adjacency_matrix(G)
    Aw = find_weights(adjacency_matrix(G))
    for edge in edges(G)
        plcx, plcy = [src(edge), dst(edge)]
        set_prop!(G, edge, :weight, Aw[plcx, plcy])
    end
    return G
end

"""
    read_adjacency

Reads a weighted sparse adjacency matrix and outputs the meta graph.
"""
function read_adjacency(As)

    rows = rowvals(As)
    vals = nonzeros(As)
    m, n = size(As)
    srcs = Vector{Int}(undef, n)
    dsts = Vector{Int}(undef, n)
    wgts = Vector{Float64}(undef, n)
    for i = 1:n
       for j in nzrange(As, i)
          row = rows[j]
          val = vals[j]
          # perform sparse wizardry...
       end
    end
    println()
    for (i, line) in enumerate(filename)
        println(line)
        spl = split(line, ",")
        srcs[i] = parse(Int64, spl[1])
        dsts[i] = parse(Int64, spl[2])
        wgts[i] = parse(Float64, spl[3])
    end
    return construct_graph(srcs, dsts, wgts)
end

"""
    construct_graph(src, dst, wgt)

Takes in three vectors (sources, destinations, weights) and constructs a MetaGraph out of them.
"""
function construct_graph(srcs, dsts, wgts)
    # the maximal value in sources and destinations is the number of nodes
    n = maximum([srcs; dsts])
    G = MetaGraph(SimpleGraph(n))
    defaultweight!(G, 0.0)
    # For each src=>dst, wgt, create a weighted edge for it
    for (s,d,w) in zip(srcs, dsts, wgts)
        # ignore zero weight edges, since the default is 0 anyway
        if w > 0
            add_edge!(G, s, d)
            set_prop!(G, s, d, :weight, w)
        end
    end
    return G
end

"""
    plot_graph(G, fn)

Save the graph `G` as an SVG in the local directory with the filename `fn`.
Uses the default `spring_layout` layout.
"""
function plot_graph(G, fn)
    mn, mx = extrema(weights(G))
    line_colors = map(edges(G)) do ε
        src, dst = Tuple(ε)
        weighted_color_mean(weights(G)[src, dst], colorant"black", colorant"black")
    end

    nodelabel = 1:nv(G)
    p = gplot(G, edgestrokec = line_colors, nodelabel = nodelabel)
    draw(SVG(fn, 10cm, 10cm), p)
end


text:
# set the Directory to the local folder
#cd("C://Users//Ali Nikpendar//Google Drive//Julia files//Grace//Temp_Projects//dataentry")

#using Queryverse
using CPLEX
using CSV
using DataFrames
using Dates
using DelimitedFiles
using JuMP
#using XLSX

# Constants
const N_Gens =  24 # number of conventional generators
const N_StorgUs =  10 # number of storage units
const N_Zones = 10
const M_Zones = 10
const N_Blocks =7
const N_Hrs = 24
const INITIAL_DAY = 1
const FINAL_DAY = 1

const FILE_ACCESS_OVER = "w+"
const FILE_ACCESS_APPEND = "a+"

# Logging file
io_log = open(
    string(
        ".//outputs//log//UC_BAU_",
        Dates.format(now(), "yyyy-mm-dd_HH-MM-SS"),
        ".txt",
    ),
    FILE_ACCESS_APPEND,
)

t1 = time_ns()
#include("import_data.jl")

##
########################### Importing input data from the input spreadsheets
# Generators' specification
#=
DF_Generators = DataFrame(XLSX.readtable(".\\inputs\\data_generators.XLSX", "data_generators")...)

# Generators location map: if a generator g is located in zone z Map_Gens[g,z]=1; and 0 otherwise
DF_Map_Gens = DataFrame(XLSX.readtable(".\\inputs\\data_generators.XLSX", "location_generators")...)
Map_Gens = convert(Matrix, DF_Map_Gens[:,2:N_Zones+1])

# Storage Units' specification and location
DF_Storage = DataFrame(XLSX.readtable(".\\inputs\\data_storage.XLSX", "data_storage")...) # storage specs
DF_Map_Storage = DataFrame(XLSX.readtable(".\\inputs\\data_storage.XLSX", "location_storage")...) # storage location as a dataframe
Map_Storage = convert(Matrix, DF_Map_Storage[:,2:N_Zones+1]) # convert storage location data to  a matrix

# energy demand at each location
Demands = XLSX.readdata(".\\inputs\\data_demand.XLSX", "data_demand", "B2:K25")
# There is no map for the demand data. Instead we take the input demand data for each zone. In other words, Demand[t,z] represents demand at zone z and time t

# Transmission system data (Capacity and Susceptance)
TranC = XLSX.readdata(".\\inputs\\data_transmission.XLSX", "LineCapacity","B2:K11") # Transmission line capacity
TranS = XLSX.readdata(".\\inputs\\data_transmission.XLSX", "LineSusceptance","B2:K11")# Transmission line susceptance

# Up and down Reserve Requirements data
Reserve_Req_Up = XLSX.readdata(".\\inputs\\data_reserve_reqs.XLSX", "data_reserve_reqs","B2:B25") # Hourly Up Reserve requirement data
Reserve_Req_Dn = XLSX.readdata(".\\inputs\\data_reserve_reqs.XLSX", "data_reserve_reqs","C2:C25")# Hourly down reserve requirement data
=#

df_gens = CSV.read(".//inputs//csv//data_generators.csv", DataFrame);
map_gens = readdlm(".//inputs//csv//location_generators.csv", ','; header = true);
df_storage = CSV.read(".//inputs//csv//data_storage.csv", DataFrame);
map_storage = readdlm(".//inputs//csv//location_storage.csv", ','; header = true);
demands = readdlm(".//inputs//csv//data_demand.csv", ','; header = true);
tranc = readdlm(".//inputs//csv//LineCapacity.csv", ','; header = true);
trans = readdlm(".//inputs//csv//LineSusceptance.csv", ','; header = true);
reserve_reqs = readdlm(".//inputs//csv//data_reserve_reqs.csv", ','; header = true);
# Reorganize data
demands_head = demands[2];
demands = demands[1];
demands = demands[:, 2:size(demands,2)];

map_gens_head = map_gens[2];
map_gens = map_gens[1];
map_gens = map_gens[:,2:N_Zones+1]
#map_gens = convert(Array{Int32,2}, map_gens[:,2:N_Zones+1]);

map_storage_head = map_storage[2];
map_storage = map_storage[1];
map_storage = map_storage[:,2:N_Zones+1]
#map_storage = convert(Array{Int32,2}, map_storage[:,2:N_Zones+1]);

tranc_head = tranc[2];
tranc = tranc[1];
tranc = tranc[:,2:size(tranc,2)];

trans_head = trans[2];
trans = trans[1];
trans = trans[:,2:size(trans,2)];

reserve_reqs_head = reserve_reqs[2];
reserve_reqs = reserve_reqs[1];
reserve_req_up = reserve_reqs[:,2];
reserve_req_dn = reserve_reqs[:,3];

DF_Generators = df_gens
Map_Gens = map_gens
DF_Storage = df_storage
Map_Storage = map_storage
Demands = demands
TranC = tranc
TranS = trans
Reserve_Req_Up = reserve_req_up
Reserve_Req_Dn = reserve_req_dn

## Pre-processing the data to calculate the model inputs:
#=
## Pre-processing calculations for the first method enforcing min up and down time constraints
ReqUpTime=zeros(N_Gens,1) # Difference between minimum up time and  the number of periods that unit g was on before t=1 of the scheduling horizon
ReqUpTimeInit=zeros(N_Gens,1) #determined based on ReqDnTime, represents the number of periods that unit must remain on at the beginning of scheduling horizon
ReqDnTime=zeros(N_Gens,1) # Difference between minimum down time and  the number of periods that unit g was off before t=1 of the scheduling horizon
ReqDnTimeInit=zeros(N_Gens,1) #determined based on ReqDnTime, represents the number of periods that unit must remain off at the beginning of scheduling horizon

for g in 1:N_Gens
   ReqUpTime[g] = (DF_Generators.MinUpTime[g] - DF_Generators.UpTimeInit[g])*DF_Generators.StatusInit[g]
   if (ReqUpTime[g] > N_Hrs)
      ReqUpTimeInit[g] = N_Hrs
   elseif (ReqUpTime[g] <0)
      ReqUpTimeInit[g] = 0
   else
      ReqUpTimeInit[g] = ReqUpTime[g]
   end
end
ReqUpTimeInit_I = round.(Int,ReqUpTimeInit)

for g in 1:N_Gens
   ReqDnTime[g] = (DF_Generators.MinDownTime[g] - DF_Generators.DownTimeInit[g])*(1-DF_Generators.StatusInit[g])
   if (ReqDnTime[g] > N_Hrs)
      ReqDnTimeInit[g] = N_Hrs
   elseif (ReqDnTime[g] <0)
      ReqDnTimeInit[g] = 0
   else
      ReqDnTimeInit[g] = ReqDnTime[g]
   end
end
ReqDnTimeInit_I = round.(Int,ReqDnTimeInit)
=#
##

# The time range lower-bound for min up constrain using the alternative approach
lbu=zeros(N_Gens, N_Hrs)
for g in 1:N_Gens , t in 1:N_Hrs
    lbu[g,t]=t-DF_Generators.MinUpTime[g]+1
    if (lbu[g,t]<1)
        lbu[g,t]=1
    end
end

lb_MUT = round.(Int, lbu)

lbd=zeros(N_Gens,N_Hrs)
for g in 1:N_Gens , t in 1:N_Hrs
    lbd[g,t]=t-DF_Generators.MinUpTime[g]+1
    if (lbd[g,t]<1)
        lbd[g,t]=1
    end
end

lb_MDT = round.(Int, lbd)

## Defining the UC model
UCmodel=Model(CPLEX.Optimizer)
set_optimizer_attribute(UCmodel, "CPX_PARAM_EPINT", 1e-5)

# Declaring the decision variables for conventional generators
@variable(UCmodel, genOnOff[1:N_Gens, 0:N_Hrs], Bin) #Bin
@variable(UCmodel, genStartUp[1:N_Gens, 1:N_Hrs], Bin) # startup variable
@variable(UCmodel, genShutDown[1:N_Gens, 1:N_Hrs], Bin) # shutdown variable
@variable(UCmodel, genOut[1:N_Gens, 0:N_Hrs]>=0) # Generator's output schedule
@variable(UCmodel, genResUp[1:N_Gens, 1:N_Hrs]>=0) # Generators' up reserve schedule
@variable(UCmodel, genResDn[1:N_Gens, 1:N_Hrs]>=0) # Generator's down rserve schedule

# declaring decision variables for storage Units
@variable(UCmodel, storgChrg[1:N_StorgUs, 1:N_Hrs], Bin) #Bin variable equal to 1 if unit runs in the charging mode
@variable(UCmodel, storgDisc[1:N_StorgUs, 1:N_Hrs], Bin) #Bin variable equal to 1 if unit runs in the discharging mode
@variable(UCmodel, storgIdle[1:N_StorgUs, 1:N_Hrs], Bin) ##Bin variable equal to 1 if unit runs in the idle mode
@variable(UCmodel, storgChrgPwr[1:N_StorgUs, 0:N_Hrs]>=0) #Chargung power
@variable(UCmodel, storgDiscPwr[1:N_StorgUs, 0:N_Hrs]>=0) # Discharging Power
@variable(UCmodel, storgSOC[1:N_StorgUs, 0:N_Hrs]>=0) # state of charge (stored energy level for storage unit at time t)
@variable(UCmodel, storgResUp[1:N_StorgUs, 0:N_Hrs]>=0) # Scheduled up reserve
@variable(UCmodel, storgResDn[1:N_StorgUs, 0:N_Hrs]>=0) # Scheduled down reserve

# declaring variables for transmission system
@variable(UCmodel, voltAngle[1:N_Zones, 1:N_Hrs]) #voltage angle at zone/bus n in t//
@variable(UCmodel, powerFlow[1:N_Zones, 1:M_Zones, 1:N_Hrs]) #transmission Flow from zone n to zone m//

# Defining the objective function that minimizes the overal cost of supplying electricity (=total variable, no-load, startup, and shutdown costs)

@objective(UCmodel, Min, sum(sum(DF_Generators.VariableCost[g]*genOut[g,t]+DF_Generators.NoLoadCost[g]*genOnOff[g,t] +DF_Generators.StartUpCost[g]*genStartUp[g,t] + DF_Generators.ShutdownCost[g]*genShutDown[g, t] for g in 1:N_Gens)
           for t in 1:N_Hrs))

#=
# Commitment variable upper bound
@constraint(UCmodel, conOnOffUB[t=1:N_Hrs, g=1:N_Gens],  genOnOff[g,t] <= 1)
@constraint(UCmodel, conGenSUUB[t=1:N_Hrs, g=1:N_Gens],  genStartUp[g,t] <= 1)
@constraint(UCmodel, conGenSDUB[t=1:N_Hrs, g=1:N_Gens],  genShutDown[g,t] <= 1)
=#

#Initialization of commitment and dispatch variables at t=0 (representing the last hour of previous scheduling horizon day=day-1 and t=24)
@constraint(UCmodel, conInitGenOnOff[g=1:N_Gens], genOnOff[g,0]==DF_Generators.StatusInit[g]) # initial generation level for generator g at t=0
@constraint(UCmodel, conInitGenOut[g=1:N_Gens], genOut[g,0]==DF_Generators.PowerInit[g]) # initial on/off status for generators g at t=0
@constraint(UCmodel, conInitSOC[p=1:N_StorgUs], storgSOC[p,0]==DF_Storage.SOCInit[p]) # SOC for storage unit p at t=0

## Constraints representing technical limits of conventional generators
#Status transition trajectory of
@constraint(UCmodel, conStartUpAndDn[t=1:N_Hrs, g=1:N_Gens], (genOnOff[g,t] - genOnOff[g,t-1] - genStartUp[g,t] + genShutDown[g,t])==0)
#Max power generation limit
@constraint(UCmodel, conMaxPow[t=1:N_Hrs, g=1:N_Gens],  genOut[g,t]+genResUp[g,t] <= DF_Generators.MaxPowerOut[g]*genOnOff[g,t] )
# Min power generation limit
@constraint(UCmodel, conMinPow[t=1:N_Hrs, g=1:N_Gens],  genOut[g,t]-genResDn[g,t] >= DF_Generators.MinPowerOut[g]*genOnOff[g,t] )
# Up reserve provision limit
@constraint(UCmodel, conMaxResUp[t=1:N_Hrs, g=1:N_Gens],  genResUp[g,t] <= DF_Generators.UpReserveLimit[g]*genOnOff[g,t] )
#Down reserve provision limit
@constraint(UCmodel, conMaxResDown[t=1:N_Hrs, g=1:N_Gens],  genResDn[g,t] <= DF_Generators.DownReserveLimit[g]*genOnOff[g,t] )
#Up ramp rate limit
@constraint(UCmodel, conRampRateUp[t=1:N_Hrs, g=1:N_Gens], (genOut[g,t] - genOut[g,t-1] <=(DF_Generators.RampUpLimit[g]*genOnOff[g, t-1]) + (DF_Generators.RampStartUpLimit[g]*genStartUp[g,t])))
# Down ramp rate limit
@constraint(UCmodel, conRampRateDown[t=1:N_Hrs, g=1:N_Gens], (genOut[g,t-1] - genOut[g,t] <=(DF_Generators.RampDownLimit[g]*genOnOff[g,t]) + (DF_Generators.RampShutDownLimit[g]*genShutDown[g,t])))
# Min Up Time limit with alternative formulation
@constraint(UCmodel, conUpTime[t=1:N_Hrs, g=1:N_Gens], (sum(genStartUp[g,k] for k=lb_MUT[g,t]:t)<=genOnOff[g,t]))
# Min down Time limit with alternative formulation
@constraint(UCmodel, conDownTime[t=1:N_Hrs, g=1:N_Gens], (1-sum(genShutDown[g,i] for i=lb_MDT[g,t]:t)>=genOnOff[g,t]))

## Constraints representing technical characteristics of storage units
# status transition of storage units between charging, discharging, and idle modes
@constraint(UCmodel, conStorgStatusTransition[t=1:N_Hrs, p=1:N_StorgUs], (storgChrg[p,t]+storgDisc[p,t]+storgIdle[p,t])==1)
# charging power limit
@constraint(UCmodel, conStrgChargPowerLimit[t=1:N_Hrs, p=1:N_StorgUs], (storgChrgPwr[p,t] - storgResDn[p,t])<=DF_Storage.Power[p]*storgChrg[p,t])
# Discharging power limit
@constraint(UCmodel, conStrgDisChgPowerLimit[t=1:N_Hrs, p=1:N_StorgUs], (storgDiscPwr[p,t] + storgResUp[p,t])<=DF_Storage.Power[p]*storgDisc[p,t])
# Down reserve provision limit
@constraint(UCmodel, conStrgDownResrvMax[t=1:N_Hrs, p=1:N_StorgUs], storgResDn[p,t]<=DF_Storage.Power[p]*storgChrg[p,t])
# Up reserve provision limit`
@constraint(UCmodel, conStrgUpResrvMax[t=1:N_Hrs, p=1:N_StorgUs], storgResUp[p,t]<=DF_Storage.Power[p]*storgDisc[p,t])
# State of charge at t
@constraint(UCmodel, conStorgSOC[t=1:N_Hrs, p=1:N_StorgUs], storgSOC[p,t]==storgSOC[p,t-1]-(storgDiscPwr[p,t]/DF_Storage.TripEfficDown[p])+(storgChrgPwr[p,t]*DF_Storage.TripEfficUp[p])-(storgSOC[p,t]*DF_Storage.SelfDischarge[p]))
# minimum energy limit
@constraint(UCmodel, conMinEnrgStorgLimi[t=1:N_Hrs, p=1:N_StorgUs], storgSOC[p,t]-(storgResUp[p,t]/DF_Storage.TripEfficDown[p])+(storgResDn[p,t]/DF_Storage.TripEfficUp[p])>=0)
# Maximum energy limit
@constraint(UCmodel, conMaxEnrgStorgLimi[t=1:N_Hrs, p=1:N_StorgUs], storgSOC[p,t]-(storgResUp[p,t]/DF_Storage.TripEfficDown[p])+(storgResDn[p,t]/DF_Storage.TripEfficUp[p])<=(DF_Storage.Power[p]/DF_Storage.PowerToEnergRatio[p]))
## Constraints representing transmission grid capacity constraints
# DC Power Flow Calculation
@constraint(UCmodel, conDCPowerFlowPos[t=1:N_Hrs, n=1:N_Zones, m=1:N_Zones], powerFlow[n,m,t]-(TranS[n,m]*(voltAngle[n,t]-voltAngle[m,t])) ==0)
@constraint(UCmodel, conDCPowerFlowNeg[t=1:N_Hrs, n=1:N_Zones, m=1:N_Zones], powerFlow[n,m,t]+powerFlow[m,n,t]==0)
# Tranmission flow bounds (from n to m and from m to n)
@constraint(UCmodel, conPosFlowLimit[t=1:N_Hrs, n=1:N_Zones, m=1:N_Zones], powerFlow[n,m,t]<=TranC[n,m])
@constraint(UCmodel, conNegFlowLimit[t=1:N_Hrs, n=1:N_Zones, m=1:N_Zones], powerFlow[m,n,t]>=-TranC[n,m])
# Voltage Angle bounds and reference point
@constraint(UCmodel, conVoltAnglUB[t=1:N_Hrs, n=1:N_Zones], voltAngle[n,t]<=π)
@constraint(UCmodel, conVoltAnglLB[t=1:N_Hrs, n=1:N_Zones], voltAngle[n,t]>=-π)
@constraint(UCmodel, conVoltAngRef[t=1:N_Hrs], voltAngle[1,t]==0)

## System-wide Constraints
#nodal balance constraint
@constraint(UCmodel, conNodBalanc[t=1:N_Hrs, n=1:N_Zones], sum((genOut[g,t]*Map_Gens[g,n]) for g=1:N_Gens) + sum((storgDiscPwr[p,t]*Map_Storage[p,n]) for p=1:N_StorgUs) - sum((storgChrgPwr[p,t]*Map_Storage[p,n]) for p=1:N_StorgUs)  - Demands[t,n] == sum(powerFlow[n,m,t] for m=1:M_Zones))
# Minimum up reserve requirement
@constraint(UCmodel, conMinUpReserveReq[t=1:N_Hrs], sum(genResUp[g,t] for g=1:N_Gens) + sum(storgResUp[p,t] for p=1:N_StorgUs) >= Reserve_Req_Up[t] )

# Minimum down reserve requirement
@constraint(UCmodel, conMinDnReserveReq[t=1:N_Hrs], sum(genResDn[g,t] for g=1:N_Gens) + sum(storgResDn[p,t] for p=1:N_StorgUs) >= Reserve_Req_Dn[t] )

JuMP.optimize!(UCmodel)
#optimize!(UCmodel)
println("Objective value: ", JuMP.objective_value(UCmodel))

    #Printing general results
    println("------------------------------------")
    println("------- OBJECTIVE VALUE -------")
    println("Objective value for day 1: ", JuMP.objective_value(UCmodel))

    println("------------------------------------")
    println("------- PRIMAL STATUS -------")
    println(primal_status(UCmodel))

    println("------------------------------------")
    println("------- DUAL STATUS -------")
    println(JuMP.dual_status(UCmodel))

    println("Day: 1 solved")
    println("---------------------------")

    system_cost = JuMP.objective_value(UCmodel)
    model_solve_time = JuMP.solve_time(UCmodel)

    write(io_log, "System cost:\t $system_cost\n")
    write(io_log, "Time to solve model (s):\t $model_solve_time\n")

## Write the optimal outcomes into spreadsheets
############# Later we need to include a variable for day so the cell number in which the results are printed is updated accordingly

# Write the conventional generators' schedules
#genOut_head = string.(fill("G",N_GEN), 1:N_GEN)
gen_out_head = ["Hour", "GeneratorID", "VariableCost", "MinPowerOut",
                "MaxPowerOut", "Output", "On/off", "ShutDown", "Startup"]

open(".//outputs//GenOutputs.csv", FILE_ACCESS_OVER) do io
    writedlm(io, permutedims(gen_out_head), ',')
    for t in 1:N_Hrs, g=1:N_Gens
        cell_n = ((t-1)*N_Gens)+g+1 # +1 is to start from the second row and leave the first row for typing
        # In the above line, we should also make  usre +1 is only applied to the first day so the results are not printed on labels
        writedlm(io, hcat(t, g, DF_Generators.VariableCost[g],
        DF_Generators.MinPowerOut[g], DF_Generators.MaxPowerOut[g],
        JuMP.value.(genOut[g,t]), JuMP.value.(genOnOff[g,t]),
        JuMP.value.(genShutDown[g,t]), JuMP.value.(genStartUp[g,t]) ), ',')
    end # ends the loop
end;

# Write storage units' schedules

gen_storage_out_head = ["Hour", "StorageUniID", "Power", "EnergyLimit", "Charge_St",
                    "Discharge_St", "Idle_St", "storgChrgPwr", "storgDiscPwr",
                    "storgSOC", "storgResUp", "storgResDn"]

open(".//outputs//StorageOutputs.csv", FILE_ACCESS_OVER) do io
    writedlm(io, permutedims(gen_storage_out_head), ',')
    for t in 1:N_Hrs, p=1:N_StorgUs
        cell_n = ((t-1)*N_StorgUs)+p+1 # +1 is to start from the second row and leave the first row for typing
        # In the above line, we should also make  usre +1 is only applied to the first day so the results are not printed on labels
        writedlm(io, hcat(t, p, DF_Storage.Power[p],
            DF_Storage.Power[p]/DF_Storage.PowerToEnergRatio[p],
            JuMP.value.(storgChrg[p,t]), JuMP.value.(storgDisc[p,t]),
            JuMP.value.(storgIdle[p,t]), JuMP.value.(storgChrgPwr[p,t]),
            JuMP.value.(storgDiscPwr[p,t]), JuMP.value.(storgSOC[p,t]),
            JuMP.value.(storgResUp[p,t]), JuMP.value.(storgResDn[p,t]) ), ',')
    end # ends the loop
end;

# Write the transmission flow schedules
tran_flow_head = ["Time period", "Source", "Sink", "Flow", "TransCap"]
open(".//outputs//TranFlowOutputs.csv", FILE_ACCESS_OVER) do io
    writedlm(io, permutedims(tran_flow_head), ',')
    for t in 1:N_Hrs, n=1:N_Zones, m=1:M_Zones
        cell_n = ((t-1)*N_Zones*M_Zones)+((n-1)*N_Zones)+m+1 # +1 is to start from the second row and leave the first row for typing
        # In the above line, we should also make  usre +1 is only applied to the first day so the results are not printed on labels
        writedlm(io, hcat(t, n, m, JuMP.value.(powerFlow[n,m,t]),
                TranC[n,m] ), ',')
    end # ends the loop
end;

#=
XLSX.openxlsx(".\\outputs\\GenOutputs.xlsx", mode="w") do xf
    sheet = xf[1]
    XLSX.rename!(sheet, "new_sheet")
    sheet["A1:I1"] = ["Hour" "GeneratorID" "VariableCost" "MinPowerOut" "MaxPowerOut" "Output" "On/off" "ShutDown" "Startup"]
    for t in 1:N_Hrs, g=1:N_Gens
        cell_n = ((t-1)*N_Gens)+g+1 # +1 is to start from the second row and leave the first row for typing
        # In the above line, we should also make  usre +1 is only applied to the first day so the results are not printed on labels
        sheet[XLSX.CellRef(cell_n,1)] = t
        sheet[XLSX.CellRef(cell_n,2)] = g
        sheet[XLSX.CellRef(cell_n,3)] = DF_Generators.VariableCost[g]
        sheet[XLSX.CellRef(cell_n,4)] = DF_Generators.MinPowerOut[g]
        sheet[XLSX.CellRef(cell_n,5)] = DF_Generators.MaxPowerOut[g]
        sheet[XLSX.CellRef(cell_n,6)] = JuMP.value.(genOut[g,t])
        sheet[XLSX.CellRef(cell_n,7)] = JuMP.value.(genOnOff[g,t])
        sheet[XLSX.CellRef(cell_n,8)] = JuMP.value.(genShutDown[g,t])
        sheet[XLSX.CellRef(cell_n,9)] = JuMP.value.(genStartUp[g,t])
    end # ends the loop

end # ends "do"

XLSX.openxlsx(".\\outputs\\StorageOutputs.xlsx", mode="w") do xf
    sheet = xf[1]
    XLSX.rename!(sheet, "new_sheet")
    sheet["A1:L1"] = ["Hour" "StorageUniID" "Power" "EnergyLimit" "Charge_St" "Discharge_St" "Idle_St" "storgChrgPwr" "storgDiscPwr" "storgSOC" "storgResUp" "storgResDn"]
    for t in 1:N_Hrs, p=1:N_StorgUs
        cell_n = ((t-1)*N_StorgUs)+p+1 # +1 is to start from the second row and leave the first row for typing
        # In the above line, we should also make  usre +1 is only applied to the first day so the results are not printed on labels
        sheet[XLSX.CellRef(cell_n,1)] = t
        sheet[XLSX.CellRef(cell_n,2)] = p
        sheet[XLSX.CellRef(cell_n,3)] = DF_Storage.Power[p]
        sheet[XLSX.CellRef(cell_n,4)] = DF_Storage.Power[p]/DF_Storage.PowerToEnergRatio[p]
        sheet[XLSX.CellRef(cell_n,5)] = JuMP.value.(storgChrg[p,t])
        sheet[XLSX.CellRef(cell_n,6)] = JuMP.value.(storgDisc[p,t])
        sheet[XLSX.CellRef(cell_n,7)] = JuMP.value.(storgIdle[p,t])
        sheet[XLSX.CellRef(cell_n,8)] = JuMP.value.(storgChrgPwr[p,t])
        sheet[XLSX.CellRef(cell_n,9)] = JuMP.value.(storgDiscPwr[p,t])
        sheet[XLSX.CellRef(cell_n,10)] = JuMP.value.(storgSOC[p,t])
        sheet[XLSX.CellRef(cell_n,11)] = JuMP.value.(storgResUp[p,t])
        sheet[XLSX.CellRef(cell_n,12)] = JuMP.value.(storgResDn[p,t])
    end # ends the loop

end # ends "do"

# Write the transmission flow schedules
XLSX.openxlsx(".\\outputs\\TranFlowOutputs.xlsx", mode="w") do tf
    sheet = tf[1]
    XLSX.rename!(sheet, "new_sheet_II")
    sheet["A1:E1"] = ["Time period" "Source" "Sink" "Flow" "TransCap"]
    for t in 1:N_Hrs, n=1:N_Zones, m=1:M_Zones
        cell_n = ((t-1)*N_Zones*M_Zones)+((n-1)*N_Zones)+m+1 # +1 is to start from the second row and leave the first row for typing
        # In the above line, we should also make  usre +1 is only applied to the first day so the results are not printed on labels
        sheet[XLSX.CellRef(cell_n,1)] = t
        sheet[XLSX.CellRef(cell_n,2)] = n
        sheet[XLSX.CellRef(cell_n,3)] = m
        sheet[XLSX.CellRef(cell_n,4)] = JuMP.value.(powerFlow[n,m,t])
        sheet[XLSX.CellRef(cell_n,5)] = TranC[n,m]
    end # ends the loop
end # ends "do"

=#
t2 = time_ns()
elapsedTime = (t2 -t1)/1.0e9;

write(io_log, "Whole program time execution (s):\t $elapsedTime\n")
close(io_log);

max_stars_repo_name: power-systems-op/grace-arpae

max_issues_repo_name: power-systems-op/grace-arpae

max_forks_repo_name: power-systems-op/grace-arpae


text:
using LearningHorse.Preprocessing
using LearningHorse.Classification
using LearningHorse.Tree
using CSV
import DataFrames

@testset "Classification" begin
    data = Matrix(dataloader("iris"))
    x, t = data[:, 1:4], data[:, 5]

    #The test for Decision Tree
    model = DecisionTree()
    @test_nowarn Tree.fit!(model, x, t)
    @test_nowarn Tree.predict(model, x)
    @test_nowarn MV("test.dot", model)

    #The test for Random forest
    model = RandomForest(10)
    @test_nowarn Tree.fit!(model, x, t)
    @test_nowarn Tree.predict(model, x)
    paths = ["test$(string(i)).dot" for i in 1 : 10]
    @test_nowarn MV(paths, model)

    LE = LabelEncoder()
    OHE = OneHotEncoder()
    t = LE(t)
    t = OHE(t)

    #The test for Logistic Regression
    model = Logistic(alpha = 0.1)
    @test_nowarn Classification.fit!(model, x, t)
    @test_nowarn Classification.predict(model, x)

    #The test for Support Vector machine Classification(One-Vs-Rest)
    model = SVC()
    @test_nowarn Classification.fit!(model, x, t)
    @test_nowarn Classification.predict(model, x)
end


text:
mutable struct MedianAccumulatorSimple{T}
    values::Vector{T}
    ind::Int
end
MedianAccumulatorSimple(::Type{T},N) where {T} = MedianAccumulatorSimple(Vector{T}(undef,2N+1),1)

function init!(acc::MedianAccumulatorSimple{T}, A::AbstractArray{T}, N::Integer, dim::Integer, Ipost::CartesianIndex, Ipre::CartesianIndex) where {T}
    D = size(A,dim)

    ind = 1
    for i=1-N:1 # N elements before and the starting element
        acc.values[ind] = A[Ipre,1,Ipost] # duplicate initial edge element
        ind+=1
    end
    for i=2:N+1 # N elements after the starting element
        acc.values[ind] = A[Ipre,min(i,D),Ipost] # duplicate edge elements (needed for large N)
        ind += 1
    end
    acc.ind = 1
    nothing
end
function update!(acc::MedianAccumulatorSimple{T}, new::T) where {T}
    acc.values[acc.ind] = new
    acc.ind = acc.ind==length(acc.values) ? 1 : acc.ind+1
    nothing
end
_median(acc::MedianAccumulatorSimple) = median(acc.values)

# 1-dimensional median filter along the selected dimension
# elements outside the range are considered to be the same as the edge elements
function medianfilter(A::AbstractArray{T}, N::Integer, dim::Integer=1) where {T}
    @assert N>=0
    (N == 0 || size(A,dim)==0) && return copy(A) # no-op
    B = similar(A)

    # Cartesian indexing follows exponential filter example from http://www.juliabloggers.com/multidimensional-algorithms-and-iteration/
    Rpre = CartesianIndices(size(A)[1:dim-1])
    Rpost = CartesianIndices(size(A)[dim+1:end])

    D = size(A,dim)

    acc = MedianAccumulatorSimple(T,N) # possibly have one accumulator for everything in Rpre to improve cache efficiency?

    for Ipost in Rpost
        for Ipre in Rpre
            # initialize
            init!(acc, A, N, dim, Ipost, Ipre)

            # moving filter
            i=1
            while true
                i1 = i-N
                i2 = i+N+1
                B[Ipre,i,Ipost] = _median(acc)

                i == size(A,dim) && break
                # update!(acc, A[Ipre,max(i1,1),Ipost], i1, A[Ipre,min(i2,D),Ipost], i2)
                update!(acc, A[Ipre,min(i2,D),Ipost])
                i += 1
            end
        end
    end

    B
end


text:
# we write a function that swaps rows u and v of some matrix A
function swapRows(A, u, v)

	# we make a copy of A to work with
	B = copy(A)

	# we reassign rows u and v of B
	B[v ,:] = A[u, :]
	B[u ,:] = A[v, :]

	# we return B
	return B
end

max_stars_repo_path: 1a/LU with pivot/swapRows.jl

max_issues_repo_path: 1a/LU with pivot/swapRows.jl

max_forks_repo_path: 1a/LU with pivot/swapRows.jl


text:
# This file was generated by the Julia Swagger Code Generator
# Do not modify this file directly. Modify the swagger specification instead.

@doc raw"""The pod this Toleration is attached to tolerates any taint that matches the triple <key,value,effect> using the matching operator <operator>.

    IoK8sApiCoreV1Toleration(;
        effect=nothing,
        key=nothing,
        operator=nothing,
        tolerationSeconds=nothing,
        value=nothing,
    )

    - effect::String : Effect indicates the taint effect to match. Empty means match all taint effects. When specified, allowed values are NoSchedule, PreferNoSchedule and NoExecute.
    - key::String : Key is the taint key that the toleration applies to. Empty means match all taint keys. If the key is empty, operator must be Exists; this combination means to match all values and all keys.
    - operator::String : Operator represents a key's relationship to the value. Valid operators are Exists and Equal. Defaults to Equal. Exists is equivalent to wildcard for value, so that a pod can tolerate all taints of a particular category.
    - tolerationSeconds::Int64 : TolerationSeconds represents the period of time the toleration (which must be of effect NoExecute, otherwise this field is ignored) tolerates the taint. By default, it is not set, which means tolerate the taint forever (do not evict). Zero and negative values will be treated as 0 (evict immediately) by the system.
    - value::String : Value is the taint value the toleration matches to. If the operator is Exists, the value should be empty, otherwise just a regular string.
"""
mutable struct IoK8sApiCoreV1Toleration <: SwaggerModel
    effect::Any # spec type: Union{ Nothing, String } # spec name: effect
    key::Any # spec type: Union{ Nothing, String } # spec name: key
    operator::Any # spec type: Union{ Nothing, String } # spec name: operator
    tolerationSeconds::Any # spec type: Union{ Nothing, Int64 } # spec name: tolerationSeconds
    value::Any # spec type: Union{ Nothing, String } # spec name: value

    function IoK8sApiCoreV1Toleration(;effect=nothing, key=nothing, operator=nothing, tolerationSeconds=nothing, value=nothing)
        o = new()
        validate_property(IoK8sApiCoreV1Toleration, Symbol("effect"), effect)
        setfield!(o, Symbol("effect"), effect)
        validate_property(IoK8sApiCoreV1Toleration, Symbol("key"), key)
        setfield!(o, Symbol("key"), key)
        validate_property(IoK8sApiCoreV1Toleration, Symbol("operator"), operator)
        setfield!(o, Symbol("operator"), operator)
        validate_property(IoK8sApiCoreV1Toleration, Symbol("tolerationSeconds"), tolerationSeconds)
        setfield!(o, Symbol("tolerationSeconds"), tolerationSeconds)
        validate_property(IoK8sApiCoreV1Toleration, Symbol("value"), value)
        setfield!(o, Symbol("value"), value)
        o
    end
end # type IoK8sApiCoreV1Toleration

const _property_map_IoK8sApiCoreV1Toleration = Dict{Symbol,Symbol}(Symbol("effect")=>Symbol("effect"), Symbol("key")=>Symbol("key"), Symbol("operator")=>Symbol("operator"), Symbol("tolerationSeconds")=>Symbol("tolerationSeconds"), Symbol("value")=>Symbol("value"))
const _property_types_IoK8sApiCoreV1Toleration = Dict{Symbol,String}(Symbol("effect")=>"String", Symbol("key")=>"String", Symbol("operator")=>"String", Symbol("tolerationSeconds")=>"Int64", Symbol("value")=>"String")
Base.propertynames(::Type{ IoK8sApiCoreV1Toleration }) = collect(keys(_property_map_IoK8sApiCoreV1Toleration))
Swagger.property_type(::Type{ IoK8sApiCoreV1Toleration }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_IoK8sApiCoreV1Toleration[name]))}
Swagger.field_name(::Type{ IoK8sApiCoreV1Toleration }, property_name::Symbol) =  _property_map_IoK8sApiCoreV1Toleration[property_name]

function check_required(o::IoK8sApiCoreV1Toleration)
    true
end

function validate_property(::Type{ IoK8sApiCoreV1Toleration }, name::Symbol, val)
end


text:
using Flux, Flux.Data.MNIST
using Flux: @epochs, onehotbatch, mse, throttle
using Base.Iterators: partition
using Juno: @progress
# using CuArrays

# Encode MNIST images as compressed vectors that can later be decoded back into
# images.

imgs = MNIST.images()

# Partition into batches of size 1000
data = [float(hcat(vec.(imgs)...)) for imgs in partition(imgs, 1000)]
data = gpu.(data)

N = 32 # Size of the encoding

# You can try to make the encoder/decoder network larger
# Also, the output of encoder is a coding of the given input.
# In this case, the input dimension is 28^2 and the output dimension of
# encoder is 32. This implies that the coding is a compressed representation.
# We can make lossy compression via this `encoder`.
encoder = Dense(28^2, N, leakyrelu) |> gpu
decoder = Dense(N, 28^2, leakyrelu) |> gpu

m = Chain(encoder, decoder)

loss(x) = mse(m(x), x)

evalcb = throttle(() -> @show(loss(data[1])), 5)
opt = ADAM(params(m))

@epochs 10 Flux.train!(loss, zip(data), opt, cb = evalcb)

# Sample output

using Images

img(x::Vector) = Gray.(reshape(clamp.(x, 0, 1), 28, 28))

function sample()
  # 20 random digits
  before = [imgs[i] for i in rand(1:length(imgs), 20)]
  # Before and after images
  after = img.(map(x -> cpu(m)(float(vec(x))).data, before))
  # Stack them all together
  hcat(vcat.(before, after)...)
end

cd(@__DIR__)

save("sample.png", sample())

max_stars_repo_name: lcy-seso/model-zoo

max_issues_repo_name: lcy-seso/model-zoo

max_forks_repo_name: lcy-seso/model-zoo


text:
using DSGE, ModelConstructors, Random, Plots
using Plots.PlotMeasures

# What do you want to do?
estimate_dsgevar  = true   # Estimate a DSGEVAR using SMC
use_estim_output  = true   # Use posterior from SMC estimation for following code. Otherwise, use default parameters.
get_VAR_system    = true   # Compute VAR coefficients and innovations-covariance matrix
do_modal_irf      = true   # Compute IRFs using modal parameters
compare_modal_irf = true   # Plot the modal DSGEVAR λ = ∞ rotation IRF and the actual DSGE IRF for observables
do_full_band_irf  = true   # Compute IRFs using parameters drawn from a distribution
create_meansbands = false  # Save full_band_irfs to MeansBands
do_parallel       = false  # Use parallel workers
n_workers         = 10
dsgevar_λ         = 0.5    # What λ do you want to use?

Random.seed!(1793)

# Set up DSGE and load data
m = AnSchorfheide("ss0")
m[:e_R] = 0. # DSGEVAR does not use
m[:e_π] = 0. # measurement error with
m[:e_y] = 0. # subspec ss3
forecast_string = "test_dsgevar" # Change this to an empty string if you don't want an identifier for saved output
m <= Setting(:sampling_method, :SMC)
m <= Setting(:impulse_response_horizons, 20)

m <= Setting(:n_particles, 50)          # a small number just to make the estimation and IRFs faster.
m <= Setting(:use_fixed_schedule, true) # Fix schedule to fewer iterations
m <= Setting(:n_Φ, 50)                  # to speed up the estimation further

m <= Setting(:date_mainsample_start, DSGE.quartertodate("1960-Q1")) # Set bounds on the periods of
m <= Setting(:date_presample_start, DSGE.quartertodate("1959-Q3"))  # data being loaded so we don't
m <= Setting(:date_forecast_start, DSGE.quartertodate("2019-Q3"))   # get NaNs.
data = df_to_matrix(m, load_data(m))    # the rows of the data MUST correspond
                                        # to the exact order of the observables specified by the DSGEVAR.

# Construct a DSGEVAR from AnSchorfheide
dsgevar = DSGEVAR(m, collect(keys(m.exogenous_shocks)), "ss3")
DSGE.update!(dsgevar, λ = dsgevar_λ)

#= ss3 prespecifies the observables, lags, and λ
Equivalently, we could have run

```
dsgevar = DSGEVAR(m)
DSGE.update!(dsgevar, observables =
collect(keys(m.observables)),
shocks = collect(keys(m.exogenous_shocks)),
lags = 4, λ = dsgevar_λ)
```

Or we could have also run

```
dsgevar = DSGEVAR(m, "ss3")
DSGE.update!(dsgevar, shocks = collect(keys(m.exogenous_shocks)), observables = collect(keys(m.observables)))
```
=#

# Estimate the DSGEVAR
if estimate_dsgevar
    if do_parallel
        my_procs = addprocs(n_workers)
        @everywhere using DSGE, OrderedCollections
    end

    estimate(dsgevar, data; run_csminwel = false, verbose = :none)

    if do_parallel
        rmprocs(my_procs)
    end
end

# Compute VAR coefficients and innovations variance-covariance matrices
# as well as population moments and the underlying DSGE's state space representation
if get_VAR_system
    var_system = Dict()
    var_system[:var_approx] = Dict()
    var_system[:dsgevar_λ] = Dict()

    # Coefficients and innovations variance-covariance matrices when approximating the DSGE
    # and when using the DSGE as a prior for the VAR
    var_system[:var_approx][:β], var_system[:var_approx][:Σ] = compute_system(dsgevar; use_intercept = true)
    var_system[:dsgevar_λ][:β], var_system[:dsgevar_λ][:Σ]   = compute_system(dsgevar, data)

    # Population moments
    var_system[:var_approx][:yyyyd], var_system[:var_approx][:xxyyd], var_system[:var_approx][:xxxxd] =
        compute_system(dsgevar; use_intercept = true, get_population_moments = true)
    var_system[:dsgevar_λ][:yyyyd], var_system[:dsgevar_λ][:xxyyd], var_system[:dsgevar_λ][:xxxxd] =
        compute_system(dsgevar, data; get_population_moments = true)

    # DSGE state space representation using the VAR's desired observables
    var_system[:var_approx][:system] = compute_system(dsgevar; use_intercept = true, get_system = true)
    var_system[:dsgevar_λ][:system]  = compute_system(dsgevar, data; get_system = true)
end

# Compute impulse responses
if do_modal_irf
    modal_irfs = Dict()

    if use_estim_output
        DSGE.update!(dsgevar, load_draws(dsgevar, :mode))
        DSGE.update!(m,       load_draws(dsgevar, :mode))
    end

    ## DSGE impulse responses
    modal_irfs[:dsge] = Dict()
    dsge_shocks = vcat(1., zeros(n_observables(m) - 1))
    system = compute_system(m)
    modal_irfs[:dsge][:cholesky] = Dict()
    modal_irfs[:dsge][:cholesky][:states], modal_irfs[:dsge][:cholesky][:obs], modal_irfs[:dsge][:cholesky][:pseudo] =
        impulse_responses(system, impulse_response_horizons(m),
                          Matrix{Float64}(I, n_observables(m), n_observables(m)),
                          dsge_shocks; restriction = :short_run, flip_shocks = false, get_shocks = false)

    # This commented code triggers a PositiveDefiniteException, likely because with the particular combination
    # of shocks and observables chosen, the long-run identification will not work.
    # modal_irfs[:dsge][:choleskyLR] = Dict()
    # modal_irfs[:dsge][:choleskyLR][:states], modal_irfs[:dsge][:choleskyLR][:obs], modal_irfs[:dsge][:choleskyLR][:pseudo] =
    #     impulse_responses(system, impulse_response_horizons(m),
    #                       Matrix{Float64}(I, n_observables(m), n_observables(m)),
    #                       dsge_shocks; restriction = :long_run, flip_shocks = false, get_shocks = false)

    modal_irfs[:dsge][:maxBC] = Dict()
    modal_irfs[:dsge][:maxBC][:states], modal_irfs[:dsge][:maxBC][:obs], modal_irfs[:dsge][:maxBC][:pseudo] =
        impulse_responses(system, impulse_response_horizons(m), (2π / 32, 2π / 6), 1;
                          flip_shocks = false, get_shocks = false)

    ## DSGEVAR impulse responses (λ = dsgevar_λ and λ = ∞)
    modal_irfs[:var_approx] = Dict() # λ = ∞
    modal_irfs[:dsgevar_λ] = Dict()  # λ = dsgevar_λ
    for method in [:cholesky, :choleskyLR, :maxBC]
        modal_irfs[:var_approx][method] =
            impulse_responses(dsgevar, method, 1; horizon = impulse_response_horizons(dsgevar),
                              flip_shocks = false, use_intercept = true)
        modal_irfs[:dsgevar_λ][method] =
            impulse_responses(dsgevar, data, method, 1; horizon = impulse_response_horizons(dsgevar),
                              flip_shocks = false)
    end
    DSGE.update!(dsgevar; λ = Inf)
    modal_irfs[:var_approx][:rotation] =
        impulse_responses(dsgevar, data; horizon = impulse_response_horizons(dsgevar),
                          deviations = true, flip_shocks = false)
    DSGE.update!(dsgevar; λ = dsgevar_λ)

    if compare_modal_irf
        DSGE.update!(dsgevar; λ = Inf)
        modal_irfs[:var_approx][:rotation] =
            impulse_responses(dsgevar, data; horizon = impulse_response_horizons(dsgevar),
                              deviations = true, flip_shocks = false, normalize_rotation = true)
        DSGE.update!(dsgevar; λ = dsgevar_λ)
        dsge_system = compute_system(m)
        dsge_states, dsge_obs, dsge_pseudo = impulse_responses(m, dsge_system)
        plots_dict = Dict{Symbol, Dict{Symbol,Plots.Plot}}()
        for (shock, j) in m.exogenous_shocks
            plots_dict[shock] = Dict{Symbol, Plots.Plot}()
            for (obs, i) in DSGE.get_observables(dsgevar)
                plots_dict[shock][obs] = plot(1:impulse_response_horizons(m), dsge_obs[i, :, j],
                                              label = "DSGE", color = :red, linestyle = :dash, linewidth = 3)
                plot!(1:impulse_response_horizons(m), modal_irfs[:var_approx][:rotation][i, :, j],
                      label = "DSGE-VAR (\\lambda = Inf)", color = :black,
                      left_margin = 20px, linewidth = 3)
            end
        end
    end
end

if do_full_band_irf
    if do_parallel
        my_procs = addprocs(n_workers)
        @everywhere using DSGE, OrderedCollections
    end

    full_band_irfs = Dict()
    modal_irfs_wrapper = Dict() # this does the same thing as the code block above, but here, we use the wrapper script to do it.

    paras = if use_estim_output
        load_draws(dsgevar, :full)
    else
        # This code repeat the modal parameters
        # and reshapes to a ndraws x nparameters Matrix.
        convert(Matrix{Float64}, repeat(map(θ -> θ.value, DSGE.get_parameters(dsgevar)), 1, 10)')
    end
    if use_estim_output
        DSGE.update!(dsgevar, load_draws(dsgevar, :mode))
    end

    ## Choose the index of the observable corresponding to the desired orthogonal shock
    n_obs_shock = 1

    ## DSGE impulse responses
    full_band_irfs[:dsge] = Dict()
    modal_irfs_wrapper[:dsge] = Dict()
    for method in [:cholesky, :maxBC]
        full_band_irfs[:dsge][method] =
            impulse_responses(m, paras, :full, method, n_obs_shock; parallel = do_parallel,
                              flip_shocks = false, create_meansbands = create_meansbands,
                              forecast_string = forecast_string)
        modal_irfs_wrapper[:dsge][method] =
            impulse_responses(m, map(θ -> θ.value, DSGE.get_parameters(dsgevar)), :mode, method, n_obs_shock; parallel = false,
                              flip_shocks = false, create_meansbands = false,
                              forecast_string = forecast_string)
    end

    ## DSGEVAR impulse responses (λ = dsgevar_λ and λ = ∞)
    full_band_irfs[:var_approx] = Dict()
    full_band_irfs[:dsgevar_λ] = Dict()
    modal_irfs_wrapper[:var_approx] = Dict()
    modal_irfs_wrapper[:dsgevar_λ] = Dict()
    for method in [:cholesky, :choleskyLR, :maxBC]
        full_band_irfs[:var_approx][method] =
            impulse_responses(dsgevar, paras, :full, method, n_obs_shock; parallel = do_parallel,
                              use_intercept = true, flip_shocks = false, create_meansbands = create_meansbands,
                              forecast_string = forecast_string)

        # The commented code below does the same as the previous lines in the case that you do
        # not want to construct a DSGEVAR object but still want to approximate a DSGE with a VAR.
        # See the docstring for more information about the differences between the two functions.
        # full_band_irfs[:var_approx][method] =
        #     impulse_responses(m, paras, :full, method, 4, [:obs_gdp, :obs_cpi, :obs_nominalrate],
        #                       collect(keys(m.exogenous_shocks)), n_obs_shock; parallel = do_parallel,
        #                       use_intercept = true, flip_shocks = false, create_meansbands = create_meansbands,
        #                       forecast_string = forecast_string)

        modal_irfs_wrapper[:var_approx][method] =
            impulse_responses(dsgevar, paras[1, :], :mode, method, n_obs_shock; parallel = do_parallel,
                              use_intercept = true, flip_shocks = false, create_meansbands = create_meansbands,
                              forecast_string = forecast_string)

        full_band_irfs[:dsgevar_λ][method] =
            impulse_responses(dsgevar, paras, data, :full, method; parallel = do_parallel,
                              flip_shocks = false, create_meansbands = create_meansbands,
                              forecast_string = forecast_string)

        modal_irfs_wrapper[:dsgevar_λ][method] =
            impulse_responses(dsgevar, paras[1, :], data, :mode, method; parallel = do_parallel,
                              flip_shocks = false, create_meansbands = create_meansbands,
                              forecast_string = forecast_string)
    end
    DSGE.update!(dsgevar; λ = Inf)
    full_band_irfs[:var_approx][:rotation] =
        impulse_responses(dsgevar, paras, data, :full, :rotation; parallel = do_parallel,
                          flip_shocks = false, create_meansbands = create_meansbands,
                          deviations = true,
                          forecast_string = forecast_string)
    DSGE.update!(dsgevar; λ = dsgevar_λ)

    DSGE.update!(dsgevar; λ = Inf)
    modal_irfs_wrapper[:var_approx][:rotation] =
        impulse_responses(dsgevar, paras[1, :], data, :mode, :rotation; parallel = do_parallel,
                          flip_shocks = false, create_meansbands = create_meansbands,
                          deviations = true,
                          forecast_string = forecast_string)
    DSGE.update!(dsgevar; λ = dsgevar_λ)

    if do_parallel
        rmprocs(my_procs)
    end
end


text:
doc"""
    obv(ohlcv; price="Close", v="Volume")

On Balance Volume

```math
    OBV_t = OBV_{t - 1} +
        \begin{cases}
            volume  & \text{if} \ close_t > close_{t-1} \\
            0       & \text{if} \ close_t = close_{t-1} \\
            -volume & \text{if} \ close_t < close_{t-1}
        \end{cases}
```

"""
function obv(ohlcv::TimeArray{T,N}; price="Close", v="Volume") where {T,N}

    ret    = percentchange(ohlcv[price])
    vol    = zeros(length(ohlcv))
    vol[1] = ohlcv[v].values[1]

    for i=2:length(ohlcv)
      if ret.values[i-1] >= 0
        vol[i] += ohlcv[v].values[i]
      else
        vol[i] -= ohlcv[v].values[i]
      end
    end

    TimeArray(ohlcv.timestamp, cumsum(vol), ["obv"], ohlcv.meta)
end

doc"""
    vwap(ohlcv, n; price="Close", v="Volume")

Volume Weight-Adjusted Price

```math
    P = \frac{\sum_j P_j Q_j}{\sum_j Q_j} \ ,\text{where Q is the volume}
```

"""
function vwap(ohlcv::TimeArray{T,N}, n::Int; price="Close", v="Volume") where {T,N}
    p   = ohlcv[price]
    q   = ohlcv[v]
    ∑PQ = moving(sum, p .* q, n)
    ∑Q  = moving(sum, q, n)
    val = ∑PQ ./ ∑Q

    TimeArray(val.timestamp, val.values, ["vwap"], ohlcv.meta)
end

vwap(ohlcv::TimeArray{T,N}) where {T,N} = vwap(ohlcv, 10)

function advance_decline(x)
    #code here
end

function mcclellan_summation(x)
    #code here
end

function williams_ad(x)
    #code here
end

doc"""
    adl(ohlcv; h="High", l="Low", c="Close", v="Volume")

**Accumulation/Distribution Line**

Developed by Marc Chaikin.

**Formula**

```math
    ADL_t = ADL_{t-1} +
        \frac{(Close_t - Low_t) - (High_t - Close_t)}{High_t - Low_t}
        \times Volume_t
```

**Reference**

- [StockCharts]
  (http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:accumulation_distribution_line)
"""
function adl(ohlcv::TimeArray; h="High", l="Low", c="Close", v="Volume")
    _h = ohlcv[h]
    _l = ohlcv[l]
    _c = ohlcv[c]
    _v = ohlcv[v]

    flow_facor = ((_c .- _l) .- (_h .- _c)) ./ (_h .- _l)
    flow_vol = flow_facor .* _v

    vals = similar(flow_vol.values)
    vals[1] = flow_vol.values[1]
    for i ∈ 2:length(flow_vol.values)
        vals[i] = vals[i-1] + flow_vol.values[i]
    end

    TimeArray(ohlcv.timestamp, vals, ["adl"], ohlcv.meta)
end


text:
include("../src/FakeModule.jl")
include("../src/FakeModule2.jl")

using Main.FakeModule
using Main.FakeModule2

FakeModule.f(3)

f(2)

g(2)

h(2)


text:
# it has unique keys
# leftChild has keys which are less than the node
# rightChild has keys which are greater than the node
# color is true if it's a Red Node, else it's false
mutable struct RBTreeNode{K}
    color::Bool
    data::Union{K, Nothing}
    leftChild::Union{Nothing, RBTreeNode{K}}
    rightChild::Union{Nothing, RBTreeNode{K}}
    parent::Union{Nothing, RBTreeNode{K}}

    RBTreeNode{K}() where K = new{K}(true, nothing, nothing, nothing, nothing)

    RBTreeNode{K}(d::K) where K = new{K}(true, d, nothing, nothing, nothing)
end

RBTreeNode() = RBTreeNode{Any}()
RBTreeNode(d) = RBTreeNode{Any}(d)

function create_null_node(K::Type)
    node = RBTreeNode{K}()
    node.color = false
    return node
end

mutable struct RBTree{K}
    root::RBTreeNode{K}
    nil::RBTreeNode{K}
    count::Int

    function RBTree{K}() where K
        rb = new()
        rb.nil = create_null_node(K)
        rb.root = rb.nil
        rb.count = 0
        return rb
    end
end

RBTree() = RBTree{Any}()

Base.length(tree::RBTree) = tree.count

"""
    search_node(tree, key)

Returns the last visited node, while traversing through in binary-search-tree fashion looking for `key`.
"""
search_node(tree, key)

function search_node(tree::RBTree{K}, d::K) where K
    node = tree.root
    while node !== tree.nil && d != node.data
        if d < node.data
            node = node.leftChild
        else
            node = node.rightChild
        end
    end
    return node
end

"""
    haskey(tree, key)

Returns true if `key` is present in the `tree`, else returns false.
"""
function Base.haskey(tree::RBTree{K}, d::K) where K
    node = search_node(tree, d)
    return (node.data == d)
end

"""
    insert_node!(tree::RBTree, node::RBTreeNode)

Inserts `node` at proper location by traversing through the `tree` in a binary-search-tree fashion.
"""
function insert_node!(tree::RBTree, node::RBTreeNode)
    node_y = nothing
    node_x = tree.root

    while node_x !== tree.nil
        node_y = node_x
        if node.data < node_x.data
            node_x = node_x.leftChild
        else
            node_x = node_x.rightChild
        end
    end

    node.parent = node_y
    if node_y == nothing
        tree.root = node
    elseif node.data < node_y.data
        node_y.leftChild = node
    else
        node_y.rightChild = node
    end
end

"""
    left_rotate!(tree::RBTree, node_x::RBTreeNode)

Performs a left-rotation on `node_x` and updates `tree.root`, if required.
"""
function left_rotate!(tree::RBTree, node_x::RBTreeNode)
    node_y = node_x.rightChild
    node_x.rightChild = node_y.leftChild
    if node_y.leftChild !== tree.nil
        node_y.leftChild.parent = node_x
    end
    node_y.parent = node_x.parent
    if (node_x.parent == nothing)
        tree.root = node_y
    elseif (node_x == node_x.parent.leftChild)
        node_x.parent.leftChild = node_y
    else
        node_x.parent.rightChild = node_y
    end
    node_y.leftChild = node_x
    node_x.parent = node_y
end

"""
    right_rotate!(tree::RBTree, node_x::RBTreeNode)

Performs a right-rotation on `node_x` and updates `tree.root`, if required.
"""
function right_rotate!(tree::RBTree, node_x::RBTreeNode)
    node_y = node_x.leftChild
    node_x.leftChild = node_y.rightChild
    if node_y.rightChild !== tree.nil
        node_y.rightChild.parent = node_x
    end
    node_y.parent = node_x.parent
    if (node_x.parent == nothing)
        tree.root = node_y
    elseif (node_x == node_x.parent.leftChild)
        node_x.parent.leftChild = node_y
    else
        node_x.parent.rightChild = node_y
    end
    node_y.rightChild = node_x
    node_x.parent = node_y
end

"""
   fix_insert!(tree::RBTree, node::RBTreeNode)

This method is called to fix the property of having no two adjacent nodes of red color in the `tree`.
"""
function fix_insert!(tree::RBTree, node::RBTreeNode)
    parent = nothing
    grand_parent = nothing
    # for root node, we need to change the color to black
    # other nodes, we need to maintain the property such that
    # no two adjacent nodes are red in color
    while  node != tree.root && node.parent.color
        parent = node.parent
        grand_parent = parent.parent

        if (parent == grand_parent.leftChild) # parent is the leftChild of grand_parent
            uncle = grand_parent.rightChild

            if (uncle.color) # uncle is red in color
                grand_parent.color = true
                parent.color = false
                uncle.color = false
                node = grand_parent
            else  # uncle is black in color
                if (node == parent.rightChild) # node is rightChild of its parent
                    node = parent
                    left_rotate!(tree, node)
                end
                # node is leftChild of its parent
                node.parent.color = false
                node.parent.parent.color = true
                right_rotate!(tree, node.parent.parent)
            end
        else # parent is the rightChild of grand_parent
            uncle = grand_parent.leftChild

            if (uncle.color) # uncle is red in color
                grand_parent.color = true
                parent.color = false
                uncle.color = false
                node = grand_parent
            else  # uncle is black in color
                if (node == parent.leftChild) # node is leftChild of its parent
                    node = parent
                    right_rotate!(tree, node)
                end
                # node is rightChild of its parent
                node.parent.color = false
                node.parent.parent.color = true
                left_rotate!(tree, node.parent.parent)
            end
        end
    end
    tree.root.color = false
end

"""
    insert!(tree, key)

Inserts `key` in the `tree` if it is not present.
"""
function Base.insert!(tree::RBTree{K}, d::K) where K
    # if the key exists in the tree, no need to insert
    haskey(tree, d) && return tree

    # insert, if not present in the tree
    node = RBTreeNode{K}(d)
    node.leftChild = node.rightChild = tree.nil

    insert_node!(tree, node)

    if node.parent == nothing
        node.color = false
    elseif node.parent.parent == nothing
        ;
    else
        fix_insert!(tree, node)
    end
    tree.count += 1
    return tree
end

"""
    push!(tree, key)

Inserts `key` in the `tree` if it is not present.
"""
function Base.push!(tree::RBTree{K}, key0) where K
    key = convert(K, key0)
    insert!(tree, key)
end

"""
    delete_fix(tree::RBTree, node::Union{RBTreeNode, Nothing})

This method is called when a black node is deleted because it violates the black depth property of the RBTree.
"""
function delete_fix(tree::RBTree, node::Union{RBTreeNode, Nothing})
    while node != tree.root && !node.color
         if node == node.parent.leftChild
            sibling = node.parent.rightChild
            if sibling.color
                sibling.color = false
                node.parent.color = true
                left_rotate!(tree, node.parent)
                sibling = node.parent.rightChild
            end

            if !sibling.rightChild.color && !sibling.leftChild.color
                sibling.color = true
                node = node.parent
            else
                if !sibling.rightChild.color
                    sibling.leftChild.color = false
                    sibling.color = true
                    right_rotate!(tree, sibling)
                    sibling = node.parent.rightChild
                end

                sibling.color = node.parent.color
                node.parent.color = false
                sibling.rightChild.color = false
                left_rotate!(tree, node.parent)
                node = tree.root
            end
        else
            sibling = node.parent.leftChild
            if sibling.color
                sibling.color = false
                node.parent.color = true
                right_rotate!(tree, node.parent)
                sibling = node.parent.leftChild
            end

            if !sibling.rightChild.color && !sibling.leftChild.color
                sibling.color = true
                node = node.parent
            else
                if !sibling.leftChild.color
                    sibling.rightChild.color = false
                    sibling.color = true
                    left_rotate!(tree, sibling)
                    sibling = node.parent.leftChild
                end

                sibling.color = node.parent.color
                node.parent.color = false
                sibling.leftChild.color = false
                right_rotate!(tree, node.parent)
                node = tree.root
            end
        end
    end
    node.color = false
    return nothing
end

"""
    rb_transplant(tree::RBTree, u::Union{RBTreeNode, Nothing}, v::Union{RBTreeNode, Nothing})

Replaces `u` by `v` in the `tree` and updates the `tree` accordingly.
"""
function rb_transplant(tree::RBTree, u::Union{RBTreeNode, Nothing}, v::Union{RBTreeNode, Nothing})
    if u.parent == nothing
        tree.root = v
    elseif u == u.parent.leftChild
        u.parent.leftChild = v
    else
        u.parent.rightChild = v
    end
    v.parent = u.parent
end

"""
   minimum_node(tree::RBTree, node::RBTreeNode)

Returns the RBTreeNode with minimum value in subtree of `node`.
"""
function minimum_node(tree::RBTree, node::RBTreeNode)
    (node === tree.nil) && return node
    while node.leftChild !== tree.nil
        node = node.leftChild
    end
    return node
end

"""
    delete!(tree::RBTree, key)

Deletes `key` from `tree`, if present, else returns the unmodified tree.
"""
function Base.delete!(tree::RBTree{K}, d::K) where K
    z = tree.nil
    node = tree.root

    while node !== tree.nil
        if node.data == d
            z = node
        end

        if d < node.data
            node = node.leftChild
        else
            node = node.rightChild
        end
    end

    (z === tree.nil) && return tree

    y = z
    y_original_color = y.color
    x = RBTreeNode{K}()
    if z.leftChild === tree.nil
        x = z.rightChild
        rb_transplant(tree, z, z.rightChild)
    elseif z.rightChild === tree.nil
        x = z.leftChild
        rb_transplant(tree, z, z.leftChild)
    else
        y = minimum_node(tree, z.rightChild)
        y_original_color = y.color
        x = y.rightChild

        if y.parent == z
            x.parent = y
        else
            rb_transplant(tree, y, y.rightChild)
            y.rightChild = z.rightChild
            y.rightChild.parent = y
        end

        rb_transplant(tree, z, y)
        y.leftChild = z.leftChild
        y.leftChild.parent = y
        y.color = z.color
    end

    !y_original_color && delete_fix(tree, x)
    tree.count -= 1
    return tree
end

Base.in(key, tree::RBTree) = haskey(tree, key)

"""
    getindex(tree, ind)

Gets the key present at index `ind` of the tree. Indexing is done in increasing order of key.
"""
function Base.getindex(tree::RBTree{K}, ind) where K
    @boundscheck (1 <= ind <= tree.count) || throw(ArgumentError("$ind should be in between 1 and $(tree.count)"))
    function traverse_tree_inorder(node::RBTreeNode{K}) where K
        if (node !== tree.nil)
            left = traverse_tree_inorder(node.leftChild)
            right = traverse_tree_inorder(node.rightChild)
            append!(push!(left, node.data), right)
        else
            return K[]
        end
    end
    arr = traverse_tree_inorder(tree.root)
    return @inbounds arr[ind]
end

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
include("InitFiles.jl")
include("AmqpConfigLoad.jl")

try
    loadConfig()
catch e
    if isa(e, FileNotFoundError)
        @info "Config file not found at: " * e.path
    else
        throw(e)
    end
end

if isfile(QUEUES_FILE_PATH)
    include(QUEUES_FILE_PATH)
    _queue_Tasks = execute_queues!()
end


text:
@test mysignif(SpecificG(5.0, 0.801_299_102E3), TestDigits) ≈ mysignif(SpecificG_Ph(5.0, 3500), TestDigits)
@test mysignif(SpecificG(5.0u"MPa", 0.801_299_102E3u"K"), TestDigits) ≈ mysignif(SpecificG_Ph(5.0u"MPa", 3500u"kJ/kg"), TestDigits)

@test mysignif(SpecificG(5.0, 0.101_531_583E4), TestDigits) ≈ mysignif(SpecificG_Ph(5.0, 4000), TestDigits)
@test mysignif(SpecificG(5.0u"MPa", 0.101_531_583E4u"K"), TestDigits) ≈ mysignif(SpecificG_Ph(5.0u"MPa", 4000u"kJ/kg"), TestDigits)

@test mysignif(SpecificG(25.0, 0.875_279_054E3), TestDigits) ≈ mysignif(SpecificG_Ph(25.0, 3500), TestDigits)
@test mysignif(SpecificG(25.0u"MPa", 0.875_279_054E3u"K"), TestDigits) ≈ mysignif(SpecificG_Ph(25.0u"MPa", 3500u"kJ/kg"), TestDigits)

@test mysignif(SpecificF(5.0, 0.801_299_102E3), TestDigits) ≈ mysignif(SpecificF_Ph(5.0, 3500), TestDigits)
@test mysignif(SpecificF(5.0u"MPa", 0.801_299_102E3u"K"), TestDigits) ≈ mysignif(SpecificF_Ph(5.0u"MPa", 3500u"kJ/kg"), TestDigits)

@test mysignif(SpecificF(5.0, 0.101_531_583E4), TestDigits) ≈ mysignif(SpecificF_Ph(5.0, 4000), TestDigits)
@test mysignif(SpecificF(5.0u"MPa", 0.101_531_583E4u"K"), TestDigits) ≈ mysignif(SpecificF_Ph(5.0u"MPa", 4000u"kJ/kg"), TestDigits)

@test mysignif(SpecificF(25.0, 0.875_279_054E3), TestDigits) ≈ mysignif(SpecificF_Ph(25.0, 3500), TestDigits)
@test mysignif(SpecificF(25.0u"MPa", 0.875_279_054E3u"K"), TestDigits) ≈ mysignif(SpecificF_Ph(25.0u"MPa", 3500u"kJ/kg"), TestDigits)

@test SpecificV(5.0, 0.801_299_102E3) ≈ SpecificV_Ph(5.0, 3500)
@test SpecificV(5.0u"MPa", 0.801_299_102E3u"K") ≈ SpecificV_Ph(5.0u"MPa", 3500u"kJ/kg")

@test SpecificV(5.0, 0.101_531_583E4) ≈ SpecificV_Ph(5.0, 4000)
@test SpecificV(5.0u"MPa", 0.101_531_583E4u"K") ≈ SpecificV_Ph(5.0u"MPa", 4000u"kJ/kg")

@test SpecificV(25.0, 0.875_279_054E3) ≈ SpecificV_Ph(25.0, 3500)
@test SpecificV(25.0u"MPa", 0.875_279_054E3u"K") ≈ SpecificV_Ph(25.0u"MPa", 3500u"kJ/kg")

@test SpecificU(5.0, 0.801_299_102E3) ≈ SpecificU_Ph(5.0, 3500)
@test SpecificU(5.0u"MPa", 0.801_299_102E3u"K") ≈ SpecificU_Ph(5.0u"MPa", 3500u"kJ/kg")

@test SpecificU(5.0, 0.101_531_583E4) ≈ SpecificU_Ph(5.0, 4000)
@test SpecificU(5.0u"MPa", 0.101_531_583E4u"K") ≈ SpecificU_Ph(5.0u"MPa", 4000u"kJ/kg")

@test SpecificU(25.0, 0.875_279_054E3) ≈ SpecificU_Ph(25.0, 3500)
@test SpecificU(25.0u"MPa", 0.875_279_054E3u"K") ≈ SpecificU_Ph(25.0u"MPa", 3500u"kJ/kg")

@test SpecificS(5.0, 0.801_299_102E3) ≈ SpecificS_Ph(5.0, 3500)
@test SpecificS(5.0u"MPa", 0.801_299_102E3u"K") ≈ SpecificS_Ph(5.0u"MPa", 3500u"kJ/kg")

@test SpecificS(5.0, 0.101_531_583E4) ≈ SpecificS_Ph(5.0, 4000)
@test SpecificS(5.0u"MPa", 0.101_531_583E4u"K") ≈ SpecificS_Ph(5.0u"MPa", 4000u"kJ/kg")

@test SpecificS(25.0, 0.875_279_054E3) ≈ SpecificS_Ph(25.0, 3500)
@test SpecificS(25.0u"MPa", 0.875_279_054E3u"K") ≈ SpecificS_Ph(25.0u"MPa", 3500u"kJ/kg")

@test SpecificCP(5.0, 0.801_299_102E3) ≈ SpecificCP_Ph(5.0, 3500)
@test SpecificCP(5.0u"MPa", 0.801_299_102E3u"K") ≈ SpecificCP_Ph(5.0u"MPa", 3500u"kJ/kg")

@test SpecificCP(5.0, 0.101_531_583E4) ≈ SpecificCP_Ph(5.0, 4000)
@test SpecificCP(5.0u"MPa", 0.101_531_583E4u"K") ≈ SpecificCP_Ph(5.0u"MPa", 4000u"kJ/kg")

@test SpecificCP(25.0, 0.875_279_054E3) ≈ SpecificCP_Ph(25.0, 3500)
@test SpecificCP(25.0u"MPa", 0.875_279_054E3u"K") ≈ SpecificCP_Ph(25.0u"MPa", 3500u"kJ/kg")

@test SpecificCV(5.0, 0.801_299_102E3) ≈ SpecificCV_Ph(5.0, 3500)
@test SpecificCV(5.0u"MPa", 0.801_299_102E3u"K") ≈ SpecificCV_Ph(5.0u"MPa", 3500u"kJ/kg")

@test SpecificCV(5.0, 0.101_531_583E4) ≈ SpecificCV_Ph(5.0, 4000)
@test SpecificCV(5.0u"MPa", 0.101_531_583E4u"K") ≈ SpecificCV_Ph(5.0u"MPa", 4000u"kJ/kg")

@test SpecificCV(25.0, 0.875_279_054E3) ≈ SpecificCV_Ph(25.0, 3500)
@test SpecificCV(25.0u"MPa", 0.875_279_054E3u"K") ≈ SpecificCV_Ph(25.0u"MPa", 3500u"kJ/kg")

@test SpeedOfSound(5.0, 0.801_299_102E3) ≈ SpeedOfSound_Ph(5.0, 3500)
@test SpeedOfSound(5.0u"MPa", 0.801_299_102E3u"K") ≈ SpeedOfSound_Ph(5.0u"MPa", 3500u"kJ/kg")

@test SpeedOfSound(5.0, 0.101_531_583E4) ≈ SpeedOfSound_Ph(5.0, 4000)
@test SpeedOfSound(5.0u"MPa", 0.101_531_583E4u"K") ≈ SpeedOfSound_Ph(5.0u"MPa", 4000u"kJ/kg")

@test SpeedOfSound(25.0, 0.875_279_054E3) ≈ SpeedOfSound_Ph(25.0, 3500)
@test SpeedOfSound(25.0u"MPa", 0.875_279_054E3u"K") ≈ SpeedOfSound_Ph(25.0u"MPa", 3500u"kJ/kg")


text:
export RedisCounter

struct RedisCounter{T} <: AbstractRedisCollection
    conn::AbstractRedisConnection
    key::String
end

RedisCounter(key) = RedisCounter{Int}(default_connection, key)

function getindex(rc::RedisCounter{T}) where T
    exec(rc.conn, "incr", rc.key) |> T
end


text:
struct FieldNameAndValuePair
    name::String
    value
end

function AbstractTrees.printnode(io::IO, x::FieldNameAndValuePair)
    if x.value isa Ptr && x.value == C_NULL
        Base.print(io, string(x.name, " -> ( 0x0 )"))
    else
        Base.print(io, x.name)
    end
end

function AbstractTrees.children(x::FieldNameAndValuePair)
    x.value isa Ptr && x.value == C_NULL && return ()
    return (simple_type_value(x.value), )
end

# function convert_to_node_type(::Val{T}) where T
#     str_name = string(T)
#     @assert startswith(str_name, "T_")
#     type_name = SubString(str_name, 3)
#     return eval(Meta.parse("SQLParser.$type_name"))
# end

function convert_to_node_type(::Val{T_List})
    return List
end

function AbstractTrees.printnode(io::IO, x::List)
    @assert x.type == T_List
    Base.print(io, string("List(length=",x.length,")"))
end

function AbstractTrees.children(x::Ptr)
    return ()
end

function AbstractTrees.printnode(io::IO, x::Value)
    actual_value = convert_to_actual_value(x)
    if actual_value === nothing
        Base.print(io, "null")
    else
        AbstractTrees.printnode(io, actual_value === nothing ? C_NULL : actual_value)
    end
end