Conversion of AMPL to Gams - type-conversion

I want to convert following lines of code into gams
AMPL
set nodes; ## {1,2,3,4}
set pipes; ### {5,6,7,8}
set arcs within {i in nodes, j in nodes: i != j}; ### {(1,2),(1,3),...}
I am not able to convert line 3 in GAMS. set arcs should contain set of nodes and pair combination

Related

randi generating the same result on a cluster

The following code should generate two random letters:
lett_array = ['abcdefghijklmnobqrstuvwxyz'];
SaveStr.random = ['_',lett_array(randi(26)),lett_array(randi(26))];
It works exactly as expected on my local machine (adds random letters to a save file name to maintain uniqueness).
However on a cluster using slurm scheduling, I get back well over 200 results that ALL CALCULATE THE SAME PAIR OF LETTERS (runs initiate within 15 mins of each other; end over the span of 45 minutes).
This seems... weird. I could probably force reasonable behaviour using
rng('shuffle');
lett_array = ['abcdefghijklmnobqrstuvwxyz'];
SaveStr.random = ['_',lett_array(randi(26)),lett_array(randi(26))];
but I don't understand how I'm having the problem in the first place.
Thoughts? Seems a big problem that matlab doesn't get reliably random numbers on linux clusters.
As stated in the rng (random number generator) documentation:
rng('default') puts the settings of the random number generator used by rand, randi, and randn to their default values. This way, the same random numbers are produced as if you restarted MATLAB.
The default settings are the Mersenne Twister with seed 0.
So you're getting the same values because the cluster nodes each act on a new instance on MATLAB, which uses rng('default') as the initialisation for the random number generator.
Using rng shuffle or rng('shuffle') is the correct thing to do if you want independent randomness on the cluster nodes.
You might also be interested to know about the char function. Since your let_array array is equivalent to ASCII 97:122, you can simply forget the letters array and do a single call to randi like so:
SaveStr.random = ['_', char(randi([97,122], 1, 2))];

How can I specify different ranges for different variables in vpasolve command in MATLAB?

I am solving a set of equations in MATLAB using vpasolve and I would like to give it search ranges for the variables. I know how to do it for one variable but I when I try to give three ranges for three variables, I do not know how it is done. Could anyone help please?
You can specify the allowed variable range using a n x 2 matrix as the last argument, i.e. for initial_guess:
If init_guess is a matrix with two columns, then the two entries of
the rows specify the bounds of a search range for the corresponding
variables. To specify a starting point in a matrix of search ranges,
specify both columns as the starting point value.
So, initial_guess for 3 (=n) input variables would be:
initial_guess = [x1_start x1_end;
x2_start x2_end;
x3_start x3_end;]

Reading a text file with varying length of lines

I have a data file (.txt) which is as follows;
A 2.2 5
B 3.2 0.5
C 0 2
A 3 2 B
A 2 6 C
B 2.3 4.5 C
First three are representing the nodes (name, feature1, feature2) whereas the last three are representing the relation between each node (node A, node B, node C). And as you see, nodes and relations are in different format (nodes=string numeric numeric whereas relations=string, numeric numeric string). At the end I will plot them based on their initial features and relations through time. I tried couple of things but the thing that nodes have 3 parameters and edges have 4 parameters makes it difficult to solve.
So, basically, I want to read the text file line by line and I would like to be able to define all the nodes and have all the parameters of the nodes as string numeric numeric and define all the relations as well to plot them in the end.
Any help is appreciated.
check out the built-in function fgetl.
fid = fopen(filename);
lineoftext = fgetl(fid);
while ischar(lineoftext)
C = strsplit(strtrim(lineoftext)); % this will be a cell array
if length(C) == 3
% then it's a node, put code here
else
% then it's relational, put code here
end
lineoftext = fgetl(fid);
end
fclose(fid);
This will read a single line from the file, split it into chunks of text in a cell array, and then count the number of chunks to see if it's a node or a relation string. You'll have to put your own code inside the if statements. Then it reads in another line and does it all over again. When it reaches the end of the file, lineoftext = -1 and the while loop ends.

How do I joint test a multi-level categorical effect in ipython using statsmodels?

I am using the Ordinary Least Squares (ols) function in statsmodels in ipython to fit a linear model where one covariate (City) is a multi-level categorical effect:
result=smf.ols(formula="Y ~ C(City) + X*C(Group)",data=s).fit();
(X is continuous, Group is a binary categorical variable).
When I do results.summary(), I get one row per level of City, however, what I would like to know is the overall significance of the 'City' covariate (i.e., compare Y~C(City)+X*C(Group) with the partial model Y~X*C(Group)).
Is there a way of doing it?
thanks in advance
Thank you user333700!
Here's an elaboration of your hint. I generate data with a 3-level categorical variable, use statsmodels to fit a model, and then test all levels of the categorical variable jointly:
# 1. generate data
def rnorm(n,u,s):
return np.random.standard_normal(n)*s+u
a=rnorm(100,-1,1);
b=rnorm(100,0,1);
c=rnorm(100,+1,1);
n=rnorm(300,0,1); # some noise
y=np.concatenate((a,b,c))+n
g=np.zeros(300);
g[0:100]=1
g[100:200]=2
g[200:300]=3
df=pd.DataFrame({'Y':y,'G':g,'N':n});
# 2. fit model
r=smf.ols(formula="Y ~ N + C(G)",data=df).fit();
r.summary()
# 3. joint test
print r.params
A=np.identity(len(r.params)) # identity matrix with size = number of params
GroupTest=A[1:3,:] # for the categorical var., keep the corresponding rows of A
CovTest=A[3,:] # row for the continuous var.
print "Group effect test",r.f_test(GroupTest).fvalue
print "Covariate effect test",r.f_test(CovTest).fvalue
The result should be something like this:
Intercept -1.188975
C(G)[T.2.0] 1.315898
C(G)[T.3.0] 2.137431
N 0.922038
dtype: float64
Group effect test [[ 120.86097747]]
Covariate effect test [[ 259.34155851]]
brief answer
you can use anova_lm (type 3) directly or use f_test or wald_test and either construct the constraint matrix or provide the constraints of the hypothesis in the form of a sequence of formulas.
http://statsmodels.sourceforge.net/devel/anova.html
http://statsmodels.sourceforge.net/devel/generated/statsmodels.regression.linear_model.RegressionResults.f_test.html

GnuPlot chart drawing for each text file

I compare four kind of sorting algorithms, and their inversions and comparisions for each algorithm are stored in a file. What I need now is to draw a scatter with markers (x,y) for each file where
x -> number of inversions
y -> number of comparisions
and scale it to the numbers, so for example we have IS10.txt which stands for InsertionSort and it has 300 lines with x's and y's.
Sample data
line 1: 20 33
line 2: 18 27
...
line 300: 21 24
The key is to be able to generate diagrams for comparison.
Plotting a single file is straightforward, just use
plot 'IS10.txt' using 1:2 title 'InsertionSort'
If you want to plot all files, you can do it as follows:
list = system('ls -1 *.txt | tr "\n" " "')
set key out
plot for [file in list] file using 1:2 title file
Here, I assumed, that all .txt files in the current directory should be plotted. You could of course also generate the list manually. It should contain all file names, delimited by a space (e.g. list = "IS10.txt HS10.txt ...").
That plots all data points of one file with the same linetype. The first file uses linetype 1, the second one linetype 2 etc. Type test to see how the points and colors of these default linetype look like.
You can use something like set linetype 1 linecolor rgb 'blue' pointtype 7 to change these settings in order to get 20 well-distinguishable point styles.