A simple python implementation of Torczon algorithm
Torczon algorithm is a derivative free algorithm initially designed for unconstrained nonlinear optimization problems. Torczon algorithm is a version of the simplex algorithm (not that used in Linear Programming problems) which avoids the Collapse of the polygone of solutions that is iteratively updated through the iterations.
This is a modified version of the Torczon algorithm that incorporates:
- Explicitly handling of hard box constraints on the decision variables
- Penalty-based handling of other (non box-like) constraints
- mutiple initial guess option for global optimization.
The main appealing feature of this family of algorithm lies in the fact that the cost function and the constraints need not to be differentiable. The counterpart is that this algorithm is not fitted to very high dimensional problems.
I personally use it intensively in solving real-life problems arising in Parameterized Nonlinear Model Predictive Control problems.
Installation via pip
pip install torczon
Code citation
Required packages
- numpy
Usage
- Define the cost function to be optimized, the cost might embed soft constraint definition via constraint penalty (the penalty can be a part of the variable p that can be any python object or variable.
def f(x,p):
return ..
- Call the solver using
solve(f_user=f,
par=p, x0=x0,
xmin=xmin,
xmax=xmax,
Nguess=Nguess,
Niter=Niter,
initial_box_width=0.1)
where
-
x0the initial guess (for the first guess) -
xmin, xmaxthe box of admissible values -
Niterthe number of iterations by single guess -
Nguess: the number of initial guesses (randomly sampled using uniform distribution inside the hypercube defined by xmin and xmax) -
initial_box_widththe amplitude of the initial steps around each initial guess to bild the polygone of the torczon algorithm.
Example
The script below calls the solver for three different optimization problems.
from torczon import solve
import numpy as np
# This is the test file for the torczon module.
# it contains three constrained optimization examples.
# Example 1
#=============
# Assume a cost function that need some class instance to be defined
class Data():
def __init__(self):
self.a = 10
self.b = -1
self.c = 1.0 # assign to 0 to put solution at (a,b)
p = Data()
# The cost function
def f1dex(x, p):
return (x[0]-p.a)**2 +p.c*(x[1]*x[0])**4+(x[1]-p.b)**2
Nguess = 5
Niter = 30
R = solve(f1dex, p, [0.0,0.0], [-10,-10], [10, 10], Nguess, Niter)
print("Example 1")
print("----------")
print(f"Best found Solution = {R.x}")
print(f"Best Value found = {R.f}")
print("------------------------------")
#----------------
# Example 2
#============
# from http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/Hedar_files/TestGO_files/Page3389.htm
def f2dex(x, constraint_penalty):
c1 = (np.sin(2*np.pi*x[0]))**3
c2 = np.sin(2*np.pi*x[1])
c3 = (x[1]**3)*(x[0]+x[1])
try:
cost = c1*c2/c3
except:
cost = 1e19
g1 = x[0]**2-x[1]+1
g2 = 1-x[0]+(x[1]-4)**2
constraint = np.max(np.array([0,np.array([g1,g2]).max()]))
return cost + constraint_penalty*constraint
Nguess = 10
Niter = 50
rho = 1e6
x0 = [0.1,0.1]
dx = 1.0
R = solve(f2dex, rho, x0, [0.001,0.001], [10, 10], Nguess, Niter, initial_box_width=dx)
print("Example 2")
print("----------")
print(f"Best found Solution = {R.x}")
print(f"Best Value found = {R.f}")
print("------------------------------")
# Example 3
#============
# from http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/Hedar_files/TestGO_files/Page2031.htm
def f3dex(x, constraint_penalty):
c1 = (x[0]-10)**2+5*(x[1]-12)**2
c2 = x[2]**4+3*(x[3]-11)**2+10*x[4]**6
c3 = 7*x[5]**2+x[6]**4-4*x[5]*x[6]-10*x[5]-8*x[6]
cost = c1+c2+c3
v1 = 2*x[0]**2
v2 = x[1]**2
g = np.zeros(4)
g[0] = v1+3*v2**2+x[2]+4*x[3]**2+5*x[4]-127
g[1] = 7*x[0]+3*x[1]+10*x[2]**2+x[3]-x[4]-282
g[2] = 23*x[0]+v2+6*x[5]**2-8*x[6]-196
g[3] = 2*v1+v2-3*x[0]*x[1]+2*x[2]**2+5*x[5]-11*x[6]
constraint = np.max(np.array([0, g.max()]))
return cost + constraint_penalty*constraint
Nguess = 5
Niter = 60
rho = 1e6
x0 = np.zeros(7)
xmin = np.asarray([-10]*7)
xmax = np.asarray([+10]*7)
R = solve(f3dex, rho, x0, xmin, xmax, Nguess, Niter)
print("Example 3")
print("----------")
print(f"Best found Solution = {R.x}")
print(f"Best Value found = {R.f}")
print("------------------------------")
Metadata
Release files for torczon 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torczon-0.0.1.tar.gz | 4.7 kB | Without provenance | Details for torczon-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torczon-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for torczon-0.0.1-py3-none-any.whl |
Total release size: 9.6 kB
Release files / torczon-0.0.1.tar.gz
| Download URL | torczon-0.0.1.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
77df96d845f6558397aff315d3bc9735d1d771f6bf46e4995405b9b852589f3b
|
|
BLAKE2b-256 checksum How to use checksums |
81b8953d8a8d9bb5376e9dec9148ec7b95879eee097b279e02aba4b6e6047c17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.7
|
Release files / torczon-0.0.1-py3-none-any.whl
| Download URL | torczon-0.0.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
950459c163f949e23dab8a789abd76ad59b69b6649759c2d09feebf6287040c4
|
|
BLAKE2b-256 checksum How to use checksums |
c57a2398ecc5d3a4a4614537631bc516ca6779d1d590c40ab850cc9cebf7e743
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.7
|
Security for torczon 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.