Resource Path Location : Type CPLEX(default) cannot extract expression - opl

I'm getting the error:
CPLEX(default) cannot extract expression
on the expression
"minimize (1 / 2)*(sum(i in 923..931, j in 1..18, k in 923..931, l in 1..18) (((f[i]*s[i][k])*d[j][l])*x[i][j])*x[k][l])+(1 / 2)*(sum(i in 1..922, j in 1..18, k in 923..931, l in 1..18) (((f[i]*s[i][k])*d[j][l])*y[i][j])*x[k][l])+(1 / 2)*(sum(i in 923..931, j in 1..18, k in 1..922, l in 1..18) (((f[i]*s[i][k])*d[j][l])*x[i][j])*y[k][l])+sum(i in 923..931, j in 1..18) (f[i]*rs[j])*x"[i][j]."
Can anyone help me? I already try to use "using CP;" but it didn't work.
My code is the following:
// Indices, Parameters and variables
int Nbeitems=...;
int Nbnitems=...;
int Nbcorridors=...;
range nitems = Nbeitems+1..Nbeitems+Nbnitems;
range eitems = 1..Nbeitems;
range corridors = 1..Nbcorridors;
dvar boolean x[nitems][corridors];
float y[eitems][corridors]=...;
float f[nitems]=...;
float s[nitems][nitems]=...;
float d[corridors][corridors]=...;
float rs[corridors]=...;
float S[nitems]=...;
// objective funtion
minimize 1/2*sum(i in nitems,j in corridors, k in nitems,l in corridors) f[i]*s[i][k]*d[j][l]*x[i][j]*x[k][l]+1/2*sum(i in eitems,j in corridors,k in nitems,l in corridors) f[i]*s[i][k]*d[j][l]*y[i][j]*x[k][l]+1/2*sum(i in nitems,j in corridors,k in eitems,l in corridors) f[i]*s[i][k]*d[j][l]*x[i][j]*y[k][l] + sum(i in nitems, j in corridors) f[i]*rs[j]*x[i][j];
// constraints
subject to
{ forall (j in corridors)
ctEachitemhasoneslot:
sum (i in eitems) y[i][j] + sum (i in nitems) x[i][j] <= 90;
forall (i in nitems)
ctEachitemhasstorenecessities:
sum(j in corridors) x[i][j]==S[i];
}

similar question at https://developer.ibm.com/answers/questions/523660/cplexdefault-cannot-extract-expression.html?utm_campaign=answers&utm_medium=email&utm_source=answers-new-question&utm_content=answers-answer-question
in your objective you have out of range. But if you write
minimize 1/2*sum(i in nitems,j in corridors, k in nitems,l in corridors)
f[i]*s[i][k]*d[j][l]*x[i][j]*x[k][l]+1/2*
sum(i in eitems,j in corridors,k in nitems,l in corridors:i in nitems)
f[i]*s[i][k]*d[j][l]*y[i][j]*x[k][l]+
1/2*sum(i in nitems,j in corridors,k in eitems,l in corridors:k in nitems) f[i]*s[i][k]*d[j][l]*x[i][j]*y[k][l] +
sum(i in nitems, j in corridors) f[i]*rs[j]*x[i][j];
Then they will go away

Related

Why does x = x * y / z give a different result from x *= y / z for integers?

I have the following function:
pub fn s_v1(n: &u64) -> u64 {
let mut x: u64 = 1;
for i in 1..=*n {
x = x * (*n + i) / i;
}
x
}
This code gives the correct answer for s_v1(&20) == 137846528820
However, if I change the line in the for loop to x *= (*n + i) / i;
The answer changes to s_v1(&20) == 16094453760
Why are the results different? Isn't x = x * y the same as x *= y ?
Because * and / have the same precedence with left associativity, the expression is not
x * ((*n + i) / i)
(which is the same as x *= (*n + i) / i) but
(x * (*n + i)) / i
As others have indicated, there are two problems:
a*=b/c is equivalent to a=a*(b/c) and not to a=a*b/c (which is implicitly a=(a*b)/c).
/ denotes division according to the types of the operands. In this case the operands are both integers, and therefore / denotes integer division discarding any remainder, and therefore (a*b)/c is not the same as a*(b/c) (unless b is an exact multiple of c).
If you want to replace the line in the loop, you'd need to split the two operations:
for i in 1..=*n {
x *= *n + i;
x /= i;
}
One disadvantage of this algorithm is that it will not produce correct results when the answer should be between MAXINT/2n and MAXINT. For that, you actually need to take advantage of what you were attempting to do:
for i in 1..=*n {
if (*n % i == 0) {
x *= *n / i + 1;
} else if (x % i == 0) {
x /= i;
x *= *n + i;
} else {
x *= *n + i;
x /= i;
}
}

Confused about a string hash function

As I was looking through some string hash fucntions, I came across this one (code below). The function processes the string four bytes at a time, and interprets each of the four-byte chunks as a single long integer value. The integer values for the four-byte chunks are added together. In the end, the resulting sum is converted to the range 0 to M-1 using the modulus operator.
The following is the function code :
// Use folding on a string, summed 4 bytes at a time
long sfold(String s, int M) {
int intLength = s.length() / 4;
long sum = 0;
for (int j = 0; j < intLength; j++) {
char c[] = s.substring(j * 4, (j * 4) + 4).toCharArray();
long mult = 1;
for (int k = 0; k < c.length; k++) {
sum += c[k] * mult;
mult *= 256;
}
}
char c[] = s.substring(intLength * 4).toCharArray();
long mult = 1;
for (int k = 0; k < c.length; k++) {
sum += c[k] * mult;
mult *= 256;
}
return(Math.abs(sum) % M);
}
The confusion for me is this chunk of code, especially the first line.
char c[] = s.substring(intLength * 4).toCharArray();
long mult = 1;
for (int k = 0; k < c.length; k++) {
sum += c[k] * mult;
mult *= 256;
To my knowledge, the substring function used in this line takes as argument : begin index inclusive, The substring will start from the specified beginIndex and it will extend to the end of the string.
For the sake of example, let's assume we want to hash the following string : aaaabbbb. In this case intLength is going to be 2 (second line of function code). Replacing the value of intlength in s.substring(intLength * 4).toCharArray() will give us s.substring(8).toCharArray() which means string index is out of bounds given the string to be hashed has 8 characters.
I don't quite understand what's going on !
This hash function is awful, but to answer your question:
There is no IndexOutOfBoundsException, because "aaaabbbb".substring(8) is ""
The purpose of that last loop is to deal with leftovers when the string length isn't a multiple of 4. When s is "aaaabbbbcc", for example, then intLength == 2, and s.substring(8) is "cc".

Math.Net Exponential Moving Average

I'm using simple moving average in Math.Net, but now that I also need to calculate EMA (exponential moving average) or any kind of weighted moving average, I don't find it in the library.
I looked over all methods under MathNet.Numerics.Statistics and beyond, but didn't find anything similar.
Is it missing in library or I need to reference some additional package?
I don't see any EMA in MathNet.Numerics, however it's trivial to program. The routine below is based on the definition at Investopedia.
public double[] EMA(double[] x, int N)
{
// x is the input series
// N is the notional age of the data used
// k is the smoothing constant
double k = 2.0 / (N + 1);
double[] y = new double[x.Length];
y[0] = x[0];
for (int i = 1; i < x.Length; i++) y[i] = k * x[i] + (1 - k) * y[i - 1];
return y;
}
Occasionally I found this package: https://daveskender.github.io/Stock.Indicators/docs/INDICATORS.html It targets to the latest .NET framework and has very detailed documents.
Try this:
public IEnumerable<double> EMA(IEnumerable<double> items, int notationalAge)
{
double k = 2.0d / (notationalAge + 1), prev = 0.0d;
var e = items.GetEnumerator();
if (!e.MoveNext()) yield break;
yield return prev = e.Current;
while(e.MoveNext())
{
yield return prev = (k * e.Current) + (1 - k) * prev;
}
}
It will still work with arrays, but also List, Queue, Stack, IReadOnlyCollection, etc.
Although it's not explicitly stated I also get the sense this is working with money, in which case it really ought to use decimal instead of double.

Recursion code in VBA

I am trying to run this code to calculate Q(n) at different Tn in the Equation 16.4 in the attached picture.But its not giving me the correct output. I would appreciate any help. Note: delta1=delta2 =...deltan = dt=1 ( I have taken here ) and further divided S term by 10000 just because in the Equation it is in basis point i.e. 100th part of 1 %.
Function Bootstrap(S As Range, Z As Range, L As Double) As Double
Dim j As Integer
Dim a As Variant
Dim b As Variant
Dim n As Integer
Dim Q() As Double
Dim sum As Double
Dim P As Double
Dim dt As Double
n = Application.WorksheetFunction.Max(S.Columns.Count, Z.Columns.Count)
a = S.Value
b = Z.Value
dt = 1
sum = 0
ReDim Q(0 To n)
Q(0) = 1
For j = 1 To n - 1
P = (b(1, j) * (L * Q(j - 1) - (L + dt * a(1, n) / 10000) * Q(j))) / (b(1, n) * (L + a(1, n) * dt / 10000)) + Q(n - 1) * L / (L + a(1, n) * dt / 10000)
sum = sum + P
Q(n) = sum
Next j
Bootstrap = sum
End Function
To solve a recursive function you can write it this way, for example
Function Factorial(n as long) as long
If n = 1 Then
Factorial = 1
Else
Factorial = n * Factorial(n-1)
End If
End function
Yes, you can see For...Loop can also do the Factorial calculation, but in your case, its much easier to use recursive solution.
Besides Eq 16.4 is intentionally written as a recursive function. It is not written as a summation function because it is harder to do so. If given to you is a summation function, then you can apply the For...Loop solution.
Hope this helps.
EDIT
Function Q(n as long) as double
If n = 1 Then
Q = 5
Else
Q = Z * ( L * Q_t - (L + d * S) * Q(n-1) ) / ( Z * ( L + d * S ) )
End If
End Function
Notice that the function Q keep calling itself in Q(n-1) when n>1. That is called recursive solution.
(Check the formula. I might copy it wrong)

Algorithm to get spiral position by coordinate

This question is a reverse of these questions:
Find the position nth element of a rectangular tiled spiral?
Coordinate Algorithm - Rotate around the center
Looping in a spiral
Currently I have this code which gets the coordinate of the nth element in a spiral of squares:
private int[] getPos(int n) {
int x = 0, z = 0;
if (--n >= 0) {
int v = (int) Math.floor(Math.sqrt(n + .25) - 0.5);
int spiralBaseIndex = v * (v + 1);
int flipFlop = ((v & 1) << 1) - 1;
int offset = flipFlop * ((v + 1) >> 1);
x += offset; z += offset;
int cornerIndex = spiralBaseIndex + (v + 1);
if (n < cornerIndex) {
x -= flipFlop * (n - spiralBaseIndex + 1);
} else {
x -= flipFlop * (v + 1);
z -= flipFlop * (n - cornerIndex + 1);
}
}
return new int[]{x,z};
}
Now I need a function that maps the other way, any ideas?
Okay. On the assumption that your spiral is, in fact, a square spriral, this is simple enough.
First, we can tell which spiral "level" (or which ring) the position will be on by using this:
//parameter "position" assumed
int level = 0;
int layerSize = 1;
while(position > layerSize * layerSize) {
layerSize += 2;
level++;
}
We know this is the case because each spiral puts squares on either side of the previous spiral (that's the +2) and so the level the position is on is going to be in the area of the largest possible square spiral.
At this point you can use getPos to get the position of (layerSize * layerSize + 1) after that, just walk the spiral shape until your position number is reached.

Resources