I want to calculate (π/4)^2-sin(π/4) in python:
import math
a=((math.pi)^2/16)-math.sin(math.pi/4)
print(a)
It gives the error:
TypeError: unsupported operand type(s) for ^: 'float' and 'float'
I also tried using
a=float(((math.pi)^2/16)-math.sin(math.pi/4))
But still doesn't work
^ is not supported in python. Instead, you can use math.pow.
import math
a=(math.pow((math.pi),2)/16.0)-math.sin(math.pi/4)
print(a)
^ is not supported in python. Instead, you can use math.pow or **
math.pow(math.pi,2)
math.pi ** 2
No sense in evaluating pi/4 twice.
import math
pi4 = math.pi/4
a = pi4**2 - math.sin(pi4)
Related
I have the following code:
from scipy.interpolate import UnivariateSpline
from scipy.optimize import fmin
a = UnivariateSpline(b,c) #Python
d = fmins(#(t) -ppval(a,t), someexpression); #Matlab where fmin is equivalent to fmins
How translate it to Python3?
#(t) -ppval(a,t) in Matlab is an anonymous function
You can denote things similarly using a lambda function in python.
By teh example here I see that the output of the UnivariateSpline
is callable, then the python analogous is lambda t: -a(t).
But you will have more problem fmins is not define then you may want to check alternatives in scipy.optimize package.
The documentation of fmin tells us that its first argument must be the function, the second argument the initial value, so it's exactly the same as in MATLAB. In that case you should be able to call
d = fmin(lambda x: -a(x), someexpression)
I have the following piece of code in Python in order to add a new constraint:
for o in range(num_origins):
for d in range(num_destinations):
for i in range(num_launchpads):
for j in range(num_launchpads):
model.addConstr(ground_time_min[o][d] >= ground_time_min[o][i] + (Y[o][d][i][j] - 1) * M)
Only Y[o][d][i][j] is a variable. ground_time_min is a parameter and M is a big number, also a parameter.
When I run this, I get the following error:
TypeError: unsupported operand type(s) for -: 'bool' and 'NoneType'
I know:
The types are neither bool or None. So, That is not the problem.
ground_time_min on the left-hand side is causing the problem.
I managed to fix the problem using either of these two approaches:
Moved the left-hand side to right:
model.addConstr(0 >= ground_time_min[o][i] - ground_time_min[o][d] + (Y[o][d][i][j] - 1) * M)
Used another syntax:
model.addConstr(ground_time_min[o][d], GRB.GREATER_EQUAL, ground_time_min[o][i] + (Y[o][d][i][j] - 1) * M)
The documentation says:
Left-hand side for the new constraint. Can be a constant, a Var, a LinExpr, a QuadExpr, or a TempConstr.
So, I am not sure what is wrong with the constraint as formulated initially. Does anyone have any idea what is going on here?
Thanks.
You must define Gurobi Variables for all of your objective function coefficients. At least ground_time_min[o][d] must be Gurobi variable.
You should first define variables on gurobi as mentioned in Gurobi Help, then use those variables with the second syntax you used above.
You may check out an implementation exmaple on C++ Integer Programming Example
I have the following formulation that I need to implement using Gurobi for optimization: See here for formula
And here is the code using the Gurobi Python API:
from gurobipy import *
m=Model()
# Create variables
alphak = m.addVars(S, B, b, a, vtype=GRB.BINARY,name="alphak")
# Set objective
obj1=quicksum(quicksum(quicksum(int((quicksum(alphak[s,B0,f,t]*alphak[s,k,f,t] for k in B0)/len(B0)) for t in range(a)) for f in range(b)) for s in S)
m.setObjective(obj1, GRB.MAXIMIZE)
# Optimize model
m.optimize()
The problem arises in the application of int() on the quicksum. This is the error:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'gurobipy.QuadExpr
Same thing of the division.
Anyone who could help me resolve this?
Thank you,
Gurobi does not understand int(), ceiling() or floor() functions. However, you can model your way around this quite easily.
Say we want to model y = floor(x). Then you can write:
y <= x
y >= x - 0.999999
y integer
I would like to create a theano.tensor.ivector variable and specify its values. In most code examples on the internet, I find v = T.ivector(). This creates the tensor variable but don't specify its value.
I tried this :
import theano.tensor as T
val = [1,5]
v = T.ivector(value=val, name='v')
but I get the following error :
File "<stdin>", line 1, in <module>
TypeError: __call__() got an unexpected keyword argument 'value'
I think you may be a little confused about the use of tensors, as it isn't a traditional variable that you assign a value to on declaration. A tensor is really a placeholder variable with a specified format that you will use in a function later. Extending on your example:
import theano.tensor as T
from theano import function
val = [1, 5]
v = T.ivector('v')
f = function([v], [v]) # Create a function that just returns the input
# Evaluate the function
f(val)
In the above code we just create a function that takes the tensor v and returns it. The value is not assigned until we call the function f(val)
You may find the baby steps page of the documentation helpful
Considering the following program:
from decimal import Decimal
from typing import Union
def foo(bar: Union[Decimal, int]):
print(Decimal(1) + bar)
print(bar + Decimal(1))
Why does mypy complain in the second print()?
$ mypy foo.py
foo.py: note: In function "foo":
foo.py:6: error: Unsupported operand types for + ("Union[Decimal, int]" and "Decimal")
I'm using mypy 0.3.1 on Python 3.5.1, Ubuntu 16.04.
EDIT: This seems to be a bug in mypy.