# How to set an objective to minimize the variance in ratio of allocation

**URL:** <https://discourse.julialang.org/t/how-to-set-an-objective-to-minimize-the-variance-in-ratio-of-allocation/100263>\
**Category:** Optimization (Mathematical)\
**Created:** [June 13, 2023, 3:20am UTC](https://discourse.julialang.org/t/how-to-set-an-objective-to-minimize-the-variance-in-ratio-of-allocation/100263 "2023-06-13T03:20:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashok\_Khatri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashok_khatri/32/50714_2.png) [@Ashok\_Khatri](https://discourse.julialang.org/u/Ashok_Khatri)\
**Post date:** [June 13, 2023, 3:20am UTC](https://discourse.julialang.org/t/how-to-set-an-objective-to-minimize-the-variance-in-ratio-of-allocation/100263/1 "2023-06-13T03:20:40Z")

</div>

I am trying to write a model where I have to distribute fractions of rent from properties to loans. The objective is to distribute the rents as equally as possible given the loan balance.

Say there are 5 properties and 8 loans, I have 40 variables X11, X12, X13…X18; X21, X22, X23…X28;…; X51, X52,…X58

Loan Balances are L1, L2, L3, L4 …L8 Rents from properties are R1,R2, R3, R4 and R5

So Minimize ( Absolute Value (R1 _X11/L1 + R2_X21/L1 + R3_X31/L1 +R4_X41/L1 +R5_X51/L1 - AverageRentLoanRatio) + Abolute Value (R1 X12/L2 + R2X22/L2 + R3_X32/L2 +R4_X42/L2 +R5_X52/L2 - AverageRentLoanRatio)+ Abolute Value (R1 _X13/L3 + R2_X23/L3 + R3_X33/L3 +R4_X43/L3 +R5_X53/L3 - AverageRentLoanRatio)+ Abolute Value (R1 X14/L4 + R2X24/L4 + R3_X34/L4 +R4_X44/L4 +R5_X54/L4 - AverageRentLoanRatio) + Abolute Value (R1 _X15/L5 + R2_X25/L5 + R3_X35/L5 +R4_X45/L5 +R5\*X55/L5 - AverageRentLoanRatio))

With Constraints such as a property cannot be allocated to a specific property and total rent is 100%

I am able to define the constraints but struggling how to write the objective.

Here is the model I have written so far

```julia
using CSV
using DataFrames
C= CSV.read("Property.csv", DataFrame)
L = CSV.read("LoanFile.csv", DataFrame)

using JuMP, GLPK

# Preparing an optimization model
m = Model(GLPK.Optimizer)

#getting the number of properties and loans
index_i = 1:nrow(C)
index_j = 1:ncol(C)-2

#setting the number of constraints
#index_constraints = 1:nrow(C)* (ncol(C)-2)

# Declaring variables
@variable(m, x[index_i, index_j] >= 0)

# Declaring bound Constraints
@constraint(m, conb[i=index_i, j=index_j],x[i, j] <= C[i,j+2])

#Declaring total 100 % constraints
@constraint(m,cont[i=index_i],sum(x[i, j] * C[i,2] for j in index_j) <= C[i,2])

# Setting the objective
Average_KDF = sum(C[i,2] for i in 1:nrow(C))/sum(L[i,2] for i in 1:nrow(L))

KDFratio = Array{Float64}(undef,nrow(C),ncol(C)-2)
KDFsumRatio = Array{Float64}(undef,nrow(L))

#@objective(m, Min,sum(abs((x[i, j] * C[i,2]/L[j,2]- Average_KDF) for j in index_j) for i in index_i)
# the above setting of objective is not working and I am struggling to formulate this.
  

# Printing the prepared optimization model

print(m)

```

Here are the files I am using for loans and properties

```julia
|﻿LoanID | Balance |
|L 1 | 100.00 |
|L 2 | 100.00 |
|L 3 | 300.00 |
|L 4 | 100.00 |
|L 5 | 50.00 |
|L 6 | 50.00 |
|L 7 | 50.00 |
|L 8 | 50.00 |

|﻿PropertyID | Rent(R) |L1|L2|L3|L4|L5|L6|L7|L8|
|P 1 | 5.00 | 1| 0| 0| 0| 0| 0| 0| 0|
|P 2 | 10.00 | 0| 1| 1| 0| 0| 0| 0| 0|
|P 3 | 1.5 | 1| 1| 0| 0| 0| 0| 0| 0|
|P 4 | 10.50 | 1| 1| 1| 1| 0| 0| 0| 0|
|P 5 | 20.00 | 0| 1| 0| 1| 0| 1| 0| 1|

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 13, 2023, 5:15am UTC](https://discourse.julialang.org/t/how-to-set-an-objective-to-minimize-the-variance-in-ratio-of-allocation/100263/2 "2023-06-13T05:15:48Z")

</div>

Hi @Ashok_Khatri, welcome aboard!

> [@Ashok\_Khatri](#):
>
> I am able to define the constraints but struggling how to write the objective.

I think that is because your objective is nonlinear. Check out the [associated docs](https://jump.dev/JuMP.jl/stable/manual/nlp/) to see how that is handled. JuMP has a particular syntax, which does not allow you to specify just any function as a constraint or objective.

As a side note, you will also need to change solver, because GLPK is made for linear programs 🙂

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [June 13, 2023, 8:16am UTC](https://discourse.julialang.org/t/how-to-set-an-objective-to-minimize-the-variance-in-ratio-of-allocation/100263/3 "2023-06-13T08:16:42Z")

</div>

Hi @ashok_Khatri, welcome to the forum.

I haven’t tested this locally so there might be typos, but this should point you in the right direction:

```julia
using JuMP
import CSV, DataFrames, HiGHS
C = CSV.read("Property.csv", DataFrames.DataFrame)
L = CSV.read("LoanFile.csv", DataFrames.DataFrame)
N = size(C, 1)
model = Model(HiGHS.Optimizer)
@variable(model, 0 <= x[i=1:N, j=1:N-2] <= C[i, j+2])
@constraint(model, [i=1:N], sum(C[i, 2] * x[i, j] for j in 1:N-2) <= C[i, 2])
Average_KDF = sum(C[:, 2]) / sum(L[:, 2])
@expression(
    model, 
    y[i=1:N], 
    sum(x[i, j] * C[i, 2] / L[j, 2] - Average_KDF for j in 1:(N-2)),
)
# To compute t >= sum | y_i |, use NormOneCone, see:
# https://jump.dev/JuMP.jl/stable/tutorials/linear/tips_and_tricks/#L1-norm
@variable(model, t >= 0)
@constraint(model, [t; y] in MOI.NormOneCone(1 + length(y)))
@objective(model, Min, t)

```

> I think that is because your objective is nonlinear

The objective is linear, albeit with a `minimize abs(x)` formulation 😄

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 13, 2023, 8:19am UTC](https://discourse.julialang.org/t/how-to-set-an-objective-to-minimize-the-variance-in-ratio-of-allocation/100263/4 "2023-06-13T08:19:48Z")

</div>

> [@odow](#):
>
> The objective is linear, albeit with a `minimize abs(x)` formulation 😄

Oh right of course, I read the post too fast and thought the `Li` were variables too, based on the title which mentioned ratios.

---

<div class="post-metadata">

**Author:** ![Ashok\_Khatri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashok_khatri/32/50714_2.png) [@Ashok\_Khatri](https://discourse.julialang.org/u/Ashok_Khatri)\
**Post date:** [June 14, 2023, 1:24pm UTC](https://discourse.julialang.org/t/how-to-set-an-objective-to-minimize-the-variance-in-ratio-of-allocation/100263/5 "2023-06-14T13:24:37Z")

</div>

Thanks a lot, @odow, that was very helpful. Appreciate your quick response.  
I was able to take the code you posted make some edits and run it and it gave me the results I was expecting.

Thanks @gdalle for the references to non-linear objectives. These will help with other problems.
