Added Project and Report
This commit is contained in:
42
project/utilities/dataset.jl
Normal file
42
project/utilities/dataset.jl
Normal file
@ -0,0 +1,42 @@
|
||||
using CSV
|
||||
using DataFrames
|
||||
using LinearAlgebra
|
||||
|
||||
@doc raw"""
|
||||
get_dataset(file_path::String, lambda::Float64)
|
||||
|
||||
Reads the ML-CUP-23/24 dataset from the given file path and returns the
|
||||
necessary matrices and vectors to solve the problem. The LS problem is
|
||||
solved with the standard solver in Julia.
|
||||
|
||||
### Input
|
||||
|
||||
- 'file_path' -- the path to the dataset file.
|
||||
- 'lambda' -- the regularization parameter.
|
||||
|
||||
### Output
|
||||
|
||||
A tuple containing
|
||||
|
||||
- 'X_hat' -- the augmented matrix X.
|
||||
- 'y_hat' -- the augmented vector y.
|
||||
- 'start' -- the initial starting point.
|
||||
- 'w_star' -- the optimal solution.
|
||||
|
||||
"""
|
||||
function get_dataset(filePath::String, λ::Real)
|
||||
X = CSV.File(filePath; header=false) |> DataFrame
|
||||
X = Matrix(X)
|
||||
|
||||
m, n = size(X)
|
||||
X_hat = [transpose(X); λ .* I(m)]
|
||||
y_hat = [(rand(n).*2 .-1); zeros(m)]
|
||||
|
||||
# initial starting point
|
||||
start = ones(m)
|
||||
|
||||
# w_star is the optimal solution provided by the julia solver
|
||||
w_star = X_hat \ y_hat
|
||||
|
||||
return X_hat, y_hat, start, w_star
|
||||
end
|
||||
91
project/utilities/genFunc.jl
Normal file
91
project/utilities/genFunc.jl
Normal file
@ -0,0 +1,91 @@
|
||||
include("dataset.jl")
|
||||
using Random
|
||||
|
||||
datasetpath = joinpath(@__DIR__, "../data_for_testing/dataset.csv")
|
||||
VALID = [:dataset, :randDataset, :exactRandDataset, :rand, :exactRand]
|
||||
|
||||
@doc raw"""
|
||||
genFunc(t::Symbol, [lambda::Real=1, m::Integer=1000, n::Integer=10, rng=default_rng()])
|
||||
|
||||
Generates different matrices for the least squares problem
|
||||
|
||||
### Input
|
||||
|
||||
- `t` -- The type of problem to generate. Possible values are:
|
||||
`:dataset`,
|
||||
`:randDataset`,
|
||||
`:exactRandDataset`,
|
||||
`:rand`,
|
||||
`:exactRand`
|
||||
|
||||
### Output
|
||||
|
||||
A named tuple with the matrix ``\hat{X}``, the vector ``\hat{y}``, the starting vector and the solution vector ``w^*``.
|
||||
|
||||
See also [`gen_dataset`](@ref).
|
||||
"""
|
||||
function genFunc(
|
||||
t::Symbol;
|
||||
λ::Real=1,
|
||||
m::Integer=1000,
|
||||
n::Integer=10,
|
||||
rng=Random.default_rng())::NamedTuple
|
||||
if t ∉ VALID
|
||||
throw(ArgumentError("The type of matrix to produce is not recognized, available types: " * join(String.(VALID), ", ")))
|
||||
end
|
||||
|
||||
if t == :dataset
|
||||
tmp = get_dataset(datasetpath, λ)
|
||||
return (; :X_hat => tmp[1], :y_hat => tmp[2], :start => tmp[3], :w_star => tmp[4])
|
||||
end
|
||||
|
||||
if t == :randDataset
|
||||
X_hat = [(rand(rng, n, m).*2 .-1); λ .* I(m)]
|
||||
y_hat = [(rand(rng, n).*2 .-1); zeros(m)]
|
||||
|
||||
# initial starting point
|
||||
start = ones(m)
|
||||
|
||||
# w_star is the optimal solution of the julia solver
|
||||
w_star = X_hat \ y_hat
|
||||
|
||||
return (; :X_hat => X_hat, :y_hat => y_hat, :start => start, :w_star => w_star)
|
||||
end
|
||||
|
||||
if t == :exactRandDataset
|
||||
X_hat = [(rand(rng, n, m).*2 .-1); λ .* I(m)]
|
||||
w_star = rand(rng, m).*2 .-1
|
||||
|
||||
# initial starting point
|
||||
start = ones(m)
|
||||
|
||||
y_hat = X_hat * w_star
|
||||
|
||||
return (; :X_hat => X_hat, :y_hat => y_hat, :start => start, :w_star => w_star)
|
||||
end
|
||||
|
||||
if t == :rand
|
||||
X_hat = rand(rng, m+n, m).*2 .-1
|
||||
y_hat = rand(rng, m+n)
|
||||
|
||||
# initial starting point
|
||||
start = ones(m)
|
||||
|
||||
# w_star is the optimal solution of the julia solver
|
||||
w_star = X_hat \ y_hat
|
||||
|
||||
return (; :X_hat => X_hat, :y_hat => y_hat, :start => start, :w_star => w_star)
|
||||
end
|
||||
|
||||
if t == :exactRand
|
||||
X_hat = rand(rng, m+n, m).*2 .-1
|
||||
w_star = rand(rng, m).*2 .-1
|
||||
|
||||
# initial starting point
|
||||
start = ones(m)
|
||||
|
||||
y_hat = X_hat * w_star
|
||||
|
||||
return (; :X_hat => X_hat, :y_hat => y_hat, :start => start, :w_star => w_star)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user