Я пытаюсь минимизировать двумерную квадратичную функцию с центром в области x1 = x2 = 1.5 с учетом ограничения c <1, где c = 0 для x1 <1 & x2 <1 и c = 1 везде, где бы то ни было.Модель ограничений предоставляется классификатором SVM.Код приведен ниже.Когда я запускаю его, я получаю сообщение об ошибке </p>
COBYLA failed to find a solution: Did not converge to a solution satisfying the constraints. See `maxcv` for magnitude of violation.
Минимальное рекомендуемое значение - [1,5,1.5].У вас есть предложение о том, что я могу сделать, чтобы заставить соблюдение ограничения?Фоном является то, что я оптимизирую аэродинамическую форму под структурными ограничениями, и они не должны нарушаться.Следовательно, формулировка проблемы аналогична приведенной ниже.
# import required libraries
import random
import matplotlib.pyplot as plt
from scipy.optimize import fmin_cobyla
from sklearn import svm
# generate grid with random positions
x1_rand=[]
x2_rand=[]
for x in range(5000):
x1_rand.append(random.uniform(-4,4))
x2_rand.append(random.uniform(-4,4))
# generate classes for the grid (classes are 0 and 1)
y_class=[]
x1_pos=[]
x2_pos=[]
x1_neg=[]
x2_neg=[]
for i in range(len(x1_rand)):
if x1_rand[i]<1 and x2_rand[i] < 1:
x1_pos.append(x1_rand[i])
x2_pos.append(x2_rand[i])
y_class.append('0')
else:
x1_neg.append(x1_rand[i])
x2_neg.append(x2_rand[i])
y_class.append('1')
# build the SVM model to separate the data
C=1.0
model = svm.SVC(kernel='rbf',gamma=0.7,C=C)
features = [x1_rand, x2_rand]
features = zip(*features)
model.fit(features,y_class)
# plot data, the classification is color coded
# red=constraint fulfilled; blue=constraint violated
plt.scatter(x1_pos,x2_pos,c='r')
plt.scatter(x1_neg,x2_neg,c='b')
# define the constraint function
def constr1(x):
return 0.5-float(model.predict([x[0],x[1]])[0])
# define the objective function
fun = lambda x: (x[0] - 1.5)**2 + (x[1] - 1.5)**2
# minimize the objective function
res = fmin_cobyla(fun,[0.5,0.5],constr1,rhoend=1e-5)
print res