Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14126

How can I define multiple combinations of Non Linear Constraints using SciPy?

$
0
0

I am trying to solve a minimisation problem using the SciPy Differential Evolution algorithm. The function to minimise is: f(a, b, c) = a*sin(c*sin(b)). But the problem is not that simple: the parameters a, b, c are dependent on two input values (in1, in2) as well as other coefficients (x1, x2, x3), and they can be thought as independent functions: a = in1 * (in1 * x1 + x2), b = in2 * x2, c = x0. So, the objective function has (in1, in2) as inputs and (x1, x2, x3) are the coefficients to be determined.

The problem is that I want to obtain the coefficients that are suitable for a given set of input values (eg. in1 = [2, 4, 6, 8], in2 = [0, 2, 4]), but in order to do that I have to define some non linear constraints for the parameters a, b, c.

It is pretty straightforward to define non linear constraints for a given function, but I don't know how to do it when the constraints are dependent on the input variables (in1, in2). So, if I have 4 in1 values and 3 in2 values, I should define 12 different combinations of constraints. I tried to define Non Linear Constraints by using NumPy arrays or Python lists, but that doesn't seem to work. I have attached below my code so you can have a better understanding of my problem. Thank you in advance.

import numpy as npfrom scipy.optimize import differential_evolution as diff_evolfrom scipy.optimize import NonlinearConstraint# Function Inputs (in1, in2)in1, in2 = np.array([2, 4, 6, 8]), np.array([0, 2, 4])# Function to minimise: y(in1, in2) = a*sin(c*sin(b))def objFunction(x):    a = in1 * (in1 * x[1] + x[2])    b = in2 * x[2]    c = x[0]    return a*np.sin(c*np.sin(b))# Bounds for function coefficients (x0, x1, x2)bounds = [(1.00, 3.00),     # x0          (-50, 0.0),       # x1          (0, 2500)]        # x2# Define Non Linear Constraints for parameters a, b, ca = lambda x: in1 * (in1*x[1] + x[2])b = lambda x: in2 * x[2]c = lambda x: x[0]# Lower limit on constr_funlb1 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]lb2 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]lb3 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]# Upper limit on constr_funub1 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]ub2 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]ub3 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]nlc1 = NonlinearConstraint(a, lb1, ub1)nlc2 = NonlinearConstraint(b, lb2, ub2)nlc3 = NonlinearConstraint(c, lb3, ub3)# Run Differential Evolution Algorithmresult = diff_evol(objFunction, bounds, constraints=(nlc1, nlc2, nlc3))

Viewing all articles
Browse latest Browse all 14126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>