Homework

Electric vehicle charging

Solve the problem of distributing a limited power to N electric vehicle chargers throughout the next K hours. Specifically, you are supposed to find an hourly-sampled optimal plan for each car that minimizes the total cost of the charging.

You are given a time-dependent maximum energy a[k] available for charging (in kWh), a time-dependent cost of the energy c[k] (in €/kWh), maximum allowed charging energy per hour for the ith car m_i (in kWh), total requested energy r_i for the ith car (also in kWh), and the departure (discrete) time d_i for each car.

All the cars are connected to chargers and can start charging from time 1. Departure time is the time when the energy charged to the ith car has reached at least r_i and charging of the car must stop.

Let’s emphasize: the index k is a time index running from 1 to K, and the index i specifies the corresponding car, i.e. i\in\{1,2,\ldots, N\}

Formulate this task as an optimization problem, identify the class of this optimization problem (LP, QP or NLP) and solve it by completing the following Julia script and heeding the following instructions.

  • Model the optimization problem either trough JuMP or Convex.
  • Solve it using one of the available solvers: HiGHS, SCS, Ipopt.
  • Upload only a single file named hw.jl as your solution.
using JuMP # or Convex

using HiGHS, SCS, Ipopt # Available solvers

"""
    find_optimal_charging_plan(
        a::Vector{Float64},
        c::Vector{Float64},
        m::Vector{Float64},
        r::Vector{Float64},
        d::Vector{Int64}
    )

Computes an optimal charging schedule for `N` electric vehicles over `K` hours.

# Arguments
- `a`: A `K`-element vector specifying the maximum available charging energy per hour (kWh).
- `c`: A `K`-element vector representing the cost of charging per hour (€/kWh).
- `m`: An `N`-element vector with the maximum allowed charging energy for each vehicle (kWh).
- `r`: An `N`-element vector specifying the total energy required by each vehicle (kWh).
- `d`: An `N`-element vector indicating the departure time (hour) of each vehicle.

# Returns
A tuple containing:
- An `N × K` matrix representing the optimal charging schedule (kWh allocated per vehicle per hour).
- The optimal total charging cost (€).
- A symbol indicating the type of optimization problem solved (`:LP`, `:QP`, or `:NLP`).
"""
function find_optimal_charging_plan(
    a::Vector{Float64},
    c::Vector{Float64},
    m::Vector{Float64},
    r::Vector{Float64},
    d::Vector{Int64}
)

    K = length(a) # Timespan (hours)
    N = length(m) # Number of vehicles

    # TODO model and solve the problem

    return zeros(N, K), 0.0, :NLP # or :LP or :QP

end

The data you can use to test your solution is given in the following tables.

Data for the three vehicles: maximum energy per hour, total requested energy, and the departure time
Car m_i (kWh) r_i (kWh) d_i
1 6 15 3
2 6 25 7
3 4 30 10
Evolution in time of the maximum available energy and the cost of the energy
Time 1 2 3 4 5 6 7 8 9 10
a[k] 11.6 11.9 10.6 8.8 8.0 8.8 10.6 11.9 11.6 10.0
c[k] 0.58 0.72 0.92 0.68 0.54 0.78 0.64 0.57 0.74 0.74
Back to top