Skip to content

Commit 30aeef2

Browse files
committed
Rename types
1 parent bcb2deb commit 30aeef2

File tree

3 files changed

+50
-45
lines changed

3 files changed

+50
-45
lines changed

src/AlgorithmsInterface.jl

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module AlgorithmsInterface
22

3-
abstract type AbstractProblem end
3+
abstract type Problem end
44

5-
abstract type AbstractAlgorithm end
5+
abstract type Algorithm end
66

7-
abstract type AbstractState end
7+
abstract type State end
88

99
function increment!(
10-
problem::AbstractProblem, algorithm::AbstractAlgorithm, state::AbstractState
10+
problem::Problem, algorithm::Algorithm, state::State
1111
)
1212
state.iteration += 1
1313
return state
@@ -22,23 +22,23 @@ The "state", which stores both the tensor network state (the `iterate`) and the
2222
current region is `alg.regions[iteration]`, while for `alg::Sweeping`, the current sweep is
2323
`alg.sweeps[iteration]`.
2424
=#
25-
mutable struct State{Iterate, Iteration} <: AbstractState
25+
mutable struct DefaultState{Iterate, Iteration} <: State
2626
iterate::Iterate
2727
iteration::Iteration
2828
end
2929

3030
function initialize_state(
31-
problem::AbstractProblem, algorithm::AbstractAlgorithm, x
31+
problem::Problem, algorithm::Algorithm, x
3232
)
33-
return State(x, 0)
33+
return DefaultState(x, 0)
3434
end
3535

3636
using Base.ScopedValues: ScopedValue, with
3737
const CALLBACKS = ScopedValue(Dict{Symbol, Any}())
3838
function callback(
39-
problem::AbstractProblem,
40-
algorithm::AbstractAlgorithm,
41-
state::AbstractState,
39+
problem::Problem,
40+
algorithm::Algorithm,
41+
state::State,
4242
event::Symbol,
4343
)
4444
f = get(CALLBACKS[], event, Returns(nothing))
@@ -50,7 +50,7 @@ function with_callbacks(f, callbacks::Pair{Symbol}...)
5050
end
5151

5252
function solve!(
53-
problem::AbstractProblem, algorithm::AbstractAlgorithm, state::AbstractState
53+
problem::Problem, algorithm::Algorithm, state::State
5454
)
5555
callback(problem, algorithm, state, :Start)
5656
while !is_finished(problem, algorithm, state)
@@ -64,42 +64,44 @@ function solve!(
6464
end
6565

6666
function is_finished(
67-
problem::AbstractProblem, algorithm::AbstractAlgorithm, state::AbstractState
67+
problem::Problem, algorithm::Algorithm, state::State
68+
)
69+
return is_finished(
70+
problem, algorithm, state, state.stopping_criterion, state.stopping_criterion_state
6871
)
69-
return throw(MethodError(is_finished, (problem, algorithm, state)))
7072
end
7173

72-
function step!(problem::AbstractProblem, algorithm::AbstractAlgorithm, state::AbstractState)
74+
function step!(problem::Problem, algorithm::Algorithm, state::State)
7375
return throw(MethodError(step!, (problem, algorithm, state)))
7476
end
7577

76-
abstract type AbstractAlgorithmIterator end
78+
abstract type AlgorithmIterator end
7779

78-
struct AlgorithmIterator{Problem, Algorithm, State} <: AbstractAlgorithmIterator
80+
struct DefaultAlgorithmIterator{Problem, Algorithm, State} <: AlgorithmIterator
7981
problem::Problem
8082
algorithm::Algorithm
8183
state::State
8284
end
8385
function iterator(
84-
problem::AbstractProblem, algorithm::AbstractAlgorithm, state::AbstractState
86+
problem::Problem, algorithm::Algorithm, state::State
8587
)
86-
return return AlgorithmIterator(problem, algorithm, state)
88+
return DefaultAlgorithmIterator(problem, algorithm, state)
8789
end
8890

89-
function is_finished(itr::AbstractAlgorithmIterator)
91+
function is_finished(itr::AlgorithmIterator)
9092
return is_finished(itr.problem, itr.algorithm, itr.state)
9193
end
92-
function callback(itr::AbstractAlgorithmIterator, event::Symbol)
94+
function callback(itr::AlgorithmIterator, event::Symbol)
9395
return callback(itr.problem, itr.algorithm, itr.state, event)
9496
end
95-
function increment!(itr::AbstractAlgorithmIterator)
97+
function increment!(itr::AlgorithmIterator)
9698
return increment!(itr.problem, itr.algorithm, itr.state)
9799
end
98-
function step!(itr::AbstractAlgorithmIterator)
100+
function step!(itr::AlgorithmIterator)
99101
return step!(itr.problem, itr.algorithm, itr.state)
100102
end
101103

102-
function Base.iterate(itr::AbstractAlgorithmIterator, init = nothing)
104+
function Base.iterate(itr::AlgorithmIterator, init = nothing)
103105
is_finished(itr) && return nothing
104106
callback(itr, :PreStep)
105107
increment!(itr)
@@ -108,4 +110,7 @@ function Base.iterate(itr::AbstractAlgorithmIterator, init = nothing)
108110
return itr.state, nothing
109111
end
110112

113+
abstract type StoppingCriterion end
114+
abstract type StoppingCriterionState end
115+
111116
end

src/eigenproblem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import .AlgorithmsInterface as AI
66
Represents the problem we are trying to solve and minimal algorithm-independent
77
information, so for an eigenproblem it is the operator we want the eigenvector of.
88
=#
9-
struct EigenProblem{Operator} <: AI.AbstractProblem
9+
struct EigenProblem{Operator} <: AI.Problem
1010
operator::Operator
1111
end
1212

1313
function update!(
14-
problem::EigenProblem, algorithm::Sweep, state::AI.AbstractState
14+
problem::EigenProblem, algorithm::Sweep, state::AI.State
1515
)
1616
operator = problem.operator
1717
x = state.iterate

src/sweeping.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ current region. For simplicity, it also accepts a `NamedTuple` of keyword argume
1111
which is converted into a function that always returns the same keyword arguments
1212
for an region.
1313
=#
14-
@kwdef struct Sweep{Regions <: AbstractVector, RegionKwargs <: Function} <: AI.AbstractAlgorithm
14+
@kwdef struct Sweep{Regions <: Vector, RegionKwargs <: Function} <: AI.Algorithm
1515
regions::Regions
1616
region_kwargs::RegionKwargs
1717
iteration::Int = 0
1818
end
19-
function Sweep(regions::AbstractVector, region_kwargs::NamedTuple, iteration::Int = 0)
19+
function Sweep(regions::Vector, region_kwargs::NamedTuple, iteration::Int = 0)
2020
function region_kwargs_fn(
21-
problem::AI.AbstractProblem,
22-
algorithm::AI.AbstractAlgorithm,
23-
state::AI.AbstractState,
21+
problem::AI.Problem,
22+
algorithm::AI.Algorithm,
23+
state::AI.State,
2424
)
2525
return region_kwargs
2626
end
@@ -30,7 +30,7 @@ end
3030
maxiter(algorithm::Sweep) = length(algorithm.regions)
3131

3232
function AI.step!(
33-
problem::AI.AbstractProblem, algorithm::Sweep, state::AI.AbstractState
33+
problem::AI.Problem, algorithm::Sweep, state::AI.State
3434
)
3535
extract!(problem, algorithm, state)
3636
update!(problem, algorithm, state)
@@ -39,82 +39,82 @@ function AI.step!(
3939
end
4040

4141
function extract!(
42-
problem::AI.AbstractProblem, algorithm::Sweep, state::AI.AbstractState
42+
problem::AI.Problem, algorithm::Sweep, state::AI.State
4343
)
4444
# Extraction step goes here.
4545
return state
4646
end
4747
function update!(
48-
problem::AI.AbstractProblem, algorithm::Sweep, state::AI.AbstractState
48+
problem::AI.Problem, algorithm::Sweep, state::AI.State
4949
)
5050
# Update step goes here.
5151
return state
5252
end
5353
function insert!(
54-
problem::AI.AbstractProblem, algorithm::Sweep, state::AI.AbstractState
54+
problem::AI.Problem, algorithm::Sweep, state::AI.State
5555
)
5656
# Insert step goes here.
5757
return state
5858
end
5959

6060
# TODO: Use a proper stopping criterion.
6161
function AI.is_finished(
62-
problem::AI.AbstractProblem, algorithm::Sweep, state::AI.AbstractState
62+
problem::AI.Problem, algorithm::Sweep, state::AI.State
6363
)
6464
state.iteration == 0 && return false
6565
return state.iteration >= length(algorithm.regions)
6666
end
6767

6868
#=
69-
Sweeping(sweeps::AbstractVector{<:Sweep})
69+
Sweeping(sweeps::Vector{<:Sweep})
7070
7171
The sweeping algorithm, which just stores a list of sweeps defined above.
7272
=#
73-
struct Sweeping{Sweeps <: AbstractVector{<:Sweep}} <: AI.AbstractAlgorithm
73+
struct Sweeping{Sweeps <: Vector{<:Sweep}} <: AI.Algorithm
7474
sweeps::Sweeps
7575
end
7676

7777
maxiter(algorithm::Sweeping) = length(algorithm.sweeps)
7878

7979
function AI.step!(
80-
problem::AI.AbstractProblem, algorithm::Sweeping, state::AI.AbstractState
80+
problem::AI.Problem, algorithm::Sweeping, state::AI.State
8181
)
8282
# Perform the current sweep.
8383
sweep = algorithm.sweeps[state.iteration]
8484
x = state.iterate
85-
region_state = AI.State(x, 0)
85+
region_state = AI.initialize_state(problem, sweep, x)
8686
AI.solve!(problem, sweep, region_state)
8787
state.iterate = region_state.iterate
8888
return state
8989
end
9090

9191
# TODO: Use a proper stopping criterion.
9292
function AI.is_finished(
93-
problem::AI.AbstractProblem, algorithm::Sweeping, state::AI.AbstractState
93+
problem::AI.Problem, algorithm::Sweeping, state::AI.State
9494
)
9595
state.iteration == 0 && return false
9696
return state.iteration >= length(algorithm.sweeps)
9797
end
9898

9999
# Sweeping by region.
100-
struct ByRegion{Algorithm <: Sweeping} <: AI.AbstractAlgorithm
100+
struct ByRegion{Algorithm <: Sweeping} <: AI.Algorithm
101101
parent::Algorithm
102102
end
103103
function AI.initialize_state(
104-
problem::AI.AbstractProblem, algorithm::ByRegion, x
104+
problem::AI.Problem, algorithm::ByRegion, x
105105
)
106106
return AI.State(x, (; sweep = 1, region = 0))
107107
end
108108
function AI.is_finished(
109-
problem::AI.AbstractProblem, algorithm::ByRegion, state::AI.AbstractState
109+
problem::AI.Problem, algorithm::ByRegion, state::AI.State
110110
)
111111
sweep_iteration = state.iteration.sweep
112112
region_iteration = state.iteration.region
113113
return sweep_iteration maxiter(algorithm.parent) &&
114114
region_iteration maxiter(algorithm.parent.sweeps[sweep_iteration])
115115
end
116116
function AI.increment!(
117-
problem::AI.AbstractProblem, algorithm::ByRegion, state::AI.AbstractState
117+
problem::AI.Problem, algorithm::ByRegion, state::AI.State
118118
)
119119
sweep_iteration = state.iteration.sweep
120120
region_iteration = state.iteration.region
@@ -127,7 +127,7 @@ function AI.increment!(
127127
state.iteration = (; sweep = sweep_iteration, region = region_iteration)
128128
return state
129129
end
130-
function AI.step!(problem::AI.AbstractProblem, algorithm::ByRegion, state::AI.AbstractState)
130+
function AI.step!(problem::AI.Problem, algorithm::ByRegion, state::AI.State)
131131
sweep = algorithm.parent.sweeps[state.iteration.sweep]
132132
sweep_state = AI.State(state.iterate, state.iteration.region)
133133
AI.step!(problem, sweep, sweep_state)

0 commit comments

Comments
 (0)