1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# -*- coding: utf-8 -*-
# Copyright 2019 Science Technology Facilities Council
# Copyright 2019 University of Manchester
#
# This work is part of the Core Imaging Library developed by Science Technology
# Facilities Council and University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ccpi.optimisation.algorithms import Algorithm
class PDHG(Algorithm):
r'''Primal Dual Hybrid Gradient
Problem:
.. math::
\min_{x} f(Kx) + g(x)
:param operator: Linear Operator = K
:param f: Convex function with "simple" proximal of its conjugate.
:param g: Convex function with "simple" proximal
:param sigma: Step size parameter for Primal problem
:param tau: Step size parameter for Dual problem
Remark: Convergence is guaranted provided that
.. math::
\tau \sigma \|K\|^{2} <1
Reference:
(a) A. Chambolle and T. Pock (2011), "A first-order primal–dual algorithm for convex
problems with applications to imaging", J. Math. Imaging Vision 40, 120–145.
(b) E. Esser, X. Zhang and T. F. Chan (2010), "A general framework for a class of first
order primal–dual algorithms for convex optimization in imaging science",
SIAM J. Imaging Sci. 3, 1015–1046.
'''
def __init__(self, f=None, g=None, operator=None, tau=None, sigma=1.,**kwargs):
'''PDHG algorithm creator
Optional parameters
:param operator: a Linear Operator
:param f: Convex function with "simple" proximal of its conjugate.
:param g: Convex function with "simple" proximal
:param sigma: Step size parameter for Primal problem
:param tau: Step size parameter for Dual problem
'''
super(PDHG, self).__init__(**kwargs)
if f is not None and operator is not None and g is not None:
self.set_up(f=f, g=g, operator=operator, tau=tau, sigma=sigma)
def set_up(self, f, g, operator, tau=None, sigma=1.):
'''initialisation of the algorithm
:param operator: a Linear Operator
:param f: Convex function with "simple" proximal of its conjugate.
:param g: Convex function with "simple" proximal
:param sigma: Step size parameter for Primal problem
:param tau: Step size parameter for Dual problem'''
print("{} setting up".format(self.__class__.__name__, ))
# can't happen with default sigma
if sigma is None and tau is None:
raise ValueError('Need sigma*tau||K||^2<1')
# algorithmic parameters
self.f = f
self.g = g
self.operator = operator
self.tau = tau
self.sigma = sigma
if self.tau is None:
# Compute operator Norm
normK = self.operator.norm()
# Primal & dual stepsizes
self.tau = 1 / (self.sigma * normK ** 2)
self.x_old = self.operator.domain_geometry().allocate()
self.x_tmp = self.x_old.copy()
self.x = self.x_old.copy()
self.y_old = self.operator.range_geometry().allocate()
self.y_tmp = self.y_old.copy()
self.y = self.y_old.copy()
self.xbar = self.x_old.copy()
# relaxation parameter
self.theta = 1
self.update_objective()
self.configured = True
print("{} configured".format(self.__class__.__name__, ))
def update(self):
# save previous iteration
self.x_old.fill(self.x)
self.y_old.fill(self.y)
# Gradient ascent for the dual variable
self.operator.direct(self.xbar, out=self.y_tmp)
# self.y_tmp *= self.sigma
# self.y_tmp += self.y_old
self.y_tmp.axpby(self.sigma, 1 , self.y_old, self.y_tmp)
# self.y = self.f.proximal_conjugate(self.y_old, self.sigma)
self.f.proximal_conjugate(self.y_tmp, self.sigma, out=self.y)
# Gradient descent for the primal variable
self.operator.adjoint(self.y, out=self.x_tmp)
# self.x_tmp *= -1*self.tau
# self.x_tmp += self.x_old
self.x_tmp.axpby(-self.tau, 1. , self.x_old, self.x_tmp)
self.g.proximal(self.x_tmp, self.tau, out=self.x)
# Update
self.x.subtract(self.x_old, out=self.xbar)
# self.xbar *= self.theta
# self.xbar += self.x
self.xbar.axpby(self.theta, 1 , self.x, self.xbar)
def update_objective(self):
p1 = self.f(self.operator.direct(self.x)) + self.g(self.x)
d1 = -(self.f.convex_conjugate(self.y) + self.g.convex_conjugate(-1*self.operator.adjoint(self.y)))
self.loss.append([p1, d1, p1-d1])
|