def test_shu(self): # Shubert u = numpy.array([-10, -10]) v = numpy.array([10, 10]) x0 = numpy.array([1, 1]) fglob = -186.730908831024 xglob = numpy.array([[ -7.08350658, 5.48286415, 4.85805691, 4.85805691, -7.08350658, -7.70831382, -1.42512845, -0.80032121, -1.42512844, -7.08350639, -7.70831354, 5.48286415, 5.48286415, 4.85805691, -7.70831354, -0.80032121, -1.42512845, -0.80032121 ], [ 4.85805691, 4.85805681, -7.08350658, 5.48286415, -7.70831382, -7.08350658, -0.80032121, -1.42512845, -7.08350639, -1.42512844, 5.48286415, -7.70831354, 4.85805691, 5.48286415, -0.80032121, -7.70831354, -0.80032121, -1.42512845 ]]) xbest, fbest, ncall = snobfit(shu, x0, (u, v), fglob=fglob, fac=self.factor, rtol=rtol) assert abs((fbest - fglob) / fglob) < rtol print "shu", xbest, fbest, ncall
def optimize(self): gr = self.graph gr.generateGlobalVariables() print("Decision Variables") print("---------------------") xnames = gr.opt.v self.xnames = xnames gr.scaleGlobalVariables(xnames) xinit = gr.getGlobalVariables(xnames, "scaled", True) for xn in xnames: print( (xn + ": " + str(gr.x[xn].value) + " scaled: " + str(gr.x[xn].scaled)) ) print("----------------------") print("Optimizer = SNOBFIT-NIST") opts = copy.deepcopy(self.options) itmax = opts.pop("itmax") ofile = opts.pop("Output File") self.flushm = opts.pop("Flush Modulus") upper = opts.pop("upper") lower = opts.pop("lower") bounds = [numpy.array(lower), numpy.array(upper)] if itmax == 0: itmax = 50000 # in nmax is 0 just set max iterations to large number # # setup solver set options ... f = len(xnames) * [0] it = 0 if ofile != "": gr.createCSV(ofile) # create a CSV output file add a coulmn for objective self.ofile = ofile self.flushc = 0 start = time.process_time() xopt, fopt, nit = snobfit.snobfit(self.func, xinit, bounds, maxiter=itmax) if ofile != "": gr.closeCSV() print("-------------------------") print(("Elapsed Time: " + str(time.process_time() - start) + " sec")) print("-------------------------") print("Solution") print("-------------------------") # resolve with opt so the best solution will be on flowsheet gr.setGlobalVariables(xnames, xopt, "scaled", True) gr.solve() # resolve with optimal input xfinal = gr.getGlobalVariables(xnames, "value", True) print(("best f " + str(fopt))) print(("best x (scaled): " + str(xopt))) print(("best x (notscaled): " + str(xfinal))) print(("number of iterations: " + str(nit)))
def snobtest( ): (u,v,fglob) = defaults() xbest, fbest, ncall = snobfit(reflectivity, p0, (u, v), dn=10, maxiter=1000, retall=True ) print xbest,fbest, ncall
def solve(self, monitors=None, mapper=None, **options): options = _fill_defaults(options, self.settings) # TODO: no mapper?? from snobfit.snobfit import snobfit self._update = MonitorRunner(problem=self.problem, monitors=monitors) x, fx, _ = snobfit(self.problem, self.problem.getp(), self.problem.bounds(), fglob=0, callback=self._monitor) return x, fx
def test_cam(self): # six-hump camel u = numpy.array([-3, -2]) v = numpy.array([3, 2]) x0 = numpy.array([1, 1]) fglob = -1.0316284535 xglob = numpy.array([0.08984201, -0.08984201, -0.71265640, 0.71265640]) xbest, fbest, ncall = snobfit(cam, x0, (u, v), fglob=fglob) assert abs(fbest - fglob) < 1.0e-6
def test_sh7(self): #Shekel 7 u = numpy.array([0, 0, 0, 0]) v = numpy.array([10, 10, 10, 10]) x0 = numpy.array([5, 5, 5, 5]) fglob = -10.4029405668187 xglob = numpy.array([4, 4, 4, 4]) xbest, fbest, ncall = snobfit(sh7, x0, (u, v), fglob=fglob) assert abs(fbest - fglob) < 1.0e-6
def test_sh5(self): # Shekel 5 u = numpy.array([0, 0, 0, 0]) v = numpy.array([10, 10, 10, 10]) x0 = numpy.array([5, 5, 5, 5]) fglob = -10.1531996790582 xglob = numpy.array([4, 4, 4, 4]) xbest, fbest, ncall = snobfit(sh5, x0, (u, v), fglob=fglob) assert abs(fbest - fglob) < 1.0e-6
def test_sh10(self): #Shekel 7 u = numpy.array([0, 0, 0, 0]) v = numpy.array([10, 10, 10, 10]) x0 = numpy.array([5, 5, 5, 5]) fglob = -10.5364098166920 xglob = numpy.array([4, 4, 4, 4]) xbest, fbest, ncall = snobfit(sh10, x0, (u, v), fglob=fglob) assert abs(fbest - fglob) < 1.0e-6
def test_hm3(self): # Hartman 3 u = numpy.array([0, 0, 0]) v = numpy.array([1, 1, 1]) x0 = numpy.array([0.5, 0.5, 0.5]) fglob = -3.86278214782076 xglob = numpy.array([0.1, 0.55592003, 0.85218259]) xbest, fbest, ncall = snobfit(hm3, x0, (u, v), fglob=fglob) assert abs(fbest - fglob) < 1.0e-6
def test_big20(self): # big dimension n = 20 u = numpy.array(range(0, n)) * 0.5 - 3 v = numpy.array(range(0, n)) * 0.5 + 2 x0 = numpy.array(range(0, n)) * 0.5 + 0.3 fglob = 0 xglob = numpy.array(range(0, n)) * 0.5 xbest, fbest, ncall = snobfit(big20, x0, (u, v), fglob=fglob, retall=1) assert abs(fbest - fglob) < 1.0e-6
def test_bra(self): u = numpy.array([-5, 0]) v = numpy.array([10, 15]) x0 = numpy.array([2, 3]) fglob = 0.397887357729739 xglob = numpy.array([[9.42477796, -3.14159265, 3.14159265], [2.47499998, 12.27500000, 2.27500000]]) xbest, fbest, ncall = snobfit(bra, x0, (u, v), fglob=fglob) assert abs(fbest - fglob) < 1.0e-6
def solve(self, monitors=None, mapper=None, **options): _fill_defaults(options, self.settings) # TODO: no mapper?? from snobfit.snobfit import snobfit self._update = MonitorRunner(problem=self.problem, monitors=monitors) bounds = numpy.array([p.bounds.limits for p in self.problem.parameters]).T x, fx, _ = snobfit(self.problem, self.problem.getp(), bounds, fglob=0, callback=self._monitor) return x, fx
def test_ros(self): """ Rosenbrock """ x0 = numpy.array([2, 3]) u = -5.12 * numpy.ones(2) v = -u fglob = 0 xglob = numpy.array([1, 1]) xbest, fbest, ncall = snobfit(ros, x0, (u, v)) # print 'optimize:',xbest, fbest, ncall assert abs(fbest) < 1.0e-6
def test_ros(self): """ Rosenbrock """ x0 = numpy.array([2, 3]) u = -5.12 * numpy.ones(2) v = -u fglob = 0 xglob = numpy.array([1, 1]) xbest, fbest, ncall = snobfit(ros, x0, (u, v), fac=self.factor) assert abs(fbest) < 1.0e-6 print "ros: ", xbest, fbest, ncall
def test_hm6(self): # Hartman 6 u = numpy.array([0, 0, 0, 0, 0, 0]) v = numpy.array([1, 1, 1, 1, 1, 1]) x0 = numpy.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5]) fglob = -3.32236801141551 xglob = numpy.array([ 0.20168952, 0.15001069, 0.47687398, 0.27533243, 0.31165162, 0.65730054 ]) xbest, fbest, ncall = snobfit(hm6, x0, (u, v), fglob=fglob) assert abs((fbest - fglob) / fglob) < 1.0e-6
def test(): u = -5.0 * numpy.ones(2) v = 5.0 * numpy.ones(2) x0 = numpy.array([0, 1]) fglob = 0 f = Fm() f.set_o( -numpy.array([[-0.7880367, -0.61562826], [-0.61562826, 0.7880367]])) xbest, fbest, ncall = snobfit(f, x0, (u, v), fglob=fglob, retall=1) print "The global minimizer", f.getGlobalMinimizer() print "The global function value", f.getGlobalFunc() print "The best solution", xbest, fbest, ncall
def test_hsf18(self): x0 = numpy.array([20, 2]) u = numpy.array([2, 0]) v = numpy.array([50, 50]) fglob = 5 xbest, fbest, ncall = snobfit(hsf18, x0, (u, v), constraintFunc=hsc18, retall=0, disp=0, fglob=fglob) assert abs((fbest - fglob) / fglob) < 1.0e-2
def test_bra(self): u = numpy.array([-5, 0]) v = numpy.array([10, 15]) x0 = numpy.array([2, 3]) fglob = 0.397887357729739 xglob = numpy.array([[9.42477796, -3.14159265, 3.14159265], [2.47499998, 12.27500000, 2.27500000]]) xbest, fbest, ncall = snobfit(bra, x0, (u, v), fglob=fglob, rtol=rtol, fac=self.factor) assert abs((fbest - fglob) / fglob) < rtol print "branin", xbest, fbest, ncall
def test_cam(self): # six-hump camel u = numpy.array([-3, -2]) v = numpy.array([3, 2]) x0 = numpy.array([1, 1]) fglob = -1.0316284535 xglob = numpy.array([0.08984201, -0.08984201, -0.71265640, 0.71265640]) xbest, fbest, ncall = snobfit(cam, x0, (u, v), fglob=fglob, rtol=rtol, fac=self.factor) assert abs((fbest - fglob) / fglob) < rtol print "cam", xbest, fbest, ncall
def test_sh5(self): # Shekel 5 u = numpy.array([0, 0, 0, 0]) v = numpy.array([10, 10, 10, 10]) x0 = numpy.array([5, 5, 5, 5]) fglob = -10.1531996790582 xglob = numpy.array([4, 4, 4, 4]) xbest, fbest, ncall = snobfit(sh5, x0, (u, v), fglob=fglob, rtol=rtol, fac=self.factor) assert abs((fbest - fglob) / fglob) < rtol print "sh5", xbest, fbest, ncall
def test_sh7(self): #Shekel 7 u = numpy.array([0, 0, 0, 0]) v = numpy.array([10, 10, 10, 10]) x0 = numpy.array([5, 5, 5, 5]) fglob = -10.4029405668187 xglob = numpy.array([4, 4, 4, 4]) xbest, fbest, ncall = snobfit(sh7, x0, (u, v), fglob=fglob, rtol=rtol, fac=self.factor) assert abs((fbest - fglob) / fglob) < rtol print "sh7", xbest, fbest, ncall
def test_sh10(self): # Shekel 10 u = numpy.array([0, 0, 0, 0]) v = numpy.array([10, 10, 10, 10]) x0 = numpy.array([5, 5, 5, 5]) fglob = -10.5364098166920 xglob = numpy.array([4, 4, 4, 4]) xbest, fbest, ncall = snobfit(sh10, x0, (u, v), fglob=fglob, rtol=rtol, fac=self.factor) assert abs((fbest - fglob) / fglob) < rtol print "sh10", xbest, fbest, ncall
def test_hm3(self): # Hartman 3 u = numpy.array([0, 0, 0]) v = numpy.array([1, 1, 1]) x0 = numpy.array([0.5, 0.5, 0.5]) fglob = -3.86278214782076 xglob = numpy.array([0.1, 0.55592003, 0.85218259]) xbest, fbest, ncall = snobfit(hm3, x0, (u, v), fglob=fglob, rtol=rtol, fac=self.factor) assert abs((fbest - fglob) / fglob) < rtol print "hm3", xbest, fbest, ncall
def testsnob4(): u = -5.0 * numpy.ones(4) v = 5.0 * numpy.ones(4) x0 = numpy.array([0.7, 4.5, 2.8, -2.4]) fglob = 0 f = Fm(n=4, m=2) o = ortho_r(4) f.set_o(o) print "The global minimizer", f.getGlobalMinimizer() print "The global function value", f.getGlobalFunc() xbest, fbest, ncall = snobfit(f, x0, (u, v), dn=12, fglob=fglob, retall=1) print "The global minimizer", f.getGlobalMinimizer() print "The global function value", f.getGlobalFunc() print "The best solution", xbest, fbest, ncall
def testsnob16(): u = -6.0 * numpy.ones(16) v = 6.0 * numpy.ones(16) x0 = numpy.array([ 0.6, -0.5, 2.8, 0, -0.58, -5.1, -3.3, 4.0, 6, 4.3, 2.58, 0.53, -1.6, 1.3, -0.6, -4.3 ]) fglob = 0 f = Fm(n=16, m=8) o = ortho_r(16) f.set_o(o) print "The global minimizer", f.getGlobalMinimizer() print "The global function value", f.getGlobalFunc() xbest, fbest, ncall = snobfit(f, x0, (u, v), dn=16, fglob=fglob, retall=1) print "The global minimizer", f.getGlobalMinimizer() print "The global function value", f.getGlobalFunc() print "The best solution", xbest, fbest, ncall