suppy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- suppy/__init__.py +1 -0
- suppy/algorithms/__init__.py +0 -0
- suppy/algorithms/level_set.py +106 -0
- suppy/algorithms/projected_subgradient.py +33 -0
- suppy/feasibility/__init__.py +25 -0
- suppy/feasibility/_bands/_ams_algorithms.py +494 -0
- suppy/feasibility/_bands/_arm_algorithms.py +371 -0
- suppy/feasibility/_bands/_art3_algorithms.py +297 -0
- suppy/feasibility/_halfspaces/_ams_algorithms.py +494 -0
- suppy/feasibility/_linear_algorithms.py +346 -0
- suppy/feasibility/_split_algorithms.py +400 -0
- suppy/perturbations/__init__.py +3 -0
- suppy/perturbations/_base.py +222 -0
- suppy/projections/__init__.py +34 -0
- suppy/projections/_basic_projections.py +904 -0
- suppy/projections/_projection_methods.py +530 -0
- suppy/projections/_projections.py +183 -0
- suppy/projections/_subgradient_projections.py +222 -0
- suppy/superiorization/__init__.py +4 -0
- suppy/superiorization/_deprecated/_sup.py +461 -0
- suppy/superiorization/_deprecated/sup.py +529 -0
- suppy/superiorization/_deprecated/sup_new.py +643 -0
- suppy/superiorization/_split_sup.py +283 -0
- suppy/superiorization/_standard_sup.py +293 -0
- suppy/superiorization/_sup.py +43 -0
- suppy/utils/__init__.py +9 -0
- suppy/utils/_array_helper.py +225 -0
- suppy/utils/_bounds.py +210 -0
- suppy/utils/_calc_DVH.py +25 -0
- suppy/utils/_decorators.py +42 -0
- suppy/utils/_func_wrapper.py +33 -0
- suppy/utils/plot.py +159 -0
- suppy-0.1.0.dist-info/METADATA +142 -0
- suppy-0.1.0.dist-info/RECORD +36 -0
- suppy-0.1.0.dist-info/WHEEL +4 -0
- suppy-0.1.0.dist-info/licenses/LICENSE +11 -0
suppy/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from suppy import feasibility
|
|
File without changes
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
from typing import List, Callable
|
|
2
|
+
import numpy.typing as npt
|
|
3
|
+
from suppy.projections import SequentialProjection, SubgradientProjection
|
|
4
|
+
from suppy.projections._projections import Projection
|
|
5
|
+
from suppy.utils import ensure_float_array
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class LevelSet:
|
|
9
|
+
"""Level set scheme that tries to minimize a function in C."""
|
|
10
|
+
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
projections: List[Projection] | SequentialProjection,
|
|
14
|
+
function: Callable,
|
|
15
|
+
gradient: Callable,
|
|
16
|
+
func_args: List | None = None,
|
|
17
|
+
gradient_args: List | None = None,
|
|
18
|
+
save_hist=False,
|
|
19
|
+
):
|
|
20
|
+
func_args = [] if func_args is None else func_args
|
|
21
|
+
gradient_args = [] if gradient_args is None else gradient_args
|
|
22
|
+
|
|
23
|
+
self.func_projection = SubgradientProjection(
|
|
24
|
+
function,
|
|
25
|
+
gradient,
|
|
26
|
+
func_args=func_args,
|
|
27
|
+
grad_args=gradient_args,
|
|
28
|
+
relaxation=1.2,
|
|
29
|
+
)
|
|
30
|
+
# TODO: shold be choosable or simultaneous?
|
|
31
|
+
self.C_projection = SequentialProjection(projections)
|
|
32
|
+
|
|
33
|
+
self.save_hist = save_hist
|
|
34
|
+
self.all_x = []
|
|
35
|
+
self.all_y = []
|
|
36
|
+
|
|
37
|
+
@ensure_float_array
|
|
38
|
+
def solve(
|
|
39
|
+
self,
|
|
40
|
+
x: npt.ArrayLike,
|
|
41
|
+
f_init: None | float = None,
|
|
42
|
+
max_iter=1000,
|
|
43
|
+
epsilon=0.1,
|
|
44
|
+
constr_tol=1e-6,
|
|
45
|
+
):
|
|
46
|
+
"""Solve the problem."""
|
|
47
|
+
if self.save_hist is True:
|
|
48
|
+
self.all_x = []
|
|
49
|
+
self.all_x.append(x.copy())
|
|
50
|
+
self.all_y = []
|
|
51
|
+
self.all_y.append(self.func_projection.func_call(x))
|
|
52
|
+
|
|
53
|
+
# if an init value is given skip first projection
|
|
54
|
+
if f_init is not None:
|
|
55
|
+
self.func_projection.level = f_init
|
|
56
|
+
else: # find a feasible point
|
|
57
|
+
x = self.C_projection.solve(x)
|
|
58
|
+
f_x = self.func_projection.func_call(x)
|
|
59
|
+
self.func_projection.level = f_x - max(
|
|
60
|
+
epsilon, abs(f_x) * epsilon
|
|
61
|
+
) # set the level to the value of the function at the feasible point
|
|
62
|
+
|
|
63
|
+
if self.save_hist is True:
|
|
64
|
+
self.all_x.append(x.copy())
|
|
65
|
+
self.all_y.append(f_x)
|
|
66
|
+
|
|
67
|
+
converged = False
|
|
68
|
+
|
|
69
|
+
while not converged:
|
|
70
|
+
i = 0
|
|
71
|
+
feasible = False
|
|
72
|
+
while i < max_iter and not feasible:
|
|
73
|
+
# x_n = x.copy()
|
|
74
|
+
|
|
75
|
+
# #project onto constraint set
|
|
76
|
+
x = self.C_projection.project(x)
|
|
77
|
+
# project onto objective set
|
|
78
|
+
x = self.func_projection.project(x)
|
|
79
|
+
|
|
80
|
+
# calculate proximity
|
|
81
|
+
# get current function difference to level set
|
|
82
|
+
level_diff = self.func_projection.level_diff(x)
|
|
83
|
+
|
|
84
|
+
# get current constraint proximity
|
|
85
|
+
c_proximity = self.C_projection.proximity(x)
|
|
86
|
+
|
|
87
|
+
# level set reached and constraints are satisfied
|
|
88
|
+
if level_diff < 0 and c_proximity < constr_tol:
|
|
89
|
+
feasible = True
|
|
90
|
+
|
|
91
|
+
i += 1
|
|
92
|
+
|
|
93
|
+
if i == max_iter:
|
|
94
|
+
converged = True
|
|
95
|
+
|
|
96
|
+
else:
|
|
97
|
+
# update level set
|
|
98
|
+
f_x = self.func_projection.func_call(x)
|
|
99
|
+
if self.save_hist is True:
|
|
100
|
+
self.all_x.append(x.copy())
|
|
101
|
+
self.all_y.append(f_x)
|
|
102
|
+
|
|
103
|
+
# update level set
|
|
104
|
+
self.func_projection.level = f_x - max(epsilon, abs(f_x) * epsilon)
|
|
105
|
+
|
|
106
|
+
return x
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# from suppy.utils.func_wrapper import func_wrapper
|
|
2
|
+
# from suppy.utils.decorators import ensure_float_array
|
|
3
|
+
# from typing import Callable
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# class Projected_subgradient:
|
|
7
|
+
# """
|
|
8
|
+
# """
|
|
9
|
+
# def __init__(self,
|
|
10
|
+
# func:Callable,
|
|
11
|
+
# grad:Callable, #what if gradient free?
|
|
12
|
+
# projection:Callable,
|
|
13
|
+
# func_args = (),
|
|
14
|
+
# grad_args = ()):
|
|
15
|
+
|
|
16
|
+
# self.wrapped_func = func_wrapper(func,func_args)
|
|
17
|
+
# self.wrapped_grad = func_wrapper(grad,grad_args)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# def step(self,x):
|
|
21
|
+
# """
|
|
22
|
+
# """
|
|
23
|
+
# x_new = x - self.wrapped_grad(x) #TODO: Gradient step size
|
|
24
|
+
# x_new = self.projection.solve(x_new) #need the full projection
|
|
25
|
+
# return x_new
|
|
26
|
+
|
|
27
|
+
# def solve(self,x):
|
|
28
|
+
# """
|
|
29
|
+
# """
|
|
30
|
+
# x_new = x
|
|
31
|
+
# for i in range(1000):
|
|
32
|
+
# x_new = self.step(x_new)
|
|
33
|
+
# return x_new
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from suppy.utils import Bounds
|
|
2
|
+
from ._bands._ams_algorithms import (
|
|
3
|
+
SequentialAMS,
|
|
4
|
+
SequentialWeightedAMS,
|
|
5
|
+
SimultaneousAMS,
|
|
6
|
+
StringAveragedAMS,
|
|
7
|
+
BlockIterativeAMS,
|
|
8
|
+
)
|
|
9
|
+
from ._bands._arm_algorithms import SequentialARM, SimultaneousARM, StringAveragedARM
|
|
10
|
+
from ._bands._art3_algorithms import SequentialART3plus
|
|
11
|
+
from ._split_algorithms import CQAlgorithm, ProductSpaceAlgorithm
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"SequentialAMS",
|
|
15
|
+
"SequentialWeightedAMS",
|
|
16
|
+
"SimultaneousAMS",
|
|
17
|
+
"StringAveragedAMS",
|
|
18
|
+
"BlockIterativeAMS",
|
|
19
|
+
"SequentialARM",
|
|
20
|
+
"SimultaneousARM",
|
|
21
|
+
"StringAveragedARM",
|
|
22
|
+
"SequentialART3plus",
|
|
23
|
+
"CQAlgorithm",
|
|
24
|
+
"ProductSpaceAlgorithm",
|
|
25
|
+
]
|
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
from abc import ABC
|
|
2
|
+
from typing import List
|
|
3
|
+
import numpy as np
|
|
4
|
+
import numpy.typing as npt
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
import cupy as cp
|
|
8
|
+
|
|
9
|
+
no_gpu = False
|
|
10
|
+
|
|
11
|
+
except ImportError:
|
|
12
|
+
no_gpu = True
|
|
13
|
+
cp = None
|
|
14
|
+
|
|
15
|
+
from suppy.feasibility._linear_algorithms import HyperslabFeasibility
|
|
16
|
+
from suppy.utils import LinearMapping
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class HyperslabAMSAlgorithm(HyperslabFeasibility, ABC):
|
|
20
|
+
"""
|
|
21
|
+
The HyperslabAMSAlgorithm class is used to find a feasible solution to a
|
|
22
|
+
set of
|
|
23
|
+
linear inequalities.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
A : npt.ArrayLike
|
|
28
|
+
The matrix representing the coefficients of the linear inequalities.
|
|
29
|
+
lb : npt.ArrayLike
|
|
30
|
+
The lower bounds for the inequalities.
|
|
31
|
+
ub : npt.ArrayLike
|
|
32
|
+
The upper bounds for the inequalities.
|
|
33
|
+
algorithmic_relaxation : npt.ArrayLike or float, optional
|
|
34
|
+
The relaxation parameter for the algorithm, by default 1.
|
|
35
|
+
relaxation : float, optional
|
|
36
|
+
The relaxation parameter for the feasibility problem, by default 1.
|
|
37
|
+
proximity_flag : bool, optional
|
|
38
|
+
A flag indicating whether to use proximity in the algorithm, by default True.
|
|
39
|
+
|
|
40
|
+
Attributes
|
|
41
|
+
----------
|
|
42
|
+
A_norm : LinearMapping
|
|
43
|
+
Internal representation of the matrix normalized with respect to the row norms.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
A: npt.ArrayLike,
|
|
49
|
+
lb: npt.ArrayLike,
|
|
50
|
+
ub: npt.ArrayLike,
|
|
51
|
+
algorithmic_relaxation: npt.ArrayLike | float = 1,
|
|
52
|
+
relaxation: float = 1,
|
|
53
|
+
proximity_flag: bool = True,
|
|
54
|
+
):
|
|
55
|
+
super().__init__(A, lb, ub, algorithmic_relaxation, relaxation, proximity_flag)
|
|
56
|
+
self.A_norm = LinearMapping(self.A.normalize_rows(2, 2))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class SequentialAMS(HyperslabAMSAlgorithm):
|
|
60
|
+
"""
|
|
61
|
+
SequentialAMS class for sequentially applying the AMS algorithm.
|
|
62
|
+
|
|
63
|
+
Parameters
|
|
64
|
+
----------
|
|
65
|
+
A : npt.ArrayLike
|
|
66
|
+
The matrix A used in the AMS algorithm.
|
|
67
|
+
lb : npt.ArrayLike
|
|
68
|
+
The lower bounds for the constraints.
|
|
69
|
+
ub : npt.ArrayLike
|
|
70
|
+
The upper bounds for the constraints.
|
|
71
|
+
algorithmic_relaxation : npt.ArrayLike or float, optional
|
|
72
|
+
The relaxation parameter for the algorithm, by default 1.
|
|
73
|
+
relaxation : float, optional
|
|
74
|
+
The relaxation parameter, by default 1.
|
|
75
|
+
cs : None or List[int], optional
|
|
76
|
+
The list of indices for the constraints, by default None.
|
|
77
|
+
proximity_flag : bool, optional
|
|
78
|
+
Flag to indicate if proximity should be considered, by default True.
|
|
79
|
+
|
|
80
|
+
Attributes
|
|
81
|
+
----------
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
def __init__(
|
|
85
|
+
self,
|
|
86
|
+
A: npt.ArrayLike,
|
|
87
|
+
lb: npt.ArrayLike,
|
|
88
|
+
ub: npt.ArrayLike,
|
|
89
|
+
algorithmic_relaxation: npt.ArrayLike | float = 1,
|
|
90
|
+
relaxation: float = 1,
|
|
91
|
+
cs: None | List[int] = None,
|
|
92
|
+
proximity_flag: bool = True,
|
|
93
|
+
):
|
|
94
|
+
|
|
95
|
+
super().__init__(A, lb, ub, algorithmic_relaxation, relaxation, proximity_flag)
|
|
96
|
+
xp = cp if self._use_gpu else np
|
|
97
|
+
if cs is None:
|
|
98
|
+
self.cs = xp.arange(self.A.shape[0])
|
|
99
|
+
else:
|
|
100
|
+
self.cs = cs
|
|
101
|
+
|
|
102
|
+
def _project(self, x: npt.ArrayLike) -> npt.ArrayLike:
|
|
103
|
+
"""
|
|
104
|
+
Projects the input array `x` onto the feasible region defined by the
|
|
105
|
+
constraints.
|
|
106
|
+
|
|
107
|
+
Parameters
|
|
108
|
+
----------
|
|
109
|
+
x : npt.ArrayLike
|
|
110
|
+
The input array to be projected.
|
|
111
|
+
|
|
112
|
+
Returns
|
|
113
|
+
-------
|
|
114
|
+
npt.ArrayLike
|
|
115
|
+
The projected array.
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
for i in self.cs:
|
|
119
|
+
p_i = self.single_map(x, i)
|
|
120
|
+
(res_li, res_ui) = self.Bounds.single_residual(p_i, i) # returns floats
|
|
121
|
+
# check if constraints are violated
|
|
122
|
+
|
|
123
|
+
# weights should be 1s!
|
|
124
|
+
if res_ui < 0:
|
|
125
|
+
self.A_norm.update_step(x, self.algorithmic_relaxation * res_ui, i)
|
|
126
|
+
elif res_li < 0:
|
|
127
|
+
self.A_norm.update_step(x, -1 * self.algorithmic_relaxation * res_li, i)
|
|
128
|
+
return x
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class SequentialWeightedAMS(SequentialAMS):
|
|
132
|
+
"""
|
|
133
|
+
Parameters
|
|
134
|
+
----------
|
|
135
|
+
A : npt.ArrayLike
|
|
136
|
+
The constraint matrix.
|
|
137
|
+
lb : npt.ArrayLike
|
|
138
|
+
The lower bounds of the constraints.
|
|
139
|
+
ub : npt.ArrayLike
|
|
140
|
+
The upper bounds of the constraints.
|
|
141
|
+
weights : None, list of float, or npt.ArrayLike, optional
|
|
142
|
+
The weights assigned to each constraint. If None, default weights are
|
|
143
|
+
used.
|
|
144
|
+
algorithmic_relaxation : npt.ArrayLike or float, optional
|
|
145
|
+
The relaxation parameter for the algorithm. Default is 1.
|
|
146
|
+
relaxation : float, optional
|
|
147
|
+
The relaxation parameter for the algorithm. Default is 1.
|
|
148
|
+
weight_decay : float, optional
|
|
149
|
+
Parameter that determines the rate at which the weights are reduced
|
|
150
|
+
after each phase (weights * weight_decay). Default is 1.
|
|
151
|
+
cs : None or list of int, optional
|
|
152
|
+
The indices of the constraints to be considered. Default is None.
|
|
153
|
+
proximity_flag : bool, optional
|
|
154
|
+
Flag to indicate if proximity should be considered. Default is True.
|
|
155
|
+
|
|
156
|
+
Attributes
|
|
157
|
+
----------
|
|
158
|
+
weights : npt.ArrayLike
|
|
159
|
+
The weights assigned to each constraint.
|
|
160
|
+
weight_decay : float
|
|
161
|
+
Decay rate for the weights.
|
|
162
|
+
temp_weight_decay : float
|
|
163
|
+
Initial value for weight decay.
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
def __init__(
|
|
167
|
+
self,
|
|
168
|
+
A: npt.ArrayLike,
|
|
169
|
+
lb: npt.ArrayLike,
|
|
170
|
+
ub: npt.ArrayLike,
|
|
171
|
+
weights: None | List[float] | npt.ArrayLike = None,
|
|
172
|
+
algorithmic_relaxation: npt.ArrayLike | float = 1,
|
|
173
|
+
relaxation: float = 1,
|
|
174
|
+
weight_decay: float = 1,
|
|
175
|
+
cs: None | List[int] = None,
|
|
176
|
+
proximity_flag: bool = True,
|
|
177
|
+
):
|
|
178
|
+
|
|
179
|
+
super().__init__(A, lb, ub, algorithmic_relaxation, relaxation, cs, proximity_flag)
|
|
180
|
+
xp = cp if self._use_gpu else np
|
|
181
|
+
self.weight_decay = weight_decay # decay rate
|
|
182
|
+
self.temp_weight_decay = 1 # initial value for weight decay
|
|
183
|
+
|
|
184
|
+
if weights is None:
|
|
185
|
+
self.weights = xp.ones(self.A.shape[0])
|
|
186
|
+
elif xp.abs((weights.sum() - 1)) > 1e-10:
|
|
187
|
+
print("Weights do not add up to 1! Renormalizing to 1...")
|
|
188
|
+
self.weights = weights
|
|
189
|
+
|
|
190
|
+
def _project(self, x: npt.ArrayLike) -> npt.ArrayLike:
|
|
191
|
+
"""
|
|
192
|
+
Projects the input array `x` onto a feasible region defined by the
|
|
193
|
+
constraints.
|
|
194
|
+
|
|
195
|
+
Parameters
|
|
196
|
+
----------
|
|
197
|
+
x : npt.ArrayLike
|
|
198
|
+
The input array to be projected.
|
|
199
|
+
|
|
200
|
+
Returns
|
|
201
|
+
-------
|
|
202
|
+
npt.ArrayLike
|
|
203
|
+
The projected array.
|
|
204
|
+
|
|
205
|
+
Notes
|
|
206
|
+
-----
|
|
207
|
+
This method iteratively adjusts the input array `x` based on the constraints
|
|
208
|
+
defined in `self.cs`. For each constraint, it computes the projection and
|
|
209
|
+
checks if the constraints are violated. If a constraint is violated, it updates
|
|
210
|
+
the array `x` using a weighted relaxation factor. The weight decay is applied
|
|
211
|
+
to the temporary weight decay after each iteration.
|
|
212
|
+
"""
|
|
213
|
+
|
|
214
|
+
weighted_relaxation = self.algorithmic_relaxation * self.temp_weight_decay
|
|
215
|
+
|
|
216
|
+
for i in self.cs:
|
|
217
|
+
|
|
218
|
+
p_i = self.single_map(x, i)
|
|
219
|
+
|
|
220
|
+
(res_li, res_ui) = self.Bounds.single_residual(p_i, i) # returns floats
|
|
221
|
+
# check if constraints are violated
|
|
222
|
+
|
|
223
|
+
if res_ui < 0:
|
|
224
|
+
self.A_norm.update_step(x, weighted_relaxation * self.weights[i] * res_ui, i)
|
|
225
|
+
elif res_li < 0:
|
|
226
|
+
self.A_norm.update_step(x, -1 * weighted_relaxation * self.weights[i] * res_li, i)
|
|
227
|
+
|
|
228
|
+
self.temp_weight_decay *= self.weight_decay
|
|
229
|
+
return x
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
class SimultaneousAMS(HyperslabAMSAlgorithm):
|
|
233
|
+
"""
|
|
234
|
+
SimultaneousAMS is an implementation of the AMS (Alternating
|
|
235
|
+
Minimization Scheme) algorithm
|
|
236
|
+
that performs simultaneous projections and proximity calculations.
|
|
237
|
+
|
|
238
|
+
Parameters
|
|
239
|
+
----------
|
|
240
|
+
A : npt.ArrayLike
|
|
241
|
+
The matrix representing the constraints.
|
|
242
|
+
lb : npt.ArrayLike
|
|
243
|
+
The lower bounds for the constraints.
|
|
244
|
+
ub : npt.ArrayLike
|
|
245
|
+
The upper bounds for the constraints.
|
|
246
|
+
algorithmic_relaxation : npt.ArrayLike or float, optional
|
|
247
|
+
The relaxation parameter for the algorithm, by default 1.
|
|
248
|
+
relaxation : float, optional
|
|
249
|
+
The relaxation parameter for the projections, by default 1.
|
|
250
|
+
weights : None or List[float], optional
|
|
251
|
+
The weights for the constraints, by default None.
|
|
252
|
+
proximity_flag : bool, optional
|
|
253
|
+
Flag to indicate if proximity calculations should be performed, by default True.
|
|
254
|
+
"""
|
|
255
|
+
|
|
256
|
+
def __init__(
|
|
257
|
+
self,
|
|
258
|
+
A: npt.ArrayLike,
|
|
259
|
+
lb: npt.ArrayLike,
|
|
260
|
+
ub: npt.ArrayLike,
|
|
261
|
+
algorithmic_relaxation: npt.ArrayLike | float = 1,
|
|
262
|
+
relaxation: float = 1,
|
|
263
|
+
weights: None | List[float] = None,
|
|
264
|
+
proximity_flag: bool = True,
|
|
265
|
+
):
|
|
266
|
+
|
|
267
|
+
super().__init__(A, lb, ub, algorithmic_relaxation, relaxation, proximity_flag)
|
|
268
|
+
|
|
269
|
+
xp = cp if self._use_gpu else np
|
|
270
|
+
|
|
271
|
+
if weights is None:
|
|
272
|
+
self.weights = xp.ones(self.A.shape[0]) / self.A.shape[0]
|
|
273
|
+
elif xp.abs((weights.sum() - 1)) > 1e-10:
|
|
274
|
+
print("Weights do not add up to 1! Renormalizing to 1...")
|
|
275
|
+
self.weights = weights / weights.sum()
|
|
276
|
+
else:
|
|
277
|
+
self.weights = weights
|
|
278
|
+
|
|
279
|
+
def _project(self, x):
|
|
280
|
+
# simultaneous projection
|
|
281
|
+
p = self.map(x)
|
|
282
|
+
(res_l, res_u) = self.Bounds.residual(p)
|
|
283
|
+
d_idx = res_u < 0
|
|
284
|
+
c_idx = res_l < 0
|
|
285
|
+
x += self.algorithmic_relaxation * (
|
|
286
|
+
self.weights[d_idx] * res_u[d_idx] @ self.A_norm[d_idx, :]
|
|
287
|
+
- self.weights[c_idx] * res_l[c_idx] @ self.A_norm[c_idx, :]
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
return x
|
|
291
|
+
|
|
292
|
+
def _proximity(self, x: npt.ArrayLike) -> float:
|
|
293
|
+
p = self.map(x)
|
|
294
|
+
# residuals are positive if constraints are met
|
|
295
|
+
(res_u, res_l) = self.Bounds.residual(p)
|
|
296
|
+
d_idx = res_u < 0
|
|
297
|
+
c_idx = res_l < 0
|
|
298
|
+
return (self.weights[d_idx] * res_u[d_idx] ** 2).sum() + (
|
|
299
|
+
self.weights[c_idx] * res_l[c_idx] ** 2
|
|
300
|
+
).sum()
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
class ExtrapolatedLandweber(SimultaneousAMS):
|
|
304
|
+
def __init__(
|
|
305
|
+
self, A, lb, ub, algorithmic_relaxation=1, relaxation=1, weights=None, proximity_flag=True
|
|
306
|
+
):
|
|
307
|
+
super().__init__(A, lb, ub, algorithmic_relaxation, relaxation, weights, proximity_flag)
|
|
308
|
+
self.a_i = self.A.row_norm(2, 2)
|
|
309
|
+
self.weight_norm = self.weights / self.a_i
|
|
310
|
+
self.sigmas = []
|
|
311
|
+
|
|
312
|
+
def _project(self, x):
|
|
313
|
+
p = self.map(x)
|
|
314
|
+
(res_l, res_u) = self.Bounds.residual(p)
|
|
315
|
+
d_idx = res_u < 0
|
|
316
|
+
c_idx = res_l < 0
|
|
317
|
+
if not (np.any(d_idx) or np.any(c_idx)):
|
|
318
|
+
self.sigmas.append(0)
|
|
319
|
+
return x
|
|
320
|
+
t_u = self.weight_norm[d_idx] * res_u[d_idx] # D*(Ax-b)+
|
|
321
|
+
t_l = self.weight_norm[c_idx] * res_l[c_idx]
|
|
322
|
+
t_u_2 = t_u @ self.A[d_idx, :]
|
|
323
|
+
t_l_2 = t_l @ self.A[c_idx, :]
|
|
324
|
+
|
|
325
|
+
sig = ((res_l[c_idx] @ (t_l)) + (res_u[d_idx] @ (t_u))) / (
|
|
326
|
+
(t_u_2 - t_l_2) @ (t_u_2 - t_l_2)
|
|
327
|
+
)
|
|
328
|
+
self.sigmas.append(sig)
|
|
329
|
+
x += sig * (t_u_2 - t_l_2)
|
|
330
|
+
|
|
331
|
+
return x
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
class BlockIterativeAMS(HyperslabAMSAlgorithm):
|
|
335
|
+
"""
|
|
336
|
+
Block Iterative AMS Algorithm.
|
|
337
|
+
This class implements a block iterative version of the AMS (Alternating
|
|
338
|
+
Minimization Scheme) algorithm.
|
|
339
|
+
It is designed to handle constraints and weights in a block-wise manner.
|
|
340
|
+
|
|
341
|
+
Parameters
|
|
342
|
+
----------
|
|
343
|
+
A : npt.ArrayLike
|
|
344
|
+
The matrix representing the linear constraints.
|
|
345
|
+
lb : npt.ArrayLike
|
|
346
|
+
The lower bounds for the constraints.
|
|
347
|
+
ub : npt.ArrayLike
|
|
348
|
+
The upper bounds for the constraints.
|
|
349
|
+
weights : List[List[float]] or List[npt.ArrayLike]
|
|
350
|
+
A list of lists or arrays representing the weights for each block. Each list/array should sum to 1.
|
|
351
|
+
algorithmic_relaxation : npt.ArrayLike or float, optional
|
|
352
|
+
The relaxation parameter for the algorithm, by default 1.
|
|
353
|
+
relaxation : float, optional
|
|
354
|
+
The relaxation parameter for the constraints, by default 1.
|
|
355
|
+
proximity_flag : bool, optional
|
|
356
|
+
A flag indicating whether to use proximity measures, by default True.
|
|
357
|
+
|
|
358
|
+
Raises
|
|
359
|
+
------
|
|
360
|
+
ValueError
|
|
361
|
+
If any of the weight lists do not sum to 1.
|
|
362
|
+
"""
|
|
363
|
+
|
|
364
|
+
def __init__(
|
|
365
|
+
self,
|
|
366
|
+
A: npt.ArrayLike,
|
|
367
|
+
lb: npt.ArrayLike,
|
|
368
|
+
ub: npt.ArrayLike,
|
|
369
|
+
weights: List[List[float]] | List[npt.ArrayLike],
|
|
370
|
+
algorithmic_relaxation: npt.ArrayLike | float = 1,
|
|
371
|
+
relaxation: float = 1,
|
|
372
|
+
proximity_flag: bool = True,
|
|
373
|
+
):
|
|
374
|
+
|
|
375
|
+
super().__init__(A, lb, ub, algorithmic_relaxation, relaxation, proximity_flag)
|
|
376
|
+
|
|
377
|
+
xp = cp if self._use_gpu else np
|
|
378
|
+
|
|
379
|
+
# check that weights is a list of lists that add up to 1 each
|
|
380
|
+
for el in weights:
|
|
381
|
+
if xp.abs((xp.sum(el) - 1)) > 1e-10:
|
|
382
|
+
raise ValueError("Weights do not add up to 1!")
|
|
383
|
+
|
|
384
|
+
self.weights = []
|
|
385
|
+
self.total_weights = xp.zeros_like(weights[0])
|
|
386
|
+
self.idxs = [xp.array(el) > 0 for el in weights] # create mask for blocks
|
|
387
|
+
for el in weights:
|
|
388
|
+
el = xp.array(el)
|
|
389
|
+
self.weights.append(el[xp.array(el) > 0]) # remove non zero weights
|
|
390
|
+
self.total_weights += el / len(weights)
|
|
391
|
+
|
|
392
|
+
def _project(self, x):
|
|
393
|
+
# simultaneous projection
|
|
394
|
+
xp = cp if self._use_gpu else np
|
|
395
|
+
|
|
396
|
+
for el, idx in zip(self.weights, self.idxs): # get mask and associated weights
|
|
397
|
+
p = self.indexed_map(x, idx)
|
|
398
|
+
(res_l, res_u) = self.Bounds.indexed_residual(p, idx)
|
|
399
|
+
d_idx = res_u < 0
|
|
400
|
+
c_idx = res_l < 0
|
|
401
|
+
|
|
402
|
+
full_d_idx = xp.zeros(self.A.shape[0], dtype=bool)
|
|
403
|
+
full_c_idx = xp.zeros(self.A.shape[0], dtype=bool)
|
|
404
|
+
|
|
405
|
+
full_d_idx[idx] = d_idx
|
|
406
|
+
full_c_idx[idx] = c_idx
|
|
407
|
+
|
|
408
|
+
x += self.algorithmic_relaxation * (
|
|
409
|
+
el[d_idx] * res_u[d_idx] @ self.A_norm[full_d_idx, :]
|
|
410
|
+
- el[c_idx] * res_l[c_idx] @ self.A_norm[full_c_idx, :]
|
|
411
|
+
)
|
|
412
|
+
|
|
413
|
+
return x
|
|
414
|
+
|
|
415
|
+
def _proximity(self, x: npt.ArrayLike) -> float:
|
|
416
|
+
p = self.map(x)
|
|
417
|
+
(res_u, res_l) = self.Bounds.residual(p) # residuals are positive if constraints are met
|
|
418
|
+
d_idx = res_u < 0
|
|
419
|
+
c_idx = res_l < 0
|
|
420
|
+
return (self.total_weights[d_idx] * res_u[d_idx] ** 2).sum() + (
|
|
421
|
+
self.total_weights[c_idx] * res_l[c_idx] ** 2
|
|
422
|
+
).sum()
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
class StringAveragedAMS(HyperslabAMSAlgorithm):
|
|
426
|
+
"""
|
|
427
|
+
StringAveragedAMS is an implementation of the HyperslabAMSAlgorithm that
|
|
428
|
+
performs
|
|
429
|
+
string averaged projections.
|
|
430
|
+
|
|
431
|
+
Parameters
|
|
432
|
+
----------
|
|
433
|
+
A : npt.ArrayLike
|
|
434
|
+
The matrix A used in the algorithm.
|
|
435
|
+
lb : npt.ArrayLike
|
|
436
|
+
The lower bounds for the variables.
|
|
437
|
+
ub : npt.ArrayLike
|
|
438
|
+
The upper bounds for the variables.
|
|
439
|
+
strings : List[List[int]]
|
|
440
|
+
A list of lists, where each inner list represents a string of indices.
|
|
441
|
+
algorithmic_relaxation : npt.ArrayLike or float, optional
|
|
442
|
+
The relaxation parameter for the algorithm, by default 1.
|
|
443
|
+
relaxation : float, optional
|
|
444
|
+
The relaxation parameter for the projection, by default 1.
|
|
445
|
+
weights : None or List[float], optional
|
|
446
|
+
The weights for each string, by default None. If None, equal weights are assigned.
|
|
447
|
+
proximity_flag : bool, optional
|
|
448
|
+
A flag indicating whether to use proximity, by default True.
|
|
449
|
+
"""
|
|
450
|
+
|
|
451
|
+
def __init__(
|
|
452
|
+
self,
|
|
453
|
+
A: npt.ArrayLike,
|
|
454
|
+
lb: npt.ArrayLike,
|
|
455
|
+
ub: npt.ArrayLike,
|
|
456
|
+
strings: List[List[int]],
|
|
457
|
+
algorithmic_relaxation: npt.ArrayLike | float = 1,
|
|
458
|
+
relaxation: float = 1,
|
|
459
|
+
weights: None | List[float] = None,
|
|
460
|
+
proximity_flag: bool = True,
|
|
461
|
+
):
|
|
462
|
+
|
|
463
|
+
super().__init__(A, lb, ub, algorithmic_relaxation, relaxation, proximity_flag)
|
|
464
|
+
xp = cp if self._use_gpu else np
|
|
465
|
+
self.strings = strings
|
|
466
|
+
if weights is None:
|
|
467
|
+
self.weights = xp.ones(len(strings)) / len(strings)
|
|
468
|
+
|
|
469
|
+
# if check_weight_validity(weights):
|
|
470
|
+
# self.weights = weights
|
|
471
|
+
else:
|
|
472
|
+
if len(weights) != len(self.strings):
|
|
473
|
+
raise ValueError("The number of weights must be equal to the number of strings.")
|
|
474
|
+
|
|
475
|
+
self.weights = weights
|
|
476
|
+
# print('Choosing default weight vector...')
|
|
477
|
+
# self.weights = np.ones(self.A.shape[0])/self.A.shape[0]
|
|
478
|
+
|
|
479
|
+
def _project(self, x):
|
|
480
|
+
# string averaged projection
|
|
481
|
+
x_c = x.copy() # create a general copy of x
|
|
482
|
+
x -= x # reset x is this viable?
|
|
483
|
+
for string, weight in zip(self.strings, self.weights):
|
|
484
|
+
x_s = x_c.copy() # generate a copy for individual strings
|
|
485
|
+
for i in string:
|
|
486
|
+
p_i = self.single_map(x_s, i)
|
|
487
|
+
(res_li, res_ui) = self.Bounds.single_residual(p_i, i)
|
|
488
|
+
if res_ui < 0:
|
|
489
|
+
self.A_norm.update_step(x_s, self.algorithmic_relaxation * res_ui, i)
|
|
490
|
+
elif res_li < 0:
|
|
491
|
+
self.A_norm.update_step(x_s, -1 * self.algorithmic_relaxation * res_li, i)
|
|
492
|
+
|
|
493
|
+
x += weight * x_s
|
|
494
|
+
return x
|