Commits (6)
__pycache__
*.csv
*.swp
......@@ -59,8 +59,18 @@ For instance, the `init` operator's interface takes no input parameter
and returns a solution to the problem.
However, some operator may need additional parameters to be passed.
To solve this problem, the framework use an interface capture pattern,
implemented in the `make` module.
To solve this problem, the framework use an interface capture pattern.
There is two ways to capture the interface: either with a functional approach,
either with an object-oriented approach.
You can use the one you prefer, the advice is to use the functional approach when
you can implement your operator as a stateless function,
and the object-oriented approach when you need àour operator to manage a state.
#### Function capture
The functional capture helpers are implemented in the `make` module.
Basically, a function in this module capture the operator function's full
interface and returns a function having the expected interface of the
operator.
......@@ -70,15 +80,26 @@ on which the operator is defined, and keyword arguments for parameters
which are specific to the operator.
#### Object-oriented capture
The object-oriented approach does not need helpers,
you just need to define a "functor" class,
that is, a class which implements the `__call__` interface.
This special function member allows to call an instance of a class
as if it was a function.
The `__call__` method should honor the targeted operator interface.
To pass fixed parameters, use the `__init__` constructor.
There is an example of an operator implemented this way
as the `steady` class in the `sho/iters.py` file.
Exercises
---------
### Setup
To setup your own solver, first copy the `snp.py` file and rename it
with your name, for instance `dreo.py`.
You will then add your algorithm(s) into this executable.
Two example algorithms are provided: a `random` search
and a `greedy` search.
Several useful stopping criterions are provided.
......@@ -87,6 +108,14 @@ for both numeric and bitstring encodings.
The `snp.py` file shows how to assemble either a numeric greedy solver
or a bitstring greedy solver.
To setup your own solver, add your algorithm(s) into the `algo.py` module,
then assemble its instance under its name into `snp.py`.
For instance, if you created the `annealing` algorithm,
you will be able to immediatly assemble `num_annealing` and `bit_annealing`.
One should be able to call your solvers with `python3 snp.py --solver num_annealing`,
for instance.
### List of exercises
......
......@@ -49,9 +49,10 @@ def rand(dim, scale):
########################################################################
def neighb_square(sol, scale, domain_width):
"""Draw a random vector in a square of witdh `scale`
around the given one."""
"""Draw a random vector in a square of witdh `scale` in [0,1]
as a fraction of the domain width around the given solution."""
assert(0 < scale <= 1)
new = sol + (np.random.random(len(sol)) * scale - scale/2)
side = domain_width * scale;
new = sol + (np.random.random(len(sol)) * side - side/2)
return new
......@@ -40,9 +40,9 @@ if __name__ == "__main__":
fixed_x = (x0,x1) ),
make.init(num.rand,
dim = 2, # Two sensors moving along y axis.
scale = w),
scale = 1.0),
make.neig(num.neighb_square,
scale = 0.1 * w,
scale = 0.1,
domain_width = w
),
iters
......