Commit 05fafce9 authored by rnahme's avatar rnahme
Browse files

generalized types in configs, added conversions from strings to functions and test case

parent 46bb9fcd
......@@ -13,13 +13,10 @@ import unittest
from numpy import array, float32
from proxtoolbox.Algorithms import *
from proxtoolbox.Problems import *
#from proxtoolbox.Problems import Ptychography
#from proxtoolbox.Problems import Sudoku
from proxtoolbox.Problems.ptychography import PALM
from sudoku_in import new_config
class Test_Problems(unittest.TestCase):
"""
......@@ -49,12 +46,12 @@ class Test_Problems(unittest.TestCase):
config = {
# This is the algorithm we use. RAAR and HPR will work.
#'algorithm':RAAR,
'algorithm':'RAAR',
# RAAR requires 2 ProxOperators
#'proj1':P_diag,
#'proj2':P_parallel,
'proj1':'P_diag',
'proj2':'P_parallel',
# P_parallel requires a sequence of projectors
#'projectors':(ProjRow,ProjColumn,ProjSquare,ProjGiven),
'projectors':('ProjRow','ProjColumn','ProjSquare','ProjGiven'),
# Relaxation parameters for RAAR/HPR
'beta0':1,
'beta_max':1,
......@@ -71,15 +68,15 @@ class Test_Problems(unittest.TestCase):
'normM':81,
# Just a random Sudoku. Not too easy, but no challenge for
# the mighty ProxToolbox!
'sudoku':array(((0,4,5,7,0,0,0,6,1),
(0,0,0,8,0,5,4,0,0),
(0,0,7,6,4,0,0,0,2),
(5,0,0,9,6,4,0,0,0),
(6,0,8,5,0,1,0,3,0),
(0,7,0,0,0,0,0,9,0),
(3,0,2,4,0,0,1,0,0),
(9,0,0,1,0,0,0,0,6),
(0,0,0,0,0,3,8,0,0)),dtype=float32)
'sudoku':((0,4,5,7,0,0,0,6,1),
(0,0,0,8,0,5,4,0,0),
(0,0,7,6,4,0,0,0,2),
(5,0,0,9,6,4,0,0,0),
(6,0,8,5,0,1,0,3,0),
(0,7,0,0,0,0,0,9,0),
(3,0,2,4,0,0,1,0,0),
(9,0,0,1,0,0,0,0,6),
(0,0,0,0,0,3,8,0,0))
}
prob_sudoku = Sudoku(config)
......@@ -98,11 +95,31 @@ class Test_Problems(unittest.TestCase):
(9,5,4,1,8,7,3,2,6),
(7,1,6,2,5,3,8,4,9)),dtype=float32)).all())
def test_sudoku_configfile(self):
"""
Tests the initialization of a Sudoku instance with a config provided in a file
"""
prob_sudoku = Sudoku(new_config)
prob_sudoku.solve()
self.assertTrue((prob_sudoku.solution == array(((8,4,5,7,3,2,9,6,1),
(2,6,9,8,1,5,4,7,3),
(1,3,7,6,4,9,5,8,2),
(5,2,3,9,6,4,7,1,8),
(6,9,8,5,7,1,2,3,4),
(4,7,1,3,2,8,6,9,5),
(3,8,2,4,9,6,1,5,7),
(9,5,4,1,8,7,3,2,6),
(7,1,6,2,5,3,8,4,9)),dtype=float32)).all())
def test_ptychography(self):
"""
Tests the integrity of Problem 'Ptychography'
Tests the integrity of Problem 'Ptychography'
"""
config = {
'sim_data_type':'gaenseliesel',
'Nx':64,
......@@ -121,9 +138,9 @@ class Test_Problems(unittest.TestCase):
'probe_guess_type':'circle',
'object_guess_type':'random',
'warmup':True,
'warmup_alg':PALM,
'warmup_alg':'PALM',
'warmup_maxiter':1,
'algorithm':PALM,
'algorithm':'PALM',
'maxiter':1, # This is usually 300. I reduced it to be able to test it.
'tol':0.625,
'ignore_error':False,
......
# -*- coding: utf-8 -*-
new_config = {
# This is the algorithm we use. RAAR and HPR will work.
'algorithm':'RAAR',
# RAAR requires 2 ProxOperators
'proj1':'P_diag',
'proj2':'P_parallel',
# P_parallel requires a sequence of projectors
'projectors':('ProjRow','ProjColumn','ProjSquare','ProjGiven'),
# Relaxation parameters for RAAR/HPR
'beta0':1,
'beta_max':1,
'beta_switch':1,
# Any algorithm requires these
'maxiter':2000,
'tol':1e-9,
# Dimension parameters
# which are the same for every standard Sudoku
'Nx':9,
'Ny':9,
'Nz':9,
'dim':4,
'normM':81,
# Just a random Sudoku. Not too easy, but no challenge for
# the mighty ProxToolbox!
'sudoku':((0,4,5,7,0,0,0,6,1),
(0,0,0,8,0,5,4,0,0),
(0,0,7,6,4,0,0,0,2),
(5,0,0,9,6,4,0,0,0),
(6,0,8,5,0,1,0,3,0),
(0,7,0,0,0,0,0,9,0),
(3,0,2,4,0,0,1,0,0),
(9,0,0,1,0,0,0,0,6),
(0,0,0,0,0,3,8,0,0))
}
\ No newline at end of file
......@@ -1649,9 +1649,9 @@ class Ptychography(Problem):
'probe_guess_type':'circle',
'object_guess_type':'random',
'warmup':True,
'warmup_alg':PALM,
'warmup_alg':'PALM',
'warmup_maxiter':2,
'algorithm':PALM,
'algorithm':'PALM',
'maxiter':2, # This is usually 300. I reduced it to be able to test it.
'tol':0.625,
'ignore_error':False,
......@@ -1684,6 +1684,10 @@ class Ptychography(Problem):
"""
self.config.update(new_config)
#self.config['algorithm'] = getattr(Algorithms, self.config['algorithm'])
self.config['algorithm'] = globals()[self.config['algorithm']]
self.config['warmup_alg'] = globals()[self.config['warmup_alg']]
if self.config['ptychography_prox'] == 'PALM':
self.config['projector_sequence'] = [P_PHeBIE_probe,
P_PHeBIE_object,
......@@ -1712,9 +1716,9 @@ class Ptychography(Problem):
raise NotImplementedError ( \
'Operator set \'%s\' not (yet) supported'%self.config['ptychography_prox'])
self.config['N_pie'] = self.config['nx']*self.config['ny'];
self.config['N_pie'] = self.config['nx']*self.config['ny']
self.config['dim'] = self.config['N_pie']; # warum zwei Parameter mit gleichem Wert vorhalten?
self.config['normM'] = 1;
self.config['normM'] = 1
def _gaussu(self, rho, z, l, wnull):
......
......@@ -9,8 +9,8 @@ from numpy import amax, argmax, array, float32, int32, zeros, zeros_like
from matplotlib import pyplot
from .problems import Problem
from proxtoolbox.Algorithms import RAAR
from proxtoolbox.ProxOperators import P_diag, P_parallel
from proxtoolbox import Algorithms
from proxtoolbox import ProxOperators
from proxtoolbox.ProxOperators.proxoperators import ProxOperator
#__all__ = ["Sudoku"]
......@@ -182,12 +182,12 @@ class Sudoku(Problem):
"""
config = {
# This is the algorithm we use. RAAR and HPR will work.
'algorithm':RAAR,
'algorithm':'RAAR',
# RAAR requires 2 ProxOperators
'proj1':P_diag,
'proj2':P_parallel,
'proj1':'P_diag',
'proj2':'P_parallel',
# P_parallel requires a sequence of projectors
'projectors':(ProjRow,ProjColumn,ProjSquare,ProjGiven),
'projectors':('ProjRow','ProjColumn','ProjSquare','ProjGiven'),
# Relaxation parameters for RAAR/HPR
'beta0':1,
'beta_max':1,
......@@ -204,15 +204,24 @@ 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':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),
(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))
}
def __init__(self, new_config={}):
......@@ -225,6 +234,17 @@ class Sudoku(Problem):
new_config : dict, optional - Parameters to initialize the problem. If unspecified, the default config is used.
"""
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'])
projs = []
for p in self.config['projectors']:
projs.append(globals()[p])
self.config['projectors'] = projs
self.config['sudoku'] = array(self.config['sudoku'],dtype=float32)
def _presolve(self):
......
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