
import scipy.optimize
from scipy.stats import norm

# function to minimize:
def g(xy):
    (x,y) = xy
    print("g({},{})".format(x,y))
    return x + y 

# constraints

noise_level = 0.0000005

# constraint 1: y <= x/2
def f1(xy):
    (x,y) = xy
    return x/2 - y + noise_level * norm.rvs(0)

# constraint 2: y >= 1/x
def f2(xy):
    (x,y) = xy
    return y - 1.0/x + noise_level * norm.rvs(0)

constraints = [
                { "type": "ineq",
                  "fun": f1
                },
                { "type": "ineq",
                  "fun": f2
                }
               ]

print(scipy.optimize.minimize(g, (11, 5), constraints=constraints))

                  


import scipy.optimize
from scipy.stats import norm

# function to minimize:
def g(xy):
    (x,y) = xy
    print("g({},{})".format(x,y))
    return x + y 

# constraints

noise_level = 0.05

# constraint 1: y <= x/2
def f1(xy):
    (x,y) = xy
    return x/2 - y + noise_level * norm.rvs(0)

# constraint 2: y >= 1/x
def f2(xy):
    (x,y) = xy
    return y - 1.0/x + noise_level * norm.rvs(0)

constraints = [
                { "type": "ineq",
                  "fun": f1
                },
                { "type": "ineq",
                  "fun": f2
                }
               ]

print(scipy.optimize.minimize(g,
                              (11, 5),
                              method = "COBYLA",
                              tol = 0.01,
                              constraints=constraints))

                  

Switch to COBYLA optimization method. Works much better.
Switch to COBYLA optimization method. Works much better.

  
  import scipy.optimize
  from scipy.stats import norm
  
  # function to minimize:
  def g(xy):
      (x,y) = xy
      print("g({},{})".format(x,y))
      return x + y 
  
  # constraints
  
- noise_level = 0.0000005
+ noise_level = 0.05
  
  # constraint 1: y <= x/2
  def f1(xy):
      (x,y) = xy
      return x/2 - y + noise_level * norm.rvs(0)
  
  # constraint 2: y >= 1/x
  def f2(xy):
      (x,y) = xy
      return y - 1.0/x + noise_level * norm.rvs(0)
  
  constraints = [
                  { "type": "ineq",
                    "fun": f1
                  },
                  { "type": "ineq",
                    "fun": f2
                  }
                 ]
  
- print(scipy.optimize.minimize(g, (11, 5), constraints=constraints))
+ print(scipy.optimize.minimize(g,
+                               (11, 5),
+                               method = "COBYLA",
+                               tol = 0.01,
+                               constraints=constraints))
  
                    
  
Switch to COBYLA optimization method. Works much better.
## Code Before:

import scipy.optimize
from scipy.stats import norm

# function to minimize:
def g(xy):
    (x,y) = xy
    print("g({},{})".format(x,y))
    return x + y 

# constraints

noise_level = 0.0000005

# constraint 1: y <= x/2
def f1(xy):
    (x,y) = xy
    return x/2 - y + noise_level * norm.rvs(0)

# constraint 2: y >= 1/x
def f2(xy):
    (x,y) = xy
    return y - 1.0/x + noise_level * norm.rvs(0)

constraints = [
                { "type": "ineq",
                  "fun": f1
                },
                { "type": "ineq",
                  "fun": f2
                }
               ]

print(scipy.optimize.minimize(g, (11, 5), constraints=constraints))

                  

## Instruction:
Switch to COBYLA optimization method. Works much better.
## Code After:

import scipy.optimize
from scipy.stats import norm

# function to minimize:
def g(xy):
    (x,y) = xy
    print("g({},{})".format(x,y))
    return x + y 

# constraints

noise_level = 0.05

# constraint 1: y <= x/2
def f1(xy):
    (x,y) = xy
    return x/2 - y + noise_level * norm.rvs(0)

# constraint 2: y >= 1/x
def f2(xy):
    (x,y) = xy
    return y - 1.0/x + noise_level * norm.rvs(0)

constraints = [
                { "type": "ineq",
                  "fun": f1
                },
                { "type": "ineq",
                  "fun": f2
                }
               ]

print(scipy.optimize.minimize(g,
                              (11, 5),
                              method = "COBYLA",
                              tol = 0.01,
                              constraints=constraints))

                  
