I wondered if someone could help me make this GAMS model to a AMPL model. I am trying to understand the language.
Before hand thanks! You can see the model below.
GAMS Model
set activity / A*G/;
alias (activity,i,j);
set prec(i,j) /
A.(B,C), (B,E).F, C.D, D.E, F.G /;
parameter duration(activity) / A 2, B 3, C 3, D 4, E 8, F 6, G 2 /;
free variable time;
nonnegative variable s(i);
equations ctime(i)
ptime(i,j) ;
ctime(i).. time =g= s(i) + duration(i);
ptime(prec(i,j)).. s(i) + duration(i) =l= s(j);
model schedule /all/;
solve schedule using lp minimizing time;
display time.l, s.l;
GAMS convert function with the option Ampl allows you to generate AMPL input file (*.mod) from a GAMS model file.
Related
I am trying to formulate and solve an optimization problem based on an article. The authors introduced 2 decision variables. Power of station i at time t, P_i,t, and a binary variable X_i,n which is 1 if vehicle n is assigned to station i.
They introduced some other variables, called utility variables. For instance, energy delivered from station i up to time t for vehicle n, E_i,t,n which is calculated based on primary decision variables and a few fix parameters.
My question is should I define the utility variables as Gekko variables? If yes, which type is more appropriate?
I = 4 # number of stations
T = 24 # hours of simulation
N = 5 # number of vehicles
p = m.Array(m.Var,(I,T),lb=0,ub= params.ev.max_power)
x = m.Array(m.Var,(I,N),lb=0,ub=1, integer = True)
Should I define E as follow to solve these equations as an example? This introduces extra variables that are not primary decision variables and are calculated based on other terms that depend on the primary decision variable.
E = m.Array(m.Var,(I,T,N),lb=0)
for i in range(I):
for n in range(N):
for t in range(T):
m.Equation(E[i][t][n] >= np.sum(0.25 * availability[n, :t] * p[i,:t]) - (M * (1 - x[i][n])))
m.Equation(E[i][t][n] <= np.sum(0.25 * availability[n, :t] * p[i,:t]) + (M * (1 - x[i][n])))
m.Equation(E[i][t][n] <= M * x[i][n])
m.Equation(E[i][t][n] >= -M * x[i][n])
All of those variable definitions and equations look correct. Here are a few suggestions:
There is no availability[] variable defined yet. If availability is a function of other decision variables, then it is generally more efficient to use an m.Intermediate() definition to define it.
As the total number of total decision variables increase, there is often a large increase in computational time. I recommend starting with a small problem initially and then scale-up to the larger sized problem.
Try the gekko m.sum() instead of sum or np.sum() for potentially more efficient calculations. Using m.sum() does increase the model compile time but generally decreases the optimization solve time, so it is a trade-off.
Or is there another problem?
It is my first time trying AMPL. I am trying to solve a nonlinear dynamic program. Here are parts of the first draft:
#defining degree
param degree >= 0 integer; # degree of polynomial approximation
param degrees {alpha in 0..degree}; # for storing degrees
...
# defining variables for optimization
var b {alpha in 0..degree}; # coefficient of polynomial
...
# defining constraints
subject to Upper_bound {i in M}: v[i] <=
sum {k in 0..n} (comb[n,k]*(p^k)*((1-p)^(n-k))*((k/n)*((ch[k,i])^d)/(1-d)) +
((n-k)/n)*(cl[k,i]^d)/(1-d)+(1/(1+r))*(sum {j in 0..degree}
(b[j]*cos(degrees[j]*acos((2*Anew[i]-Amax)/(Amax)))))));
I get a syntax error when I pass the mod file:
model DP.mod;
DP.mod, line 41 (offset 1658):
syntax error
context: >>> (b[j]*cos(degrees[j]*acos((2*Anew[i]-Amax)/(Amax))))))) <<< ;
Please help.
It has been solved, thanks! There was an extra ending bracket.
While trying to solve a circuit equation, I stumbled onto a type of Lienard Equation. But, I am unable to solve this analytically.
x'' + a(x-1)x' + x = V ------------------------- (1)
where dash(') represent differentiation w.r.t time(t).
The following substitution y =x-V and w(y) = y', it gets converted into first order equation
w*w' + a(y+V-1)w + y = 0; ---------------------- (2)
here dash(') represent differentiation w.r.t y.
if I substitute z = (int)(-a*(y+V-1), (int) represent integration. The equation gets converted into Abel equation of second kind.
w*w' - w = f(z). -------------------- (3)
differentiation w.r.t z.
it get complicated and complicated.
Can someone help in solving the equation (1). in some other method or proceeding with the method, I started. ANy help will be extremely appreciated
Is there any command that can solve multi objective model directly?
I mean, without using weighted sum or epsilon constraint methods, can we solve multi objective model in gams?
Many thanks!
This is the epsilon constraint model in GAMS which is applicable in solving bi-objective optimization problems and finding the pareto optimal front.
$title Pareto optimal front determination
$onText
For more details please refer to Chapter 2 (Gcode2.16), of the following book:
Soroudi, Alireza. Power System Optimization Modeling in GAMS. Springer, 2017.
--------------------------------------------------------------------------------
Model type: NLP
--------------------------------------------------------------------------------
Contributed by
Dr. Alireza Soroudi
IEEE Senior Member
email: alireza.soroudi#gmail.com
We do request that publications derived from the use of the developed GAMS code
explicitly acknowledge that fact by citing
Soroudi, Alireza. Power System Optimization Modeling in GAMS. Springer, 2017.
DOI: doi.org/10.1007/978-3-319-62350-4
$offText
Variable of1, of2, x1, x2;
Equation eq1, eq2, eq3, eq4;
eq1.. 4*x1 - 0.5*sqr(x2) =e= of1;
eq2.. -sqr(x1) + 5*x2 =e= of2;
eq3.. 2*x1 + 3*x2 =l= 10;
eq4.. 2*x1 - x2 =g= 0;
x1.lo = 1; x1.up = 2;
x2.lo = 1; x2.up = 3;
Model pareto1 / all /;
Set counter / c1*c21 /;
Scalar E;
Parameter report(counter,*), ranges(*);
solve pareto1 using nlp maximizing of1;
ranges('OF1max') = of1.l;
ranges('OF2min') = of2.l;
solve pareto1 using nlp maximizing of2;
ranges('OF2max') = of2.l;
ranges('OF1min') = of1.l;
loop(counter,
E = (ranges('OF2max') - ranges('OF2min'))*(ord(counter) - 1)/(card(counter) - 1) + ranges('OF2min');
of2.lo = E;
solve pareto1 using nlp maximizing of1;
report(counter,'OF1') = of1.l;
report(counter,'OF2') = of2.l;
report(counter,'E') = E;
);
display report;
I'm trying to solve a model using Julia-JuMP. The following is the outline of the model that I created. Here, z[i,j] is a binary variable and d[i,j] is the cost for which z[i,j]=1.
My constraint creates an infinite number of constraint and hence I need to use a separation algorithm to solve it.
First, I solve the model without any constraint, so the answer to all variables z[i,j] and d[i,j] are zero.
Then, I'm including the separation algorithm (which is given inside the if condition). Even though I'm including if z_value == 0, z_values are not passing to it.
Am I missing something in the format of this model?
m = Model(solver=GurobiSolver())
#variable(m, z[N,N], Bin)
#variable(m, d[N,N]>=0)
#objective(m, Min, sum{ d[i,j]*z[i,j], i in N, j in N} )
z_value = getvalue(z)
d_value = getvalue(d)
if z_value == 0
statement
elseif z_value == 1
statement
end
#constraint(m, sum{z[i,j], i in N, j in N}>=2)
solve(m)
println("Final solution: [ $(getvalue(z)), $(getvalue(d)) ]")
You're multiplying z by d which both are variables, hence your model is non-linear,
Are the costs d[i,j] constant or really a variable of the problem ?
If so you need to use a non-linear solver