Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Scientific Calculations with Julia/Personal Budget Optimization

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Scientific Calculations with Julia Personal Budget Optimization Contents Profit Maximization Personal budget optimization is a classic operations research problem, which requires defining spending variables, imposing budget constraints, and choosing an objective function. Specifically, a linear or nonlinear model with logarithmic utility (objective function) can be constructed, which must be maximized. In mathematical terms the linear problem is with n expenditure categories: the non-linear one: Where: is the expense allocated to the category is the weight (relative importance) of the category is the total available budget is the mandatory minimum spend for the category is the maximum allowable expense for the category Utility weights represent how much a certain expense contributes to the well-being or satisfaction of person. For example, suppose there are three expense categories: | Expense | Utility weight | | ------ | -------- | | food | 3 | | entertainment | 1 | | travel | 4 | Therefore: €1 spent on travel provides more satisfaction €1 spent on food provides average utility €1 spent on entertainment provides less utility Therefore, the nonlinear model will try to allocate more budget to the categories with greater weight, maximizing: with the related budget constraints of the type: In the linear model, however, the following is maximized: with: and The weights are chosen by subjective evaluation or by observing where the most is spent and what It is preferable to sacrifice. In the nonlinear model, the logarithmic function is used because it can be shown that the optimal solution to the problem in the unconstrained case is: That is, each spending category receives a share proportional to its weight regardless of the total budget B. In the linear model, the optimal solution is always at the vertices of the constraint polyhedron, meaning each category is set to its minimum or maximum, never in the middle. This is a fundamental result of linear programming. With logarithmic utility, however, the solution is internal, meaning each category receives an intermediate allocation. Implementation in Julia First, I install the Julia libraries I need: using Pkg Pkg.add("JuMP") Pkg.add("Ipopt") Pkg.add("HiGHS") Linear Model The utility-maximizing spending variables are calculated, subject to the various constraints, using the HiGHS library: using JuMP, HiGHS total_budget = 3000.0 # € monthly model = Model(HiGHS.Optimizer) @variable(model,800 <= rent <= 1000) @variable(model,300 <= food <= 600) @variable(model,100 <= transportation <= 300) @variable(model,0 <= leisure <= 400) @variable(model,100 <= savings <= 800) @variable(model,0 <= clothes <= 200) @constraint(model, rent + food + transportation + leisure + savings + clothes <= total_budget) @objective(model, Max, 0.8*rent + 1.5*food + 1.2*transport + 2*leisure + 1.8*savings + 0.4*clothes) optimize!(model) println("______________________________________________________________") println("Rent = ", value(rent)) println("Food = ", value(food)) println("Transportation = ", value(transportation)), println("Leisure = ", value(leisure)) println("Savings = ", value(savings)) println("Clothes = ", value(clothes)) Running HiGHS 1.13.1 (git hash: 1d267d97c): Copyright (c) 2026 under Apache 2.0 license terms Using BLAS: blastrampoline LP has 1 row; 6 cols; 6 nonzeros Coefficient ranges: Matrix [1e+00, 1e+00] Cost [4e-01, 2e+00] Bound [1e+02, 1e+03] RHS [3e+03, 3e+03] Presolving model 1 rows, 1 cols, 1 nonzeros 0s 0 rows, 0 cols, 0 nonzeros 0s Presolve reductions: rows 0(-1); columns 0(-6); nonzeros 0(-6) - Reduced to empty Performed postsolve Solving the original LP from the solution after postsolve Model status : Optimal Objective value : 4.2200000000e+03 P-D objective error : 0.0000000000e+00 HiGHS run time : 0.00 ______________________________________________________________ Rent = 900.0 Food = 600.0 Transportation = 300.0 Leisure = 400.0 Savings = 800.0 Dress upti = 0.0 These values ​​maximize the utility function subject to the constraints. Nonlinear Model Calculate the expenditure variables that maximize logarithmic utility subject to the various constraints using the Ipopt library: using JuMP, Ipopt total_budget = 3000.0 # € monthly model_nl = Model(Ipopt.Optimizer) set_optimizer_attribute(model_nl, "print_level", 0) @variable(model_nl,800 <= rent <= 1000) @variable(model_nl,300 <= food <= 600) @variable(model_nl,100 <= transportation <= 300) @variable(model_nl,0 <= entertainment <= 400) @variable(model_nl,100 <= savings <= 800) @variable(model_nl,0 <= clothes <= 200) @constraint(model_nl, rent + food + transportation + entertainment + savings + clothes <= total_budget) # Logarithmic utility: U(x) = w_i * log(x_i) @objective(model_nl, Max, 0.8*log(rent) + 1.5*log(food) + 1.2*log(transportation) + 2*log(entertainment) + 1.8*log(savings) + 0.4*log(clothes)) optimize!(model_nl) println("Rent = ", value(rent)) println("Food = ", value(food)) println("Transportation = ", value(transportation)), println("Entertainment = ", value(entertainment)) println("Savings = ", value(savings)) println("Clothes = ", value(clothes)) ****************************************************************************** This program contains Ipopt, a library for large-scale nonlinear optimization. Ipopt is released as open source code under the Eclipse Public License (EPL). For more information, visit https://github.com/coin-or/Ipopt ********************************************************************************** Rent = 800.0086863353699 Food = 599.4405027557945 Transportation = 299.9919466163567 Entertainment = 399.9950988314647 Savings = 736.8257493821642 Clothing = 163.7329107226028 These values ​​maximize the utility function subject to the constraints.