BayesOpt
demo_multiprocess.py
1 #!/usr/bin/env python
2 # -------------------------------------------------------------------------
3 # This file is part of BayesOpt, an efficient C++ library for
4 # Bayesian optimization.
5 #
6 # Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 #
8 # BayesOpt is free software: you can redistribute it and/or modify it
9 # under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # BayesOpt is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 # ------------------------------------------------------------------------
21 
22 import numpy as np
23 from bayesoptmodule import BayesOptContinuous
24 from multiprocessing import Process, Pipe
25 
26 # Function for testing.
27 def testfunc(Xin):
28  total = 5.0
29  for value in Xin:
30  total = total + (value -0.33)*(value-0.33)
31  return total
32 
33 
34 def worker(pipe):
35  x = None
36  while True:
37  x = pipe.recv()
38  if x == 'STOP':
39  break
40 
41  result = testfunc(x)
42  pipe.send(result)
43 
44 
46 
47  def __init__ (self, pipe, n_dim):
48  Process.__init__(self)
49  BayesOptContinuous.__init__(self, n_dim)
50  self.pipe = pipe
51 
52  def run(self):
53  mvalue, x_out, error = self.optimize()
54  self.pipe.send('STOP')
55  self.mvalue = mvalue
56  self.x_out = x_out
57  self.error = error
58 
59  return
60 
61  def evaluateSample(self, x):
62  self.pipe.send(x)
63  result = self.pipe.recv()
64  return result
65 
66 
67 if __name__ == '__main__':
68  params = {
69  'n_iterations' : 50,
70  'n_init_samples' : 20,
71  's_name' : "sGaussianProcessNormal",
72  'c_name' : "cHedge(cEI,cLCB,cExpReturn,cOptimisticSampling)"
73  }
74 
75  pipe_par, pipe_child = Pipe()
76 
77  bo = BayesOptProcess(pipe_child,n_dim=5)
78  bo.parameters = params
79 
80  p = Process(target=worker, args=(pipe_par,))
81 
82  bo.start()
83  p.start()
84 
85  bo.join()
86  p.join()
def optimize(self)
Main function.
Python Module for BayesOptContinuous.