-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Julian's Small Example\n", | ||
"\n", | ||
"Julian came up with a small $n = 2$ example (excluding sex data) for testing the theory behind solving the problem. This example can easily be extended and then solved using the AlphaRGS module. This is a quick demonstration of using the module within an interactive notebook environment." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import alphargs" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Problem Variables\n", | ||
"\n", | ||
"The variables for Julian's $n = 2$ problem are as follows:\n", | ||
"$$\n", | ||
" \\Sigma = \\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix},\\quad\\\n", | ||
" \\bar{\\mu} = \\begin{bmatrix} 1 \\\\ 2 \\end{bmatrix},\\quad\\\n", | ||
" \\Omega = \\begin{bmatrix} \\frac{1}{9} & 0 \\\\ 0 & 4 \\end{bmatrix}.\n", | ||
"$$\n", | ||
"This can easily be extended to include sex data by reflecting those variable across both sires and dams, filling in the remainder with zeros. \n", | ||
"$$\n", | ||
" \\Sigma = \\begin{bmatrix}\n", | ||
" 1 & 0 & 0 & 0 \\\\\n", | ||
" 0 & 1 & 0 & 0 \\\\\n", | ||
" 0 & 0 & 1 & 0 \\\\\n", | ||
" 0 & 0 & 0 & 1\n", | ||
" \\end{bmatrix},\\quad\\\n", | ||
" \\bar{\\mu} = \\begin{bmatrix} 1 \\\\ 1 \\\\ 2 \\\\ 2 \\end{bmatrix},\\quad\\\n", | ||
" \\Omega = \\begin{bmatrix}\n", | ||
" \\frac{1}{9} & 0 & 0 & 0 \\\\\n", | ||
" 0 & \\frac{1}{9} & 0 & 0 \\\\\n", | ||
" 0 & 0 & 4 & 0 \\\\\n", | ||
" 0 & 0 & 0 & 4\n", | ||
" \\end{bmatrix},\\quad\\\n", | ||
" \\mathcal{S} = \\lbrace 1, 3 \\rbrace,\\quad\\\n", | ||
" \\mathcal{D} = \\lbrace 2, 4 \\rbrace.\n", | ||
"$$\n", | ||
"The matrix variables are stored in [`A04.txt`](A04.txt), [`EBV04.txt`](EBV04.txt), and [`S04.txt`](S04.txt) respectively, where the first and last are matrices in coordinate format. These are then loaded into Python using `load_problem`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# key problem variables loaded from standard format txt files\n", | ||
"sigma, mubar, omega, n = alphargs.load_problem(\"A04.txt\", \"EBV04.txt\", \"S04.txt\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Since we have constructed the problem to have alternating sex data, we may approach it as we did in [`example.py`](../50/example.py) using range iterates." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"sires = range(0, n, 2)\n", | ||
"dams = range(1, n, 2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Gurobi\n", | ||
"\n", | ||
"AlphaRGS defines functions for solving the standard and robust genetic selection problems using the [gurobipy](https://pypi.org/project/gurobipy/) Python interface to Gurobi. Below these are used to solve the above problem for $\\lambda = 0.5, \\kappa = 1$: " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Set parameter Username\n", | ||
"Academic license - for non-commercial use only - expires 2025-02-26\n", | ||
"i w_std w_rbs\n", | ||
"1 0.00000 0.38200\n", | ||
"2 0.00000 0.38200\n", | ||
"3 0.50000 0.11800\n", | ||
"4 0.50000 0.11800\n", | ||
"\n", | ||
"w_std objective: 1.87500\n", | ||
"w_rbs objective: 0.77684 (z = 0.37924)\n", | ||
"Maximum change: 0.38200\n", | ||
"Average change: 0.38200\n", | ||
"Minimum change: 0.38200\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"lam = 0.5\n", | ||
"kap = 1\n", | ||
"\n", | ||
"# computes the standard and robust genetic selection solutions\n", | ||
"w_std, obj_std = alphargs.gurobi_standard_genetics(sigma, mubar, sires, dams, lam, n)\n", | ||
"w_rbs, z_rbs, obj_rbs = alphargs.gurobi_robust_genetics(sigma, mubar, omega, sires, dams, lam, kap, n)\n", | ||
"\n", | ||
"# print a comparison of the two solutions\n", | ||
"alphargs.print_compare_solutions(w_std, w_rbs, obj_std, obj_rbs, z2=z_rbs, name1=\"w_std\", name2=\"w_rbs\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Finally, we can use `check_uncertainty_constraint` to explore how close our $z\\geq \\sqrt{w^{T}\\Omega w}$ constraint came to equality." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n", | ||
" z: 0.37923871642022844\n", | ||
"w'*Ω*w: 0.3792386953366983\n", | ||
" Diff: 2.1083530143961582e-08\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"if not alphargs.check_uncertainty_constraint(z_rbs, w_rbs, omega, debug=True):\n", | ||
" raise ValueError" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |