Commit e3d71f71 authored by rnahme's avatar rnahme
Browse files

created working config containing classes and instances in Sudoku

parent 05fafce9
......@@ -25,10 +25,8 @@ class RAAR(Algorithm):
Dictionary containing the problem configuration.
It must contain the following mappings:
proj1: ProxOperator
First ProxOperator (the class, no instance)
proj2: ProxOperator
Second ProxOperator (the class, no instance)
proxoperators: 2 ProxOperators
Tuple of ProxOperators (the class, no instance)
beta0: number
Starting relaxation parmater
beta_max: number
......@@ -46,8 +44,8 @@ class RAAR(Algorithm):
dim: int
Size of the product space
"""
self.proj1 = config['proj1'](config)
self.proj2 = config['proj2'](config)
self.proj1 = config['proxoperators'][0](config)
self.proj2 = config['proxoperators'][1](config)
self.normM = config['normM']
self.beta0 = config['beta0']
self.beta_max = config['beta_max']
......@@ -62,18 +60,15 @@ class RAAR(Algorithm):
"""
##### PREPROCESSING
proj1 = self.proj1;
proj2 = self.proj2;
normM = self.normM
normM = self.normM;
beta = self.beta0;
beta = self.beta0
iters = self.iters
change = zeros(maxiter+1,dtype=u.dtype);
change[0] = 999;
gap = change.copy();
change = zeros(maxiter+1,dtype=u.dtype)
change[0] = 999
gap = change.copy()
tmp1 = 2*proj2.work(u) - u;
tmp1 = 2*self.proj2.work(u) - u
##### LOOP
......@@ -82,11 +77,11 @@ class RAAR(Algorithm):
beta = (tmp*self.beta0) + ((1-tmp)*self.beta_max);
iters += 1;
tmp3 = proj1.work(tmp1);
tmp3 = self.proj1.work(tmp1);
tmp_u = ((beta*(2*tmp3-tmp1)) + ((1-beta)*tmp1) + u)/2;
tmp2 = proj2.work(tmp_u);
tmp2 = self.proj2.work(tmp_u);
tmp3 = proj1.work(tmp2);
tmp3 = self.proj1.work(tmp2);
tmp_change = 0; tmp_gap = 0;
if self.Ny == 1 or self.Nx == 1:
......@@ -111,8 +106,8 @@ class RAAR(Algorithm):
##### POSTPROCESSING
u = tmp2;
tmp = proj1.work(u);
tmp2 = proj2.work(u);
tmp = self.proj1.work(u);
tmp2 = self.proj2.work(u);
if self.Ny == 1:
u1 = tmp[:,1];
u2 = tmp2[:,1];
......@@ -126,6 +121,6 @@ class RAAR(Algorithm):
u1 = tmp;
u2 = tmp2;
change = change[1:iters+1];
gap = gap[1:iters+1];
gap = gap[1:iters+1];
return u1, u2, iters, change, gap;
return u1, u2, iters, change, gap
......@@ -184,8 +184,7 @@ class Sudoku(Problem):
# This is the algorithm we use. RAAR and HPR will work.
'algorithm':'RAAR',
# RAAR requires 2 ProxOperators
'proj1':'P_diag',
'proj2':'P_parallel',
'proxoperators':('P_diag','P_parallel'),
# P_parallel requires a sequence of projectors
'projectors':('ProjRow','ProjColumn','ProjSquare','ProjGiven'),
# Relaxation parameters for RAAR/HPR
......@@ -204,15 +203,6 @@ class Sudoku(Problem):
'normM':81,
# Just a random Sudoku. Not too easy, but no challenge for
# the mighty ProxToolbox!
# 'sudoku':array(((2,0,0,0,0,1,0,3,0),
# (4,0,0,0,8,6,1,0,0),
# (0,0,0,0,0,0,0,0,0),
# (0,0,0,0,1,0,0,0,0),
# (0,0,0,0,0,0,9,0,0),
# (0,0,5,0,0,3,0,0,7),
# (0,0,0,0,0,0,0,0,0),
# (1,0,0,0,0,7,4,9,0),
# (0,2,4,1,0,0,0,0,0)),dtype=float32)
'sudoku':((2,0,0,0,0,1,0,3,0),
(4,0,0,0,8,6,1,0,0),
(0,0,0,0,0,0,0,0,0),
......@@ -235,14 +225,24 @@ class Sudoku(Problem):
"""
self.config.update(new_config)
self.config['algorithm'] = getattr(Algorithms, self.config['algorithm'])
self.config['proj1'] = getattr(ProxOperators, self.config['proj1'])
self.config['proj2'] = getattr(ProxOperators, self.config['proj2'])
add_config = self.config.copy()
add_config['proxoperators'] = []
for prox in self.config['proxoperators']:
add_config['proxoperators'].append(getattr(ProxOperators, prox))
# add_config.update({'prox1':getattr(ProxOperators, self.config['proj1']),
# 'prox2':getattr(ProxOperators, self.config['proj2'])})
projs = []
# self.config['algorithm'] = getattr(Algorithms, self.config['algorithm'])
# self.config['proj1'] = getattr(ProxOperators, self.config['proj1'])
# self.config['proj2'] = getattr(ProxOperators, self.config['proj2'])
add_config['projectors'] = []
for p in self.config['projectors']:
projs.append(globals()[p])
self.config['projectors'] = projs
add_config['projectors'].append(globals()[p])
add_config['sudoku'] = array(self.config['sudoku'],dtype=float32)
self.algorithm = getattr(Algorithms, self.config['algorithm'])(add_config)
self.config['sudoku'] = array(self.config['sudoku'],dtype=float32)
......@@ -251,13 +251,11 @@ class Sudoku(Problem):
"""
Prepares argument for actual solving routine
"""
sudoku = self.config['sudoku']
u = zeros((9,9,9,4),dtype=sudoku.dtype)
u = zeros((9,9,9,4),dtype=self.config['sudoku'].dtype)
for x in xrange(9):
for y in xrange(9):
z = sudoku[x,y]-1
z = self.config['sudoku'][x,y]-1
if z >= 0:
u[x,y,z,:] = 1
......@@ -268,10 +266,10 @@ class Sudoku(Problem):
"""
Runs the algorithm to solve the given sudoku problem
"""
algorithm = self.config['algorithm'](self.config)
# algorithm = self.config['algorithm'](self.config)
self.u1,self.u2,self.iters,self.change,self.gap = \
algorithm.run(self.u,self.config['tol'],self.config['maxiter'])
self.algorithm.run(self.u,self.config['tol'],self.config['maxiter'])
def _postsolve(self):
......
......@@ -167,6 +167,12 @@ class P_parallel(ProxOperator):
for p in config['projectors']:
self.proj.append(p(config))
# for p in config['projectors']:
# self.proj.append(globals()[p](config))
# pp = globals()[p]
# self.proj.append(pp(config))
def work(self,u):
"""
Sequentially applies the projections of 'self.proj' to the input data.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment