summaryrefslogtreecommitdiffstats
path: root/Wrappers/Python/ccpi/optimisation/functions/IndicatorBox.py
blob: 51d08d1315c83e4737499a74b44d0474e18c872c (plain)
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
# -*- coding: utf-8 -*-
#  CCP in Tomographic Imaging (CCPi) Core Imaging Library (CIL).

#   Copyright 2017 UKRI-STFC
#   Copyright 2017 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

#   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 ccpi.optimisation.functions import Function
import numpy

class IndicatorBox(Function):
    
    
    r'''Indicator function for box constraint
            
      .. math:: 
         f(x) = \mathbb{I}_{[a, b]} = \begin{cases}
            
                                            0, if x\in[a, b]
                                            \infty, otherwise                            
                                    \end{cases}
    
    '''
    
    def __init__(self,lower=-numpy.inf,upper=numpy.inf):

        super(IndicatorBox, self).__init__()
        self.lower = lower
        self.upper = upper
        

    def __call__(self,x):
        
        '''Evaluates IndicatorBox at x'''
                
        if (numpy.all(x.array>=self.lower) and 
            numpy.all(x.array <= self.upper) ):
            val = 0
        else:
            val = numpy.inf
        return val
    
    def gradient(self,x):
        return ValueError('Not Differentiable') 
    
    def convex_conjugate(self,x):
        
        '''Convex conjugate of IndicatorBox at x'''

        return x.maximum(0).sum()
         
    def proximal(self, x, tau, out=None):
        
        r'''Proximal operator of IndicatorBox at x

            .. math:: prox_{\tau * f}(x)
        '''
        
        if out is None:
            return (x.maximum(self.lower)).minimum(self.upper)        
        else:               
            x.maximum(self.lower, out=out)
            out.minimum(self.upper, out=out) 
            
    def proximal_conjugate(self, x, tau, out=None):
        
        r'''Proximal operator of the convex conjugate of IndicatorBox at x:

          ..math:: prox_{\tau * f^{*}}(x)
        '''

        if out is None:
            
            return x - tau * self.proximal(x/tau, tau)
        
        else:
            
            self.proximal(x/tau, tau, out=out)
            out *= -1*tau
            out += x

            
            
if __name__ == '__main__':  

    from ccpi.framework import ImageGeometry, BlockDataContainer

    N, M = 2,3
    ig = ImageGeometry(voxel_num_x = N, voxel_num_y = M)            
    
    u = ig.allocate('random_int')
    tau = 2
    
    f = IndicatorBox(2, 3)
    
    lower = 10
    upper = 30
        
    z1 = f.proximal(u, tau)
    
    z2 = f.proximal_conjugate(u/tau, 1/tau)
    
    z = z1 + tau * z2
    
    numpy.testing.assert_array_equal(z.as_array(), u.as_array())  

    out1 = ig.allocate()
    out2 = ig.allocate()
    
    f.proximal(u, tau, out=out1)
    f.proximal_conjugate(u/tau, 1/tau, out = out2)
    
    p = out1 + tau * out2
    
    numpy.testing.assert_array_equal(p.as_array(), u.as_array()) 
    
    d = f.convex_conjugate(u)
    print(d)
    
    
    
    # what about n-dimensional Block
    #uB = BlockDataContainer(u,u,u)
    #lowerB = BlockDataContainer(1,2,3)
    #upperB = BlockDataContainer(10,21,30)
    
    #fB = IndicatorBox(lowerB, upperB)
    
    #z1B = fB.proximal(uB, tau)