0

How do I solve this error?

TypeError: NumPy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.

I have programmed an optimizing program that must minimize the cost of a wall design. The wall is based on 3 parameters, x, k and m. There are constraints to the sizes of x, k and m as shown. Another constraint is that z (or deflection) must be kept under 100mm. The equation for deflection changes based on a certain t (or time) at which the blast wall is experiencing the blast. If t is below a certain time value which is calculated dependent on, x, k and m the equation is as shown. If t is above the same certain time value, the equation for z changes.

Here is the programming... Please help many thanks :)

import numpy as np
from numpy import linspace
from math import cos
from math import sin
from scipy.optimize import minimize


#Function for minimising 
def calcCost(c):
    k = c[0]
    m = c[1]
    x = c[2]
    Cost = (900 + 825*k**2 - 1725) + (10*m - 200) + ((2400*x**2)/4)
    return Cost


#Objective function
def objective(c):
    return calcCost(c)

#Defining Variables
def calck(c):
    k = c[0]
    k=k
    k.resize(12,)
    return k
def calcm(c):
    m = c[1]
    m=m
    m.resize(12,)
    return m
def calcx(c):
    x = c[2]
    x=x
    x.resize(12,)
    return x
def calcz(c):
    k = c[0]
    x = c[1]
    m = c[2]
    l = linspace(0,140,141)
    for t in l:
        if t <= ((20 - 0.12*x**2 + 4.2*x)/1000):
            deflection = ((((1000+9*x**2-183*x)*1000)/k)*(1-cos(t*((k/m)**0.5))) + (((1000+9*x**2-183*x)*1000)/k*((20 - 0.12*x**2 + 4.2*x)/1000))*((sin(t*((k/m)**0.5))/((k/m)**0.5))-t))*1000
        else: 
            deflection = ((((1000+9*x**2-183*x)*1000)/(k*((k/m)**0.5)*((20 - 0.12*x**2 + 4.2*x)/1000)))*(sin(((k/m)**0.5)*t))-(sin(((k/m)**0.5)*(t-((20 - 0.12*x**2 + 4.2*x)/1000))))-(((1000+9*x**2-183*x)*1000)/k)*cos(((k/m)**0.5)*t))*1000
    deflection.resize(12,)
    return deflection
    
#Constraint functions
def kconstraint1(c):
    k = c[0]
    return k-(1*10**6) >= 0
def kconstraint2(c):
    k = c[0]
    return k-(7*10**6) <= 0
def mconstraint1(c):
    m = c[0]
    return m-200 >= 0
def mconstraint2(c):
    m = c[0]
    return  m-1200 <= 0
def xconstraint1(c):
    x = c[0]
    return x >= 0
def xconstraint2(c):
    x = c[0]
    return x <= 10
def zconstraint1(c):
    k = c[0]
    x = c[1]
    m = c[2]
    l = linspace(0,140,141)
    
    for t in l:
        
        if t <= ((20 - 0.12*x**2 + 4.2*x)/1000):
            deflection = ((((1000+9*x**2-183*x)*1000)/k)*(1-cos(t*((k/m)**0.5))) + (((1000+9*x**2-183*x)*1000)/k*((20 - 0.12*x**2 + 4.2*x)/1000))*((sin(t*((k/m)**0.5))/((k/m)**0.5))-t))*1000
        else: 
            deflection = ((((1000+9*x**2-183*x)*1000)/(k*((k/m)**0.5)*((20 - 0.12*x**2 + 4.2*x)/1000)))*(sin(((k/m)**0.5)*t))-(sin(((k/m)**0.5)*(t-((20 - 0.12*x**2 + 4.2*x)/1000))))-(((1000+9*x**2-183*x)*1000)/k)*cos(((k/m)**0.5)*t))*1000

    return deflection <= 99.99999999          

b = (0.5,1)
be = (0.5,10)
bb = (0.1,2.0)
bnds = (b,be,bb,bb)

con1 = ({'type':'ineq','fun':kconstraint1})
con2 = ({'type':'ineq','fun':kconstraint2})
con3 = ({'type':'ineq','fun':mconstraint1})
con4 = ({'type':'ineq','fun':mconstraint2})
con5 = ({'type':'ineq','fun':xconstraint1})
con6 = ({'type':'ineq','fun':xconstraint2})
con7 = ({'type':'ineq','fun':zconstraint1})

cons = [con1,con2,con3,con4,con5,con6,con7]

xGUESS = 5
kGUESS = 3*10**6
mGUESS = 700
zGUESS = 90


x0 = np.array([xGUESS,kGUESS,mGUESS,zGUESS])

sol = minimize(objective,x0,method='SLSQP',bounds=bnds,constraints=cons,options={'disp':True})
xOpt = sol.x
CostOPT = sol.fun
kOPT = calck(xOpt)
xOPT = calcx(xOpt)
mOPT = calcm(xOpt)
zOPT = calcz(xOpt)

print(str(CostOPT))
print(str(calcx))
print(str(calcm))
print(str(calck))
print(str(calcz))
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • 1
    What line is the error occurring on? Will you please add the full traceback? –  Dec 20 '21 at 01:39
  • First step is to identify exactly where the error occurs. By matching the error with the code we might be able to find the spot (looking for `-` with something that might be a boolean), but it would be a lot easier if you told us. Then identify the variables and their type within the problem expression. When asking a question, try to put yourself in our shoes - imagine what we know or can know, and what added information you have. – hpaulj Dec 20 '21 at 04:12
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Dec 28 '21 at 04:20

0 Answers0