Skip to content

Commit 3dcf21a

Browse files
authored
Fix logic of Inversion.__next__ method (#55)
The first model that gets returned should be the initial model, so the zeroth model matches the zeroth iteration (status before any minimization is carried out). This would also make the list of models generated by the iterator the same as the `Inversion.models`.
1 parent 1f251b6 commit 3dcf21a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/inversion_ideas/inversion.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,27 @@ def __init__(
8787
self.model = initial_model.copy()
8888

8989
# Initialize the counter
90-
if not hasattr(self, "_counter"):
91-
self._counter = 0
90+
self._counter = 0
9291

9392
def __next__(self):
9493
"""
9594
Run next iteration in the inversion.
9695
"""
97-
if self.counter == 0 and self.log is not None:
96+
if self.counter == 0:
9897
# Add initial model to log (only on zeroth iteration)
99-
self.log.update(self.counter, self.model)
98+
if self.log is not None:
99+
self.log.update(self.counter, self.model)
100+
100101
# Initialize stopping criteria (if necessary)
101102
if hasattr(self.stopping_criteria, "initialize"):
102103
self.stopping_criteria.initialize()
103104

105+
# Increase counter by one
106+
self._counter += 1
107+
108+
# Return the initial model in the zeroth iteration
109+
return self.model
110+
104111
# Check for stopping criteria before trying to run the iteration
105112
if self.stopping_criteria(self.model):
106113
get_logger().info(
@@ -124,9 +131,8 @@ def __next__(self):
124131
# We update the directives here (and not at the end of this method), so after
125132
# each iteration the objective function is still the same we passed to the
126133
# minimizer.
127-
if self.counter > 0:
128-
for directive in self.directives:
129-
directive(self.model, self.counter)
134+
for directive in self.directives:
135+
directive(self.model, self.counter)
130136

131137
# Minimize objective function
132138
if isinstance(self.minimizer, Minimizer):

0 commit comments

Comments
 (0)