I have a set j and parameter edge. I have a graph too.
Set j/1*5/;
Alias(j,jp);
Parameter edge(j,jp)
That edge(j,jp) =1 if there is arc from j to jp , and it's 0 if there isn't arc from j to jp.
I maked edge(j,jp) .
I want to define a set or parameter , for saving index of neighborhood of node "j".
I Mean , neighborhood (j)={jp : edge(j,jp)=1}
I write ,below command but I get error.
Set neighborhood (j)
Neighborhood (j)$edge (j,jp) =JP.val;
How can I obtain neighborhood of especial node?
Do you work with an directed graph and assume there is just one neighbor for each j? Then, try this:
Set j/1*3/;
Alias(j,jp);
Parameter edge(j,jp) / 1.2 1, 2.3 1, 3.1 1 /;
Parameter Neighborhood (j);
Neighborhood (j) = sum(jp$edge(j,jp), jp.val);
Otherwise: What do you expect to see in Neighborhood, if there is more than one neighbor?
Related
How do I create a set to whose elements were added the first 4 letters of another set and the first 2 letters of the elements of another set?
For example, I have 2 sets: one is composed of years and the other of quarters:
SET TY /2019`*`2040/;
`SET TQ /Q1`*`Q4/;
And I need another set that is the Cartesian product of both, but in a single value for each element:
SET T /2019Q1*2019Q4,2020Q1*2020Q4… 2040Q1*2040Q4/;
In Stata for example, I would do this:
Global year “2019 2020 2021 … 2040 “
Global quarter “Q1 Q2 Q3 Q4”
Foreach y of global year {
Foreach q of global quarter{
Global T = ‘y’ ‘q’
…
}}
How can I did this in GAMS?
If you really need this new one dimensional set, that has no logical relation to TY and TQ, you could do it like this:
SET TY /2019*2040/;
SET TQ /Q1*Q4/;
Set T(*);
$onEmbeddedCode Python:
t = []
for ty in gams.get('TY'):
for tq in gams.get('TQ'):
t.append(ty + tq)
gams.set('T',t)
$offEmbeddedCode T
display T;
However, I do not know your use case of course, but maybe something like this could make more sense for you:
SET TY /2019*2040/;
SET TQ /Q1*Q4/;
Set T(TY,TQ) /#TY.#TQ/;
display T;
I have a matrix, of dimension, i rows and j columns, a specific element of which is called x(i,j), where say i are plants, and j are markets. In standard GAMS notation:
Sets
i canning plants / seattle, san-diego /
j markets / new-york, chicago, topeka / ;
Now, I also wish to create a loop, over time- for 5 periods. Essentially, say I define
Set t time period
/period1
period2
period3
period4
period5
/ ;
Parameters
time(t)
/ period1 1,
period2 2,
period3 3,
period4 4,
period5 5
/ ;
Basically, I want to re-run this loop, which contains a bunch of other commands, but I wish to re-define this matrix from period 2 onwards, to look like this:
x("seattle",j)=x("seattle",j)+s("new-york",j)
x("new-york",j)=0'
Essentially, within the loop, I want the matrix x(i,j) to look different after period 2, wherein the column x("seattle",j) is replaced with the erstwhile x("seattle",j)+s("new-york",j) and the column x("new-york",j) is set to 0.
The loop would start like :
loop
(t,
...
Option reslim = 20000 ;
option nlp = conopt3 ;
solve example using NLP maximizing VARIABLE ;
) ;
I am not sure how to keep redefining this matrix within the loop, for each period>2.
Please note: After period 2, the matrix looks the same. The change only happens once (i.e., the matrix elements do not keep looping from the previous period, but just switch once , at the end of period 2, and then stay constant thereafter.
Any help on this is much appreciated!
You can use a $ condition to make this change in the loop for period2 only, like this:
x("seattle",j)$sameAs(t,'period2')=x("seattle",j)+s("new-york",j);
I have set i/1*7/ and parameter u(i) /1 15,2 5,3 15,4 30,5 21,6 30,7 11/
I want to find maximum element of u(i) , i use 'smax' , I write this code:
set j/1*7/;
parameter u(i) /1 15,2 5,3 15,4 30,5 21,6 30,7 11/;
scalar max_val;
max_val=smax(j,u(j));
To find the index of maximum element, I wrote this
set posmax(j) 'position of largest element on deg(j)';
posmax(j) = deg(j) = max_val ;
There are two maximum members here, but I just want one.for example iwant to have posmax=6 , or posmax=4 . but i dont want posmax = 6,7.
What command should i use?
How can I access the first member?
In general, how can I access a particular member in a subset?
i write posmax('1') but i get error.
Scalars mypos;
Mypos=smin (j$posmax(j) ,j.val);
I am trying to formulate a flowshop scheduling problem in Pyomo. This is an Abstract model
Problem description
There are 3 jobs (chest, door and chair) and 3 machines (cutting, welding, packing in that order). Objective is to minimise the makespan. The python code and the data are as follows.
## flowshop.py ##
from pyomo.environ import *
flowshop = AbstractModel()
flowshop.jobs = Set()
flowshop.machines = Set()
flowshop.machinesN = Param()
flowshop.jobsN = Param()
flowshop.proc_T = Param(flowshop.jobs,
flowshop.machines,
within=NonNegativeReals)
flowshop.start_T = Var(flowshop.jobs,
flowshop.machines,
within=NonNegativeReals)
flowshop.makespan = Var(within=NonNegativeReals)
def makespan_rule(flowshop,i,j):
return flowshop.makespan >= flowshop.start_T[i,j]+flowshop.proc_T[i,j]
flowshop.makespan_cons = Constraint(flowshop.jobs,
flowshop.machines,
rule=makespan_rule)
def objective_rule(flowshop):
return flowshop.makespan
flowshop.objc = Objective(rule=objective_rule,sense=minimize)
## data.dat ##
set jobs := chest door chair ;
set machines := cutting welding packing ;
param: machinesN := 3 ;
param: jobsN := 3 ;
param proc_T:
cutting welding packing :=
chest 10 40 45
door 30 20 25
chair 05 30 15
;
I havent added all the constraints yet, I plan to add them after this issue gets fixed. In the code (flowhop.py) above, for the makespan_rule, I want the makespan to be more that the completion time of only the last machine.
Currently, it is set to be more than completion times of all the machines.
For that, I believe, I have to get the last index of the machines set.
For that, I tried flowshop.machines[-1], but it gives an error saying:
Cannot index unordered set machines
How do I solve this issue?
Thanks for the help.
PS - I am also struggling to model the binary variables used to define the precedence of a job. If you have any ideas regarding that, that would also be helpful.
As the error says Cannot index unordered sets, the set flowshop.machines is not ordered. One needs to provide ordered=True argument in the while declaring the set -
flowshop.machines = Set(ordered=True)
After this, one can access any element by normal indexing - flowshop.machines[i]
For the binary variables, one can declare them as -
c = flowshop.jobsN*(flowshop.jobsN-1)/2
flowshop.prec = Var(RangeSet(1,c),within=Binary)
Then, this variable can be used to decide the precedence between 2 jobs and to formulate the assignment constraints. The precedence variable corresponding to a pair of jobs can be found out using the indices of the jobs (for which the flowshop.jobs has to be an ordered set - flowshop.jobs = Set(ordered=True))
We are investigating migrating a prototype into SQL Server (azure).
We have LineStrings that also have M values. What we would like to do is given another M value find out what its geographical location is.
To aid your visualisation, here is a real-world example:
I have a linestring that represents a flight path. Because the flight goes up and down the distance the plane has actually moved is not the same as the total length of the linestring. We have calibrated M values as a part of the linestring but need to be able to plot on it where a given event occurred. All we know about this event is its M value.
SET #g = geometry::STGeomFromText('LINESTRING(1 0 NULL 0, 2 2 NULL 5, 1 4 NULL 9, 3 6 NULL 15)', 0);
Given something like the above, what is the lat and long of a point with an M value of 8?
This should be an equivalent postgis's ST_LocateAlong
The M value is not a time, but a distance. It should be understood that this distance is arbitrary and does not directly relate to the length of the line and is calibrated against known points. This is due to the set being based on historic data that is in no way accurate by today's standards.
*Note I am not sure if I have Nulled the Z or M value. The extra parameter we are considering here is the M only.