can I use multiplication of sets in AMPL? - ampl

I am trying to write a model for a graph problem.
I defined a pairwise set for labeling the edges. because I need to find inner product between some edges. More precisely:
Suppose E is the set of edges, I defined :
set N, within E cross E;
for example if E={1,2,3}, N can be :{(2,3),(1,2)}
i.e I need to find inner product between edge 1 and edge 3 and also between edge 1 and edge 2.
No my problem is that I have to related edges to vertices.
For example suppose V={v1,v2,v3,v4,v5} is the set of vertices. and edge 1 is between v1 and v3 and edge 2 is between v2 and v3.
I need to define another pairwise set for example:
set N, within V cross V;
But I need to determine the set of edges behind that. for example if M={(1,3),(2,3)} I need a third column that determine the first pair is belong to edge 1 and the second belong to edge 2.
how I can do that in AMPL?

You could do it like this:
set vertices;
set edgenames;
set edges_with_vertices within edgenames cross vertices cross vertices;
such that for any triple in edges_with_vertices the first element is the edge name, and the second and third are the end vertices.

Related

The mass of components in a binary system for given largest and smallest distance

I try to calculate the mass of the component stars in a binary system.
I only have the period and the largest and smallest distance between them and know how to use them to get the total mass.
To my knowledge, I think I need the distance from one of the stars to the barycenter.
Is that possible to calculate each mass of the component members with this information?
Thank you for your help!
I think, if you only have the period T and the largest a_max and smallest a_min distance between the two stars, as you pointed out, you can calculate the total mass, using the formula
mass_1 + mass_2 = (2*pi / T)^2 * ((a_min + a_max)^3 / G)
However, you cannot calculate the individual masses solely from this information, because the prescribed data, the period T and the largest a_max and smallest a_min distance, are for the relative postion of the stars, not the individual.
What do I mean. Assume you have two stars whose motion has the parameters given above. Then, by Newtonian mechanics, let us assume your coordinate system is placed at the barycenter and if you denote the position vectors r1 and r2 pointing from the barycenter to the respective stars, then, the equations of motion are
(d/dt)^2 r1 = - ( mass_2*G / |r2 - r1|^3 )*(r1 - r2)
(d/dt)^2 r2 = - ( mass_1*G / |r2 - r1|^3 )*(r2 - r1)
If you subtract the first vector differential equation from the second, and you set r = r2 - r1, you obtain the vector differential equation (3 scalar differential equations and 3 scalar variables, the 3D coordinates of the relative position vector r)
(d/dt)^2 r = - ( (mass_1 + mass_2)*G / |r|^3 ) * r
This is the classical vector differential equation that describes the time-evolution of the relative position vector r between the two stars. The information you have, the period T and the largest a_max and smallest a_min, can be used to find a specific solution to the last equation above, the one for r, which gives you the relative motion r = r(t) between the two stars with the prescribed properties. However, the motion of any pair of stars with arbitrary masses mass_1 and mass_2, that sum up to the same value mass_1 + mass_2, will provide a solution to the vector differential equation
(d/dt)^2 r = - ( (mass_1 + mass_2)*G / |r|^3 ) * r
and among all such solution there will be some that posses the desired properties: period T and the largest a_max and smallest a_min. Observe that T, a_min and a_max are properties of the vector r and not so much properties of the individual r1 and r2, which tells you that you cannot find the individual masses.

Constraining on a Minimum of Distinct Values with PuLP

I've been using the PuLP library for a side project (daily fantasy sports) where I optimize the projected value of a lineup based on a series of constraints.
I've implemented most of them, but one constraint is that players must come from at least three separate teams.
This paper has an implementation (page 18, 4.2), which I've attached as an image:
It seems that they somehow derive an indicator variable for each team that's one if a given team has at least one player in the lineup, and then it constrains the sum of those indicators to be greater than or equal to 3.
Does anybody know how this would be implemented in PuLP?
Similar examples would also be helpful.
Any assistance would be super appreciated!
In this case you would define a binary variable t that sets an upper limit of the x variables. In python I don't like to name variables with a single letter but as I have nothing else to go on here is how I would do it in pulp.
assume that the variables lineups, players, players_by_team and teams are set somewhere else
x_index = [i,p for i in lineups for p in players]
t_index = [i,t for i in lineups for t in teams]
x = LpVariable.dicts("x", x_index, lowBound=0)
t = LpVAriable.dicts("t", t_index, cat=LpBinary)
for l in teams:
prob += t[i,l] <=lpSum([x[i,k] for k in players_by_team[l]])
prob += lpSum([t[i,l] for l in teams]) >= 3

How can I find (generate) data points form a shape in 2D in MATLAB ? For example, the letter A , B ,and C. Thanks

How can I find or generate data points form a shape in 2D in MATLAB ? For example, the letters A, B, and C.
You can use fill()
An example for an octogon, provided by
See https://www.mathworks.com/help/matlab/ref/fill.html
% Generate the points required for the fill.
t = (1/16:1/8:1)'*2*pi; % using 1/8 steps we get an 8 sided object.
x = cos(t);
y = sin(t);
% fill the data
fill(x,y,'r')
axis square % prevent skewing the result.
An example of generating the x y coordinates of a rectangle with an offset of (5,5):
x=[5 5 25 25 5]
y=[5 15 15 5 5]
You have 5 points because you need to include the final point to complete the path ( I believe ) Follow the blue path when collecting the x coordinates and the y coordinates. You can see we start at 5,5 then move to 5,15 --- so the first part of the path is
x=[5 5 ...
y=[5 15 ...
If you want to generate the coordinates automatically, you could use a program like InkScape (vector program) to help you convert a character to paths, but here is a simple example drawn with the pen tool:
The points are given by
m 0,1052.3622 5,-10 5,0 5,10 z
which 1052.3622 is VERY large, but is ultimately because I placed my shape at the bottom of the page. if we set this to be 0,0 it would go to the top of the page.

Metropolis Hastings Random Walk SQL Implementation

Is it possible and efficient to implement MHRW algorithm in SQL?
I want to sample a direct large graph with +1 million nodes and this seems to be one of the best ways to do it. The purpose of the algorithm is for undirect graphs, but I think it can work for directed ones too
The algorithm:
v <- initial node
while stop criteria not met do
select node w uniformly at random from neighbors of v;
generate uniformly at random 0<= p <= 1
if p <= (degree of v) / (degree of w)
then v <- w
else
stay at v
end if
end while
I take the initial node from table1, which contains all nodes and their properties. In table2 I have two columns that display all connections between nodes (and a way to get a nodes degree). The stop criteria would be the size of the sample, ie, while sample <= ~100.000 nodes.
Best regards.

Number of BST's given a linked list of numbers

Suppose I have a linked list of positive numbers, how many BST's can be generated from them, provided all nodes all required to form the tree?
Conversely, how many BST's can be generated, provided any number of the linked list nodes can exist in these trees?
Bonus: how many balanced BST's can be formed? Any help or guidance is greatly appreciated.
You can use dynamic programming to compute that.
Just note that it doesn't matter what the numbers are, just how many. In other words for any n distinct integers there is the same amount of different BSTs. Let's call this number f(n).
Then if you know f(k) for k < n, you can get f(n):
f(n) = Sum ( f(i) + f(n-1-i), i = 0,1,2,...,n-1 )
Each summand represents the number of trees for which the (1+i)-th smallest number is at the root (thus in the left subtree where are i numbers and in the right subtree there are n-1-i).
So DP solves this.
Now the total number of BSTs (with any nodes from the list) is just a sum:
Sum ( Binomial(n,k) * f(k), k=1,2,3,...,n )
This is because you can pick k of them in Binomial(n,k) ways and then you know that there are f(k) BSTs for them.