8000 Requesting some help to setup a complicated constraint · lmfit · Discussion #998 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Requesting some help to setup a complicated constraint #998

Unanswered
JulianoGianlupi asked this question in Q&A
Discussion options

You must be logged in to vote

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@newville
Comment options

@JulianoGianlupi
Comment options

@newville
Comment options

@JulianoGianlupi
Comment options

I understood that you have questions and I have been trying to honestly answer them. Let me try to put it this way:

  • Forget the equation in my initial post
  • Can this constraint definition defined for SciPy's minimize
x = np.linspace(0,1,100)

y = 0.5 * x
y = rng.normal(y, .01) 

# print(y)

def constraint_fun(params):
    f = (0.1*x[0] + 10 * x[1]+ 5 * x[2])
    return 1 - f # ie, 0.1*x[0] + 10 * x[1] + 5 * x[2] < 1

def error(params, x, yobs):
    y = (params[0] + params[1] + params[2]) * x
    return ((y - yobs)**2).sum()

l_error = lambda p: error(p, x, y)
p0=[9, .9, 1/5]
constraints = {'type': 'ineq', 'fun': constraint_fun}
res = minimize(l_error, p0, 
               constraints=constraints, 
               bounds=((0, None),(0, None),(0, None),))
print(res)
print(res.x.sum())

be translated into lmfit? If yes how? If no why?

I have read your docs and couldn't figure out how. Your example for inequality are for only 2 variables and it is not clear how to expand it into more. As mentioned before I tried

import lmfit as lf
chem_names = ["A", "B", "C"]
max_concs = [4,5,6]
parameters =  lf.Parameters()
parameters.add(name="seed_frac", value=1, min=0, max=1)

complete_seed_frac_expr_str = "seed_frac-base_n"
parameters.add(name="base_n", value=1, min=0) 
for i, chem_name in enumerate(chem_names):
   complete_seed_frac_expr_str += f"-beta_{chem_name}_n * {max_concs[i]}"
   parameters.add(name=f"beta_{chem_name}_n", value=0, min=0) 
print(complete_seed_frac_expr_str)
parameters["base_n"].expr= complete_seed_frac_expr_str.replace("-base_n", "")

for chem_name in chem_names:
   parameters[f"beta_{chem_name}_n"].expr= complete_seed_frac_expr_str.replace(f"-beta_{chem_name}_n", "")
parameters.pretty_print()

I.e.,

base_n + beta_A_n * 4 + beta_B_n * 5 + beta_C_n * 6 < seed_frac

But that runs into a recursion error when trying to minimize

@newville
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
0