Vehicle Routing in python to solve with Gurobi - i struggle with the objective function for a three index formulation (quicksum method) - gurobi

My problem is, I recently got into python and I did a two index flow formulation already - if someone is interested in the code, just let me know.
Now i dont know how to give the quicksum method quicksum 3 variables - anyone has a clue how to code the objective function? so how to give it the i,j and the k
Just look at the objective function of the picture.
https://imgur.com/a/1W7m5Li
for example if i do it vor c[i,j] and x[i,j] it looks like this (quicksum(x[i, j]*c[i, j] for i, j in A))
Now i tried to adapt and integrated the k like this : mdl.setObjective(quicksum(c[i,j]*x[i,j,k] for i,j in A) for k in K)
I'm struggling with it for 5 days now and I am trying to solve this problem with phyton and gurobi - but it wont let me introduce 3 variables - most of the time I get a
SyntaxError: unexpected EOF while parsing error. The two index version works just fine
maybe i need to integrate the one quicksum into the for loop?

The generator expression needs to be inside the quicksum function:
mdl.setObjective(quicksum(c[i,j]*x[i,j,k] for i,j in A for k in K))

So i tried introcuing vrp related variables in the dic - also didnt work
i will just post the complete code here now - the 2 index version is working on the left side
the three index version on the right is not wokring at all
https://imgur.com/a/RbWVNDd

Related

Error when using least_squares to solve constrained nonlinear system of equations

I'm trying to solve a nonlinear system of 7 equations with least_squares, as some variables are molar fractions and go between 0 and 1 (fsolve doesnt allow constraints as far as I've read). Heres the main part of the code:
def fun_t(w,x_2):
x_1=1-x_2;
z_A=w[0];
z_B=w[1];
z_AB=w[2];
gamma_A=w[3];
gamma_B=w[4];
gamma_AB=w[5];
eps=w[6]
K_gamma=gamma_AB/(gamma_A*gamma_B)
K=K_gamma*z_AB/(z_A*z_B)
phi_A=z_A*v_A/(z_A*v_A+z_B*v_B+z_AB*v_AB);
phi_B=z_B*v_B/(z_A*v_A+z_B*v_B+z_AB*v_AB);
phi_AB=z_AB*v_AB/(z_A*v_A+z_B*v_B+z_AB*v_AB);
return [z_AB-eps/(1-eps),
z_A-(x_2-eps)/(1-eps),
z_B-(x_1-eps)/(1-eps),
eps-(1-(1-4*K/(K+K_gamma)*x_1*x_2)**0.5)/2,
R*T*np.log(gamma_A)-v_A*(alphaT_AB*phi_B**2+alphaT_AAB*phi_AB**2+(alphaT_AB+alphaT_AAB-alphaT_BAB)*phi_B*phi_AB),
R*T*np.log(gamma_B)-v_B*(alphaT_AB*phi_A**2+alphaT_BAB*phi_AB**2+(alphaT_AB+alphaT_BAB+alphaT_AAB)*phi_A*phi_AB),
R*T*np.log(gamma_AB)-v_AB*(alphaT_AAB*phi_A**2+alphaT_BAB*phi_B**2+(alphaT_AAB+alphaT_BAB-alphaT_AB)*phi_A*phi_B)
]
x_2=np.linspace(0,1,11)
guess=[0.5,0.5,0.5,1,1,1,0.25];
roots_t=np.zeros(shape=(len(x_2),len(guess)))
for i in range(len(x_2)):
roots_t[i] = least_squares(fun_t, guess, bounds = ((0,0,0,-5,-5,-5,0),(1,1,1,5,5,5,0.5)), args=x_2[i])
I get the error TypeError: fun_t() argument after * must be an iterable, not numpy.float64. My goal is to give the elements of the array x_2 as the fuction's second argument in order to get the rest of the variables for a set of compositions. I dont understand why it cant iterate over x_2, as it worked when I was using fsolve.
I'm just starting using python for this kind of calculations, so any help will be much appreciated!

How to get list of basic variables from JuMP/Gurobi?

I'm solving an LP in Julia/JuMP using Gurobi as my solver. I want to know which variables in my solution are basic. How do I obtain this information?
Just checking which variables are non-zero is not sufficient, since we might be dealing with a degenerate solution (i.e., basic variables equal to zero).
I found two similar questions online, but the solutions suggested do not seem to apply to the current version of JuMP anymore (or am I missing something?):
https://discourse.julialang.org/t/how-to-obtain-a-basic-solution/1784
https://groups.google.com/g/julia-opt/c/vJ6QuFBfPbw?pli=1
There's a Gurobi attribute called VBasis (https://www.gurobi.com/documentation/9.0/refman/vbasis.html) that seems to be what I'm looking for, but I don't know how to access it.
This is poorly documented, but you can see which constraints are basic:
model = Model(Gurobi.Optimizer)
#variable(model, x >= 0)
#constraint(model, c, 2x >= 1)
#objective(model, Min, x)
optimize!(model)
julia> MOI.get(model, MOI.ConstraintBasisStatus(), c)
NONBASIC::BasisStatusCode = 1
julia> MOI.get(model, MOI.ConstraintBasisStatus(), LowerBoundRef(x))
BASIC::BasisStatusCode = 0
Note that since variables can have lower and upper bounds, we report which constraints are basic, rather than which variables are.
Documentation:
https://jump.dev/MathOptInterface.jl/stable/apireference/#MathOptInterface.ConstraintBasisStatus
https://jump.dev/MathOptInterface.jl/stable/apireference/#MathOptInterface.BasisStatusCode

Mathematica can't solve DSolve[{f[0] ==d,f'[0]==v0,f''[t] == -g*m2/f[t]^2}, f, t]?

In[11]:= $Version
Out[11]= 9.0 for Linux x86 (32-bit) (November 20, 2012)
In[12]:= DSolve[{f[0] == d, f'[0] == v0, f''[t] == g*m2/f[t]^2}, f, t]
DSolve::bvimp: General solution contains implicit solutions. In the boundary
value problem these solutions will be ignored, so some of the solutions will
be lost.
Out[12]= {}
The code above pretty much says it all. I get the same error if I replace g*m2 with 1.
This seems like a really simple DFQ to solve. I'd like to tell DSolve to assume all variables are real and that d, g, and m2 are all greater than 0, but there's unfortunately no way to do that.
Thoughts?
You are trying for a symbolic solution. And unfortunately, symbolic integration is hard (while symbolic differentiation is easy).
The way this integration works is to obtain the energy functional by integrating once
E = 1/2*f'[t]^2 + C/f[t]
and then to isolate f'[t]. The resulting integral is not easy to solve and leads to the mentioned implicit solutions.
Did you really want to get the symbolic solution or only some function table to plot the solutions or compute other related quantities?
Since it was clarified that the requested quantity is the maximum of certain solutions: This can be computed by setting v=0 in the energy equation
C/x = E = 1/2*v0^2 + C/x0
or
x = C*x0/(C + 1/2*v0^2*x0 )
One would have to analyze the time aspect to make sure that this extremum is reached before passing again at the initial point x0.

Arrays with attributes in Julia

I am making my first steps in julia, and I would like to reproduce something I achieved with numpy.
I would like to write a new array-like type which is essentially an vector of elements of arbitrary type, and, to keep the example simple, an scalar attribute such as the sampling frequency fs.
I started with something like
type TimeSeries{T} <: DenseVector{T,}
data::Vector{T}
fs::Float64
end
Ideally, I would like:
1) all methods that take a Vector{T} as argument to take on TimeSeries{T}.
e.g.:
ts = TimeSeries([1,2,3,1,543,1,24,5], 12.01)
median(ts)
2) that indexing a TimeSeries always returns a TimeSeries:
ts[1:3]
3) built-in functions that return a Vector to return a TimeSeries:
ts * 2
ts + [1,2,3,1,543,1,24,5]
I have started by implementing size, getindex and so on, but I definitely do not see how it could be possible to match points 2 and 3.
numpy has a quite comprehensive way to doing this: http://docs.scipy.org/doc/numpy/user/basics.subclassing.html. R also seems to allow linking attributes attr()<- to arrays.
Do you have any idea about the best strategy to implement this sort of "array with attributes".
Maybe I'm not understanding, why is for say point 3 it not sufficient to do
(*)(ts::TimeSeries, n) = TimeSeries(ts.data*n, ts.fs)
(+)(ts::TimeSeries, n) = TimeSeries(ts.data+n, ts.fs)
As for point 2
Base.getindex(ts::TimeSeries, r::Range) = TimeSeries(ts.data[r], ts.fs)
Or are you asking for some easier way where you delegate all these operations to the internal vector? You can clever things like
for op in (:(+), :(*))
#eval $(op)(ts::TimeSeries, x) = TimeSeries($(op)(ts.data,x), ts.fs)
end

Issue programming a math equation in Objective-C?

I am trying to solve the following equation in my program:
7.7^2 x 0.012^2/(0.2145 x 1.67^(16/3))
That should equal : 0.002582 (this is verified w/ google & scientific calculator)
This is the code that I am using
CGFloat eX1 = pow(7.7, 2) * pow(0.012, 2)/(0.2145 * pow(1.67, (16/3)));
NSLog(#"%f",eX1);
And even though, I believe my code should give me the same results, it's actually giving me:0.002679
What am I doing wrong? What can I do to obtain the correct answer?
Change (16/3) to (16.0/3.0). Otherwise 16/3 results in 5, not 5.33333349.
And you have 7.2 instead of 7.7 at the start.