5. pyopus.problems — Test problems for optimizaion algorithms

Test function suites module

Test problems with a common problem interface (CPI).

Example - adding a random delay to a function evaluation (for benchmarking) file delay_wrapper.py in folder demo/problems/

# Delaying the evaluation of a test function. 

from pyopus.optimizer.base import RandomDelay
from pyopus.problems.mgh import Rosenbrock
from platform import system
if system()=='Windows':
	# clock() is the most precise timer in Windows
	from time import clock as timer
else:
	# time() is the most precise timer in Linux
	from time import time as timer

def myfunc(x):
	return 2.0*x
	
if __name__=='__main__':
	callf=myfunc
	xini=10.0
	
	# Delay is chonen randomly from [1.0, 2.0] with uniform distribution. 
	delayedCallable=RandomDelay(callf, [1.0, 2.0])
	
	print("Starting evaluation of myfunc() without delay.")
	t1=timer()
	f=callf(xini)
	dt=timer()-t1
	print("Evaluation took %.3fs, f=%e." % (dt, f))
	
	print("\nStarting evaluation of myfunc() with delay.")
	t1=timer()
	f=delayedCallable(xini)
	dt=timer()-t1
	print("Evaluation took %.3fs, f=%e." % (dt, f))
	
	# Calling a test suite function evaluates both f and g. To delay
	# the call to f() or g() method, use wrapper objects. 
	rosenbrockObject=Rosenbrock()
	xini=rosenbrockObject.initial
	# Delaying function evaluation
	delayedF=RandomDelay(rosenbrockObject.cpi()['f'], [1.0, 2.0])
	print("\n\nStarting Rosenbrock f evaluation with delay.")
	t1=timer()
	f=delayedF(xini)
	dt=timer()-t1
	print("Evaluation took %.3fs, f=%e" % (dt, f))
	
	# Delaying gradient evaluation
	delayedG=RandomDelay(rosenbrockObject.cpi()['g'], [1.0, 2.0])
	print("\nStarting Rosenbrock g evaluation with delay.")
	t1=timer()
	g=delayedG(xini)
	dt=timer()-t1
	print("Evaluation took %.3fs, g=%s" % (dt, str(g)))