Functions in GAMS - optimization

In GAMS, I need to put an equation in a function. Look at this example
$ontext
minimize 5x^2+3x-12
subject to
4x+2x^2<10
$offtext
variable OF, x;
equation obj,cons;
obj .. OF=E=5*x*x+3*x-12;
cons .. 4*x+2*x*x=L=10;
model this /all/;
solve this using NLP maximizing OF;
display x.l,OF.l,cons.l;
The solution is
VARIABLE x.L = 1.449
VARIABLE OF.L = 2.854
EQUATION cons.L = 10.000
Now, let say I want to define the objective as a function:
f(x)=5x^2+3x-12
and replace the first constraint with
obj .. OF=E=f(x);
How should I do this?
Thank you!

I am not sure, if I understand your motivation to do this, but what you could do, is using a macro like this:
variable OF, x;
equation obj,cons;
$macro f(x) 5*x*x+3*x-12
obj .. OF=E=f(x);
cons .. 4*x+2*x*x=L=10;
model this /all/;
solve this using NLP maximizing OF;
display x.l,OF.l,cons.l;

Related

How to use PySCIPOpt for feasibility-only problem

I have used CVXPY and some of its LP solvers to determine whether a solution to an A*x <= b problem is feasible, and now I would like to try PySCIPOpt. I could not find an example of this in the docs, and I'm having trouble figuring out the right syntax. With CVXPY the code is simply:
def do_cvxpy(A, b, solver):
x = cvxpy.Variable(A.shape[1])
constraints = [A#x <= b] #The # denotes matrix multiplication in CVXPY
obj = cvxpy.Minimize(0)
prob = cvxpy.Problem(obj, constraints)
prob.solve(solver=solver)
return prob.status
I think with PySCIPOpt one cannot use matrix notation as above, but must treat vectors and matrices as collections of scalar variables, each of which has to be added individually, so I tried this:
def do_scip(A, b):
model = Model("XYZ")
x = {}
for i in range(A.shape[1]):
x[i] = model.addVar(vtype="C", name="x(%s)" % i)
model.setObjective(0) #Is this right for a feasibility-only problem?
model.addCons(A*x <= b) #This is certainly the wrong syntax
model.optimize()
return model.getStatus()
Could anyone please help me out with the correct form for the constraint in addCons() for this kind of problem, and confirm that an acceptable way to ask whether a solution is feasible is to simply pass 0 as the objective?
I'm still not positive about the setObjective(0), but at least I can get the code to run without errors by "unpacking" the A matrix and the b vector and adding each element as a constraint:
for i in range(ncols):
for j in range(nrows):
model.addCons(A[j,i]*x[i] <= b[i])
I also discovered that CVXPY actually has an interface to SCIP, but it gives me an error when I try to use it:
getSolObjVal cannot only be called in stage SOLVING without a valid solution
which seems to suggest that the interface cannot be used for feasibility-only problems.

Passing a variable as a parameter for a new model in GAMS

I'm writing a program where I have two mathematical models that are solved sequentially in a way that the variable X7(f,p) from the first model becomes the parameter rwdemand(f,p) to the second.
Main code elements for the issue described above:
Sets
f raw materials /f1*f14/
p periods /p1*p4/;
Positive Variable X7(f,p) quantity of raw material f required in period p;
Equation
*First model
r6_rwinventory(f,p).. X4(f,p-1) + X7(f,p-leadtime)=e= sum((t,m,sp),((rmconsumption(f,t,m)*X1(t,m,sp))+X4(f,p)));
Parameter rmdemand(f,p);
rmdemand(f,p)= X7.l;
Equation
*Second model
r3_demand(f,p).. X4(f,p-1) + sum((s,d),X2(f,s,d,p-leadtime)) =e= rmdemand(f,p) + X4(f,p);
Model First_model "real instance set for Lot sizing model (SMM-LS)." /fo,r1_produnits,r2_packsetup,r3_bulkinventory,r4_packinventory,r5_maxbulkinventory,r6_rwinventory,r7_usedinventory1/
Second_model "real instance set for Raw material purchasing model (SMM-RMP)" /fo2,r1_maxd,r2_order,r3_demand,r4_maxinventory,r5_mininventory/;
Solve First_model using mip minimizing Z
Solve Second_model using mip minimizing A;
Display Z.l,A.l;
Writing this way:
Parameter rmdemand(f,p);
rmdemand(f,p)= X7.l;
It doesn't work and appears the error 141: Symbol declared but no values have been assigned. Check for missing data definition, assignment, data loading or implicit assignment via a solve statement.
How can I fix that?
Regards!
Ana! You can only use X7.l to obtain a value after you have solved the first model. So, I believe this might work:
(...)
Equation
*First model
r6_rwinventory(f,p).. X4(f,p-1) + X7(f,p-leadtime)=e= sum((t,m,sp),((rmconsumption(f,t,m)*X1(t,m,sp))+X4(f,p)));
Parameter rmdemand(f,p);
Equation
*Second model
r3_demand(f,p).. X4(f,p-1) + sum((s,d),X2(f,s,d,p-leadtime)) =e= rmdemand(f,p) + X4(f,p);
Model First_model "real instance set for Lot sizing model (SMM-LS)." /fo,r1_produnits,r2_packsetup,r3_bulkinventory,r4_packinventory,r5_maxbulkinventory,r6_rwinventory,r7_usedinventory1/
Second_model "real instance set for Raw material purchasing model (SMM-RMP)" /fo2,r1_maxd,r2_order,r3_demand,r4_maxinventory,r5_mininventory/;
Solve First_model using mip minimizing Z;
*Insert the attribution of value between the two solve statements
rmdemand(f,p)= X7.l;
Solve Second_model using mip minimizing A;
Display Z.l,A.l;
Hope it works :)

tf.function property in pytorch

I'm a beginner in pytorch, and I have some functions that are needed to implement in network.
My question is: is there any way like tf.function, or should I use "class(nn.Module)" with variable?
For example, let X be a 10x2 matrix . In pseudo-code:
a = Variable(1.0)
b = Variable(1.0)
Y = a*X[:,0]**2 + b*X[:,1]
In PyTorch you don't need things like tf.function, you just use normal Python code (because of the dynamic graph).
Please give more detailed example (with code) of what you're trying to do if the above doesn't answer your question.

Maximizing Likelihood, Julia

I have a log-likelihood function and I want to maximize it in respect to theta (N), and it is defined as:
function loglik(theta,n,r)
N=theta;k=length(n);
ar1=float(lgamma(N+1));ar2=sum(n)*log(sum(n)/(k*N));ar3=(k*N-sum(n))*log(1-(sum(n))/(k*N));
par=float(lgamma((N-r)+1));
return(-(ar1+ar2+ar3-par)) end
The I use Optim.jl's optimize function as:
r=optimize(b->loglik(b,nn, 962), 978, BFGS() );
Where nn is an array. And I get this error:
ERROR:MethodError no method matching optimize (::#46#47,::Float64, ::Optim.BFGS)
Can anyone help?
You're almost there! You need to initialize it with an array.
optimize(b->loglik(first(b),nn,962), [978.,], BFGS())
(though you still need to provide us with nn for this answer to show the output)
edit: since b is a scalar in loglik, I changed it to b->loglik(first(b),nn, 962) as suggested by Chris Rackauckas below.

Is the equation below means in a MIP model

As the title mentioned
sum((r,l), Mer.l(e,r) * Mel(e,l)) =e= 0;
I use a GAMS mip model to solve a problem, and Mer(e,r) and Mel(e,l) are both binary variables, and if I did not write the .l suffix, the compile will give Endogenous relational operations require model type "dnlp"" error message, but I am not sure the equation above staying the original meaning that is the Mer(e,r) still a variable? and is it still changeable with the mip solving process?
If you use Mer.l, the model will not use Mer as a variable that gets optimized anymore, but will use the (initial) levels of the variables Mer as constant numbers. But you could reformualte your equation, so that it stays linear. As I understand it, you want to make sure that for each e and each combination of r and l you will never get Mer=1 and Mel=1 (one could be 1 or both should be 0). So you could formulate is as e.g.:
equation e(e,r,l);
e(e,r,l).. Mer(e,r) + Mel(e,l) =l= 1;
I hope that helps,
Lutz