GAMS Definition of a Subset with k and k+1 - set

So I have the following set:
i 1,2,3,...,I
j 1,2,3,...,J
k 1,2,3,...,K with k´2,3,4,...,K
I am defining my set and my parameters via GDX-import through a Excel Sheet so I can change the Set and the parameters dynamically in order to do a calculation study with a linear-programming model.
How do I define this kind of set with k´ so it works with parameters like d_kk´(Distance from k to k´) and t_jkk´?
Keep in mind that the solution has to work on a large scale at least 1,...,100 for every indice.
Thanks a lot.
Cheers,
SAM

Sounds like you whant to calculate the distance between different points in set k. Use a alias set and a multidimentional set:
Set i /1*I/
j /1*J/
k /1*K/
kk(k,k);
alias(ka,k);
kk(k,ka)$(ord(k)<ord(ka))=yes;
parameter
x(k) 'x-koordinate'
y(k) 'y-koordinate'
d(k,k) 'Distance from k to ka';
x(k)=uniform(0,1);
y(k)=uniform(0,1);
d(k,ka)$kk(k,ka)=sqrt((x(k)-x(ka))^2+(y(k)-y(ka))^2);

Related

Compare cardinality of multiple sets and get specific value from member of greatest set

I am using clingo to solve flood-it problems. I use the predicate frontier([CELL], [COLOR], [TIMESTEP]) to keep track of all cells that are neighbors of the flood. The set of frontiers could look something like this:
frontier(c(1,3),2,3) frontier(c(2,1),2,3) frontier(c(2,2),3,3) frontier(c(2,3),3,3) frontier(c(3,1),3,3) frontier(c(3,2),3,3) frontier(c(4,1),3,3)
We can split this set in two subsets. One where each color value is 2 or 3 respectively. What I need is basically two things:
Determine which subset is bigger, i.e. if there are more cells with color value 2 or 3 (BTW the number of colors is not fixed, thus a solution has to be generic)
Get the color value of a member of the biggest set
How can I compare the cardinalities of n (n>=2) sets in predicate logic?
Thank you in advance!
I found an answer which is more domain (i.e. clingo) specific than general.
What I initially do is count the number of cells that are of color C:
frontier_subset_size(C,N) :- color(C), N = #count{ X : frontier(X,C) }.
Then I filter the biggest set(s) using the #max aggregate:
max_subset_color(C) :- frontier_subset_size(C,N), N = #max{ M : frontier_subset_size(_,M) }.
This works as desired for this specific problem.
Yet I would like to know how to do that in pure predicate logic.

Checking if there exist two set which are equal

I have a set J of customers and a set of facilities I. For each customer j there are K_j<=|I| different distances to facilities. Let these (sorted) distances be D_j^1 < D_j^2 < ... < D_j^ {K_j}. For each distance we define a set of facilities being closer than (in the soft sense) than D_j^k units of distance from customer j. This set is given by V_j^k={ i\in I | d_{ij}<=D_j^k }. My question is, is there a smart way to check if there exists i,j,k,l such that V_j^K=V_i^l? It can be assumed that the indices in V_j^k are sorted. My only solution is something like
for(j in J) do
for(k in {1,...,K[j]} ) do
V=V[j][k];
for(i in J\{j} )
for(l in {1,...,K[i]} ) do
compare(V,V[i][l])
where the compare function just compares the entries in the two sets. But this has a very high running time. Are there some brilliant ways of performing this task?
You have a matrix (M), where a row corresponds to a customer. The elements of this matrix are sets of facilities. M_cf = the set containing the f closest facility to customer c. In this matrix, the elements of the first column will be sets containing only one facility, which is always the closest facility to the customer. In the second column, every set will have 2 facilities, etc. Now we will find sets that have the same facilities. These sets must be in the same column, otherwise the number of elements in each set would be different, so the sets couldnt be equal. In a column f, you would normally need to compare all sets to all other sets to find equal sets, which would result in O(f^2) time complexity. You can reduce it to O(f*log(f)) if you instead sort the sets in this column using an appropriate algorithm. The total time complexity will be O(j*f*log(f)), which is way better than your current O(j^2*f^2), assuming O(1) compare.

MATLAB How to fill individual entries of a sparse matrix using vectorised form?

I have a sparse matrix and I need to fill certain entries with a specific value, I am using a for loop right now but I know its not the correct way to do it so I was wondering if its possible to vectorise this for loop?
K = sparse(N);
for i=vectorofrandomintegers
K(i,i) = 1;
end
If I vectorise it normally as so:
K(A,A) = 1;
then it fills all the entries in each row denoted by A whereas I want individual entries (i.e. K(1,1) = 1 or K(6,6)=1).
Also, the entries are not diagonally adjacent so I can't plop the identity matrix into it.
If you are going to use a vectorized method, you would need to get the linear indices to be set. The issue is that if you define your sparse matrix as K = sparse(N) and then linearly index into K, it would extend the size of it in one direction only and not along both row and column. Thus, you need to specify to MATLAB that you are
looking to use this sparse to store a 2D array. Thus, it would be -
K = sparse(N,N);
Get the linear indices to index into K using sub2ind and set them -
ind1 = sub2ind([N N],vectorofrandomintegers,vectorofrandomintegers);
K(ind1) = 1;
It's fairly simple
i'd use
K((A-1)*N+A))=1;
i believe that should fix your problem by treating the matrix as a vector
Instead of declaring and then filling a sparse matrix, you can fill it at the same time you define it:
i = vectorofrandomintegers; j = i;
K = sparse(i,j,1,N,N)

How to specify custom order of values in variable's domain?

Let's assume I have a variable V and value of V can be any number from the range 0..5. However, some values are more preferred than other others therefore it would help me to specify the domain of V as an ordered sequence.
Can I do it in SICStus Prolog?
Example:
% PSEUDOCODE
%
% 3 is more preferred than 4; 4 is more preferred than 2; and so on..
% So I would write something like this:
V in {3,4,2,5,1,0},
getDomainAsList(V, List), % the predicate do not exist
% and the List would be: [3,4,2,5,1,0] and not [1,2,3,4,5]
I read the manual and I did not find anything that would help. I can solve the problem by custom labeling (i.e., convert the domain of V to a list, sort it and assign a value to V) but I expect worse performance.
There is a manual page describing this.
See the value(Enum) option to labeling/2, here:
You can have an array or list of all the values in the preferred order.
Then you work with array indexes in your program, and at the very end you return values corresponding to the indexes.

Finding sets that are a subset of a specific set

Lets say I have 4 different values A,B,C,D with sets of identifiers attached.
A={1,2,3,4,5}
B={8,9,4}
C={3,4,5}
D={12,8}
And given set S of identifiers {1,30,3,4,5,12,8} I want it to return C and D. i.e. retrieve all sets from a group of sets for which S is a superset.
Is there any algorithms to perform this task efficiently (Preferably with low memory complexity. Using external device for storing data is not an option) ?
A trivial solution would be for each member in the superset S retrieve list of sets that include that member (basically inverted index) and for each returned set check that all of his members are in the superset. Unfortunately because on average the superset will include at least one member for each set there is a significant and unacceptable performance hit with this approach.
I am trying to do this in Java. Set consist of integers and the value they identify is an object.
Collection of sets is not static and bound to change during the course of execution. There will be some limit on the set number though.
Set size is not limited. But on average it's between 1 and 20.
Go through each element x in S.
For each set t for which x ∈ t, increment a counter—call it tcount—associated with t.
After all that, for each set t for which tcount = | t |, you know that t ⊆ S.
Application.
After step 2.
Acount = 4,
Bcount = 1,
Ccount = 3,
Dcount = 2.
Step 3 processing.
Acount ≠ |A| (4 ≠ 5) — Reject,
Bcount ≠ |B| (1 ≠ 3) — Reject,
Ccount = |C| (3 = 3) — Accept,
Dcount = |D| (2 = 2) — Accept.
Note after cgkanchi note: The following algorithm is under the assumption that you don't really use sets but arrays. If that is not the case, you should look for a method which implements intersection of sets and then the problem is trivial. This is about how to implement the notion of intersection using arrays.
Sort all sets using heapsort for in-place sorting O(1) space. It runs in O(nlogn) and soon enough it will pay you back.
For each set L of all sets:
2.1. j = 0
2.2. For the i element in L:
2.2.1. Starting from j element find L[i] in S for which L[i] = S[j] else reject. If L and S and large enough use binary search or interpolation search (for the second one, have a look at your data distibution)
2.3. Accept
As for Java, I’d use a Hashtable for the lookup table of the elements in S. Then for each element in X, the set you want to test if it’s a subset of S, test if it’s in the lookup table. If all elements of X are also in S, then S is a superset of X.

Resources