-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor: round 2 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
02eb51f
4c79b26
80d27de
2eba8a6
794209f
96b9187
27a2ad8
89554e9
28ad88a
da2d851
1d5dd75
6679f40
32e7655
b842d00
ff517e0
6b153ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| [deps] | ||
| CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" | ||
| CrystalStructurePrediction = "4140a7b2-00d5-4ecf-8adb-c63b9db3f1fd" | ||
| SCIP = "82193955-e24f-5292-bf16-6f2c5261a85f" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| # TODO: remove this function | ||
| function build_problem( | ||
| grid_size::AbstractVector{Int}, | ||
| population_list::AbstractVector{Int}, | ||
| interaction_matrix::AbstractMatrix{T}; | ||
| optimizer = HiGHS.Optimizer | ||
| optimizer = SCIP.Optimizer | ||
| ) where T<:Real | ||
| csp = Model(optimizer) | ||
| num_grid_points = prod(grid_size) | ||
|
|
@@ -21,12 +22,26 @@ function build_problem( | |
| return value.(x) | ||
| end | ||
|
|
||
| """ | ||
| build_linear_problem(grid_size, population_list, interaction_vector, proximal_pairs; optimizer = SCIP.Optimizer, optimizer_options = Dict()) | ||
|
|
||
| Build a linear problem for crystal structure prediction. Suited for solvers not supporting quadratic constraints. | ||
|
|
||
| # Arguments | ||
| - `grid_size::NTuple{N, Int}`: The size of the grid. | ||
| - `population_list::AbstractVector{Int}`: The number of atoms of each species. | ||
| - `interaction_vector::AbstractVector{T}`: The interaction vector. | ||
| - `proximal_pairs::AbstractVector{Tuple{Int, Int}}`: The proximal pairs. | ||
| - `optimizer`: The optimizer. | ||
| - `optimizer_options`: The options for the optimizer, e.g. `optimizer_options = Dict("NodefileSave" => 1)` for Gurobi. | ||
| """ | ||
| function build_linear_problem( | ||
| grid_size::NTuple{N, Int}, | ||
| population_list::AbstractVector{Int}, | ||
| interaction_vector::AbstractVector{T}, | ||
| proximal_pairs::AbstractVector{Tuple{Int, Int}}; | ||
| optimizer = HiGHS.Optimizer | ||
| optimizer = SCIP.Optimizer, | ||
| optimizer_options = Dict() | ||
| ) where {N, T<:Real} | ||
| csp = Model(optimizer) | ||
| num_grid_points = prod(grid_size) | ||
|
|
@@ -48,21 +63,19 @@ function build_linear_problem( | |
| @constraint(csp, s[i + (j-1)*(j-2)÷2] <= x[i]) | ||
| @constraint(csp, s[i + (j-1)*(j-2)÷2] <= x[j]) | ||
| @constraint(csp, s[i + (j-1)*(j-2)÷2] >= x[i] + x[j] - 1) | ||
| end | ||
| end | ||
| end | ||
| @objective(csp, Min, dot(interaction_vector, s)) | ||
| for (key, value) in optimizer_options | ||
| set_optimizer_attribute(csp, key, value) | ||
| end | ||
| optimize!(csp) | ||
| assert_is_solved_and_feasible(csp) | ||
| return objective_value(csp), value.(x), value.(s) | ||
| end | ||
|
|
||
| """ | ||
| build_quadratic_problem( | ||
| grid_size::NTuple{N, Int}, | ||
| population_list::AbstractVector{Int}, | ||
| interaction_matrix::AbstractMatrix{T}, | ||
| optimizer | ||
| ) where {N, T<:Real} | ||
| build_quadratic_problem(grid_size, population_list, interaction_matrix; optimizer = SCIP.Optimizer, optimizer_options = Dict()) | ||
|
|
||
| Build a quadratic problem for crystal structure prediction. | ||
|
|
||
|
|
@@ -71,12 +84,14 @@ Build a quadratic problem for crystal structure prediction. | |
| - `population_list::AbstractVector{Int}`: The number of atoms of each species. | ||
| - `interaction_matrix::AbstractMatrix{T}`: The interaction matrix. | ||
| - `optimizer`: The optimizer. | ||
| - `optimizer_options`: The options for the optimizer, e.g. `optimizer_options = Dict("NodefileSave" => 1)` for Gurobi. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this NodefileSave for?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is an advanced parameter of Gurobi solver. https://docs.gurobi.com/projects/optimizer/en/current/reference/parameters.html#nodemethod |
||
| """ | ||
| function build_quadratic_problem( | ||
| grid_size::NTuple{N, Int}, | ||
| population_list::AbstractVector{Int}, | ||
| interaction_matrix::AbstractMatrix{T}, | ||
| optimizer | ||
| interaction_matrix::AbstractMatrix{T}; | ||
| optimizer = SCIP.Optimizer, | ||
| optimizer_options = Dict() | ||
| ) where {N, T<:Real} | ||
|
|
||
| csp = Model(optimizer) | ||
|
|
@@ -91,8 +106,9 @@ function build_quadratic_problem( | |
| @constraint(csp, sum(x[num_grid_points*(t-1)+p] for t in range(1, num_species)) <= 1) | ||
| end | ||
| @objective(csp, Min, sum(interaction_matrix[i,j]*x[i]*x[j] for i in range(1, num_species*num_grid_points) for j in range(i+1, num_species*num_grid_points) if (j-i)%num_grid_points != 0)) | ||
| set_optimizer_attribute(csp, "NodefileStart", 1) | ||
|
|
||
| for (key, value) in optimizer_options | ||
| set_optimizer_attribute(csp, key, value) | ||
| end | ||
| optimize!(csp) | ||
| assert_is_solved_and_feasible(csp) | ||
| return objective_value(csp), value.(x), csp | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.