Abstract. The goal of this work is to study the optimal controls for the COVID-19 epidemic in Brazil. We consider an agestructured SEIRQ model with quarantine compartment, where the controls are the quarantine entrance parameters. We then compare the optimal controls for different quarantine lengths and distribution of the total control cost by assessing their respective reductions in deaths in comparison to the same period without quarantine. The best strategy provides a calendar of when to relax the isolation measures for each age group. Finally, we analyse how a delay in the beginning of the quarantine affects this calendar by changing the initial conditions.
At the end of 2019 a novel coronavirus emerged in the city of Wuhan, China. In January 2020 the disease was given the name COVID-19 and, by mid February, China already faced over 60 thousand cases [26] . Many scientists then began to model disease to forecast its worldwide impact ( [23] , [5] ), even influencing policies of many governments.
As the disease spread across Europe and the United States, some countries were forced to implement quarantines or even lockdowns to mitigate the harms and were able to do the so called "flattening of the curve", i.e., to postpone and dampen the maximum number of active cases. By mid May, Brazil is entering this stage, with some states declaring stricter quarantine policies.
Optimal control theory has been applied to general epidemic models ( [1] , [18] ) as well as to specific disease models such as HIV ( [10] , [6] , [13] ), tuberculosis ( [21] , [12] ) and influenza ( [15] ). Recently, a few works applying optimal control theory to the COVID-19 pandemic, such as [7] and [4] , have appeared. This paper focuses on a SEIR model with quarantine as was proposed in [9] , but dividing the population in age groups as in [2] . This is particularly important since COVID-19 has worse consequences on the elderly than it does on younger people.
Our goal is to calculate the optimal quarantine strategies numerically for different choices of parameters in the model, which reflect the decisions governments must make when implementing these policies, such as evaluating the economical costs of the quarantine for each of the age groups and when to start implementing the measures. Then, we compare the controls by looking at how they reduce deaths in comparison to the same period without quarantine. The best strategy gave us a calendar of when to relax the measures in each of the age groups.
Our model consists of a classical SEIR model with a quarantined class. Besides, we assume that the population has an age structure (see [22] , [8] for models with a continuous age structure and [25] , [24] for models with a discrete one). There are three age groups, described in Table 1 .
and Q i (t) be the number of susceptible, exposed, infected, recovered and quarantined individuals in each age group at time t ≥ 0, respectively. We assume that the total population 
All parameters are nonnegative. β ij is the transmission coefficient from age group i to age group j. Typically, it will be assumed that β ij = β ji for all i, j. σ i and γ i are the latency and recovery periods, respectively, for age group i. λ is the exit rate from the quarantine. Our controls are the u i (t), which denote the percentage of susceptible individuals in each age group that are put into quarantine at time t. As such, they satisfy, a priori,
However, it is unrealistic to expect an entire population to stay under quarantine for a long time. There are essential workers such as healthcare professionals and police officers that cannot stay at home during these times. As most of these workers are in age group 2, we suppose that all of age groups 1 and 3 can be quarantined (for age group 3, indeed, this is especially important since they are a risk group for the COVID-19 pandemic). Thus, we shall loosen (2.2) by considering, for example, 0 ≤ u 2 (t) ≤ u max . Evaluating u max is one of the tasks of each government's authorities. In this paper, we fix this parameter at u max = 0.9. This means that
be the total population of age group i. Adding the equations in the system above, we see that N i (t) is also constant for i ∈ {1, 2, 3}. Since R i (t) only appears in the other equations as a part of N i (t), we substitute the equations for R i i(t) by N i (t).. Hence, we may also consider the system of (2.4)
Parameter values are taken from [2] and are listed in Table 2 . The data fitting was performed using an adaptation of a least-squares algorithm from [17] .
To see how the numbers of infections and recoveries are distributed in the three age groups, we refer to the data available in [3] , shown in Table 3 . For simplification, we suppose that the difference between the number of cases and the number of deaths represents the number of Table 2 . Parameter values (data from [2] ).
Parameter Value Parameter Value β 11 1.76168 σ 1 0.27300
recoveries. This is not necessarily correct, because some of the patients that we considered as recovered might still carry the disease, but we use this approach due to the scarcity of information regarding recoveries we currently have. The respective distributions are shown in Table 4 . According to [26] , Brazil had as of May 13, 2020 a total of 97,575 active COVID-19 cases. Even though there seems to be a large subnotification in the country [20] , this number will be considered as the total number of infected individuals nonetheless. To estimate the number of exposed cases, we look at data from May 8, 2020, since the mean incubation period of the disease is thought to be around 5 days [14] . Once again according to [26] , at this time Brazil had 76,603 active cases, so this gives us an estimation of 20,972 exposed cases. We also suppose that these cases follow the age distributions of cases from Table 4 . Finally, as of May 8, there were 65,124 recovered cases in Brazil [26] .
Therefore, our initial time will consist of data from Brazil as of May 8, 2020. The total population is assumed to be 200 million, divided into 40% young people, 50% adults and 10% elderly. We also assume that there are no quarantined individuals when the simulation starts. Since the numbers of exposed, infected and recovered are very small in comparison to the total population, we assume that the initial number of susceptible individuals is equal to the total population of the respective age group. The initial conditions of all variables, rounded to the nearest integers, are listed in Table 5 .
Using system (2.4), we consider the functional to be minimized as Quarantined 0 0 0
In the formula above, T is the quarantine duration and the parameters B i are the costs of the control. We assume that B i > 0 for i ∈ {1, 2, 3} and that
where B ∈ R is the total control cost. Sufficient conditions for the existence of the optimal controls follow from standard results from optimal control theory. For instance, we can use Theorem 2.1 in [11] to show that the optimal control exists. Pontryagin's maximum principle ( [19] , [16] ) establish that optimal controls are solutions of the Hamiltonian system with Hamiltonian function
where λ S i , λ E i , λ I i , λ Q i and λ N i are the adjoint variables. These variables must satisfy the adjoint equations
where i ∈ {1, 2, 3} and C ∈ {S, E, I, Q, N }. The adjoint system is detailed in (3.5) below.
(3.5)
The adjoint variables also must satisfy the transversality conditions
Finally, the optimality conditions come from solving
This results in
Since we are considering bounded controls (because of (2.3)), the u * i are calculated using
where u 1 max = u 3 max = 1 and u 2 max = 0.9. Uniqueness of the optimal controls (at least for small enough T ) also follow from standard results, such as Theorem 2.3 in [11] . Numerical solutions of systems (2.4) and (3.5) can be found by a forward-backward sweep method [16] . The algorithm starts with an initial guess of the controls u 1 , u 2 and u 3 and then solves (2.4) forward in time. After this first part, it uses the results and the initial guesses to solve (3.5) backward in time and new controls are defined following (3.9). This process continues until it converges.
Quarantines are not just a matter of public health, for they also present economic questions, for example. This means that we must pay close attention to the control costs B i . These numbers reflect how the population is capable of dealing with the quarantine of the respective age group. Smaller values of B i mean that the population can withstand a stricter quarantine without many economical side effects. This is not possible, on the other hand, for bigger values of B i .
Since the bigger economic toll of the quarantine lies on the adults (because they form almost all of the economic active population), we assume that B 2 is the greatest of the three values. As the isolation of the young implies closing schools, this educational impact makes B 1 the second highest cost, albeit much smaller than B 2 .
How the total cost B is distributed among the age groups determines the shape of the optimal controls. To study this relationship, we let B = 5000 and define four cost distributions D 1 , D 2 , D 3 and D 4 , detailed in Table 6 . Table 6 . The four distributions considered for the total control B.
Distribution adults, then for the young and lastly for the elderly. The differences lie in the times it takes for these relaxations, which are described in Table  7 .
Our goal now is to compare these distributions by investigating the number of deaths caused by the pandemic at the end of the quarantine for each one of them. As in [2] , the deaths will be calculated as a fraction of the recovered, since there is no disease induced mortality in our model. From Table 3 , we can derive the case fatality rates µ 1 , µ 2 and µ 3 for age groups 1, 2 and 3, respectively. The results are (4.1)
= 0.003, µ 2 = 891 113059 = 0.008, µ 3 = 17948 121928 = 0.147.
Let D denote the number of deaths at the end of the quarantine due to the disease. By our discussion above, we can write
Because of the uncertain nature of the parameters and due to the high number of unreported cases, we do not show the crude numbers of D for the four distributions. The approach we use is to select the smallest values as unit and then scale the other values accordingly. The results are displayed in Table 8 . We also included D 0 , representing the outcome of no quarantine. Table 8 . Proportion of deaths in comparison to the lowest outcomes. On the one hand, we see that all the optimal controls reduces the number of deaths in more than 100 times in comparison to "doing nothing" scenario. On the other hand, the four distributions produce very similar results, with D 4 being slightly better for both quarantine lengths.
For a better visualisation, we plot the graphs of the total number of infections for distribution D 4 in Figure 2 .
Both curves reach their minimum around the same time that the controls reach zero. For a quarantine of 60 days, the number of cases at the end is, indeed, much smaller than they are in the beginning. However, for a shorter quarantine, there could even be more cases than the initial total. This fact shows that quarantines cannot be too short, or else the overall situation in the end could be worse than in the beginning. Moreover, the number of cases start to go up again towards the end.
To finish this Section, we analyse how the initial conditions affect the optimal control. We can interpret this as a way to see what happens if it takes too long for these measures to be implemented. We do this by considering initial conditions of exposed, infected and recovered twice and four times as much as their original values. As of May 13, 2020, the number of active cases in Brazil doubles every 10 days [26] , so this means waiting 10 or 20 days, respectively, to start the quarantine. In the plots of Figure 3 , we consider distribution D 4 and quarantine length of 60 days. The plot on the left of Figure 3 is the same as the last one in 1. The quarantine is relaxed for the young, adults and elderly after 13, 11 and 47 days, respectively. The plot in the middle has the initial conditions of exposed, infected and recovered multiplied by two. Now, the quarantine is relaxed only after 27, 26 and 53 days, respectively. The last plot multiplies the initial conditions by four. The relaxations happen, respectively, after 39, 38 and 54 days.
This shows that the quarantines have to be much stricter if there is a delay in their implementation. Moreover, applying (4.2) with and without quarantine, we see that the deaths are reduced by factors of 79 and 40 when initial conditions are multiplied by 2 and 4, respectively. With an earlier quarantine entrance, Table 8 showed that the reduction was by a factor of 155 for distribution D 4 , so the longer it takes for the quarantine to start, the less effective it is.
In this paper we considered an age-structured SEIRQ model, where the quarantine entrance parameters are thought as controls of the system, and we looked for the optimal controls via Pontryagin's maximum principle. After writing down the optimality system, we calculated the optimal controls numerically and analysed how some of the parameters influence the results.
These parameters represent the difficult choices authorities must make, such as deciding how many essential workers are allowed to remain circulating, estimating the economic impact of the quarantine and even when to start it. As such choices are made, the optimal controls give guidelines of how to proceed.
In Section 4, we considered a constant total control cost and distributed it among the age groups in four ways. The distribution with the best results with regard to deaths during the quarantine gave us a calendar of when to relax the isolation measures in the three age groups (for quarantine lengths of 30 and 60 days, respectively):
• For the youngs, the date of relaxation was the 15th or the 13th day. • For the adults, the relaxation started at the 12th day or the 11th day. • For the elderly, it started at the 25th day or the 47th day. The optimal controls that induce this calendar produced a reduction in the number of deaths of 110 and 155 times, respectively, in comparison to the same period of time without quarantine. However, in both cases the number of infected cases reached a minimum just before the end of the simulation, so by the time the quarantine ended, the cases were going up again, even becoming bigger than the original values for the shorter length we considered. This shows that the quarantines are not effective if they are not long enough.
We also showed that waiting too long to start the quarantine makes the period before the relaxation become longer. This also produced a loss in efficacy, since the reduction of deaths due to the quarantine (in comparison to the "doing nothing" scenario) appeared to be approximately inversely proportional to the number of initial cases.
In our model we used data from Brazil as initial conditions. Brazil is a very large country, with many cities at different stages of the pandemic. This means that studies such as this one should be made locally to best suit the characteristics of each city. As the plots of Figure 3 suggest, the sooner the quarantine is implemented, the shortest the time the controls need to stay at their maximum is.