I am a beginner in Jacop constraint solver. I looked into its documents but couldn't find how to solve my following problem. I have a matrix[n][n] of nxn domain variables of IntVar. Now, I need to apply the following constraint to it:
matrix[matrix[i][j]][k] == x
Here x is a regular integer, I mean not domain variable.
The problem that i am facing is that matrix[i][j] is a domain variable, and I am unable to give it as the first index in matrix[index][k] again.
I will really appreciate your help.
Basically you need to use element constraints to do that. First, create a vector matrix_k that represent a column k of matrix. Then, you can use Element constraint of the form Element(matrix[i][j], matrix_k, x).
Related
I have to solve a problem with permutations. The function takes vector a with n elements as a parameter. I declare b as #variable - there should be the permutation 1:n that gives the best result after finding the solution of a problem.
The error appears when I want to create #constraint. I have to use a[b[1]], so it takes the first element from vector which is a variable. It gives my error, that I can't use type VariableRef as a index of an array. But how can I get around this when I have to use it?
I sounds as if you have two optimisation problems one of which is an integer programming problem. You might think about separating the two.
(Sorry for not writing a comment, my reputation is still too low ;-) )
two values of the matrix A are changing as you can see in the picture. Why is this happening? What can I do to solve it?
I tried 'Execute the entire worksheet' but could not solve it.
The Matrix which you first assign to A contains the name C in the formulas used for both the A[2,3] and A[3,2] entries.
And then you assign a Vector to the name C.
So, of course, that affects those two entries of A.
If you don't want that to happen then choose use two different names for the two different purposes, eg. C for the Vector and CP in the entries, or vice versa.
You will always run into trouble if you try to use the same name to mean two different things. That's not just a Maple issue: it'll happen to you in math, and in other programming languages.
I am using BsplinesComp for a sample problem.
The objective is to maximize the area under the line.
My problem arises when I want to set a constraint for one of the values in the output array that bspline gives. So a value such that the spline goes through that no matter what configuration it is in.
I tried this in two ways and I have uploaded the codes. They are both very badly coded so i think there is a neater way to do so. Links to codes:
https://gist.github.com/stackoverflow38/5eae1e86c5802a4df91becdf580d28c5
1- Using an extra explicit component in which the middle array value is imposed to be a selected value
2- Tried to use an execcomp but I get an error. Target shapes do not match.
I vaguely remember reading such a question but could not find it.
Overall I am trying to set a constraint for either the first, middle or last value of the bspline and some range that it should be in.
Similar to the plots here
So, I think you want to know the best way to do this, and the best way is to not use any extra components at all. You can directly constrain a single point in the output of the BsplinesComp by using the "indices" argument in the add_constraint call. Here, I constrain the first point in the spline to lie on the interval [-1, 1].
model.add_constraint('interp.h', lower=-1, upper=1, indices=[0])
Running the model gives me a shape that looks more like one of the ones you included.
Just for reference, for the errors you got with 1 and 2:
Not sure what is wrong here, but maybe the version you uploaded isn't the latest. You never used the AeraComp in a constraint, so it didn't do anything.
The exception was due to a size mismatch in connecting the vector output of the Bsplines comp to a scaler expression. You can do this by specifying the "src_indices", giving it a list of which indices in the array to connect to the target. model.connect('interp.h', 'execcomp.x', src_indices=[0])
I am using GAMS to solve a network distribution problem and this is my first time using GAMS. I have the following constraint (see Image) which I want to write in gams but keep getting errors. Trying to figure it out using IF statement or any other way to solve it. The variable z is a binary variable, which has been declared already.
Thanks!
Image
You do not need an if statement, but can handle this with dollar conditions.
You can do it with dollar conditions in the equation (as done here), or you could write three separate equations with dollar conditions to define the domain of each equation.
E_z(u,v,i).. sum(j, z(u,v,j,i)) - sum(j, z(u,v,i,j))
=E=
0 + 1$(sameas(i,u)) - 1$(sameas(i,v));
The sameas operator is documented here. If your sets have numerical values, it might be cleaner to do a value comparison, e.g. $(i.val = u.val).
You can read more about conditional expressions in GAMS in the following link:
https://www.gams.com/latest/docs/userguides/userguide/_u_g__cond_expr.html
I am trying to do some calculations where I divide two vectors. Sometimes I encounter a division by zero, which cannot take place. Instead of attempting this division, I would like to store an empty element in the output.
The question is: how do I do this? Can vectors have empty fields? Can a structure be the solution to my problem or what else should I use?
No, there must be something in the memory slot. Simply store a NaN or INT_MIN for integer values.