I am a beginner of programming. I'm trying to practice run a simulation with CPLEX.
Since I want to be a person who wants to work in an area in optimization.
Therefore, I am trying to study some journals from different areas by myself.
The attached image is the objective function and constraints.
I made up the code written below. I am not sure I made it right or wrong.
enter image description here
//Data
{string} product = ...;
{string} interval = ...;
// Limit
float low[interval] = ...;
float upper[interval] = ...;
// Maximum demand
float A[product] = ...;
// Slope of demand function
float varphi[product][interval] = ...;
// Price
float p[product] = ...;
// Setup cost
float f[product] = ...;
// Inventory holding cost rate
float h[product][interval] = ...;
// Variable cost rate
float v[product][interval] = ...;
// Variables
dvar float+ X[product][interval]; enter code here
dvar float+ D[product][interval];
dvar float+ a[interval];
dvar float+ beta[product][interval];
// Objective
maximize sum(i in product, j in interval) p[i]*X[i][j]-f[i]*beta[i][j]-v[i][j]*X[i][j]-0.5*h[i][j]*X[i][j] - F;
// Constraint
subject to{
forall(j in interval) sum(j in interval) a[j] == 1;
forall(i in product, j in interval) beta[i][j] <= a[j];
forall(i in product, j in interval) sum(i in product, j in interval) X[i][j] <= C;
forall(i in product, j in interval) X[j][i] <= D[i][j]*beta[i][j];
forall(j in interval) sum(i in product) beta[i][j] <= upper[j]*a[j];
forall(j in interval) sum(i in product) beta[i][j] >= low[j]*a[j];
forall(i in product, j in interval) D[i][j] = (A[i]-varphi[i][j]*p[i])*a[j];
}
I want to demonstrate how an iterative procedure may be applied to progressively narrow the interval of search to precisely determine the optimal number of products to produce in order to maximize profit.
The journal explains as "The cost and revenue data for these smaller intervals are provided as new inputs to the model which then identifies one of these new intervals as best in the subsequent iteration. The procedure is repeated in successive iterations until the last interval has only one or two levels (i.e., numbers of products) from which the model is able to make a final choice of product variety. In order to save time when the marginal benefit from successive iterations is very small, we also terminate the process if the difference between the objective function values (profit) of successive iterations is below a small predetermined convergence parameter. The model determines which of the levels of the final interval is optimal and also identifies which particular products to produce and in what quantities.
The first stage of this process begins with 100 products (as before) and a configuration involving price structure 3 and cost structure 3 (please see Tables 2–5). The product variety range is divided into four intervals with interval boundaries shown in Table 1. The result from this stage is that the third interval is selected as optimal, with 75 products and their corresponding optimal production quantities identified. The data are shown in Table 9 and the results are provided in Table 12, Table 13 (which also provide results from subsequent stages)."
Tables are attached below.
enter image description here
enter image description here
enter image description here
enter image description here
Product is total 100.
I have not decided the values of other parameters.
What I want to know is how to run the following iterative procedure?
When I run CPLEX, the objective function in the code keeps telling there is an error?
Thank you for your kind answers and you are the bests.
Regards,
forall(j in interval) sum(j in interval) a[j] == 1;
Looks bad since you used j twice !
For errors you should share dat file so that Other users could try
Related
I am trying to formulate a linear program that will assign different number of employees to start in different days. Each group of employees starting on a day will get two days off during the week. However, the schedule is unknown. For example, employees starting Monday can be off any two days in the week. Since the number that will start on day (i) is unknown and whether they will have a day off or not is unknown, I will have the product of two decision variables - one is an integer xi (employees starting on day i) and a binary variable yij (whether the employees starting on day i have a day off on day j).
I am done with formulation and here it is:
Decision variables 1: xi (employees starting on day i)
Decision variables 2: yij (1 if employees starting on day i are working on day j, or 0 if employees starting on day i are off on day j)
Objective function:
Minimize total employees-- sum (i in 1..7) xi
Subject to:
xi*yij >= Requiredj (the number of available workers on day j have to satisfy the demand on day j)
I am trying to code this on CPLEX but i dont know how to make xi*yij linear and write the code....can anyone please help me?
Thank you.
In How to with OPL How to multiply a decision variable by a boolean decision variable in CPLEX ?
// suppose we want b * x <= 7
dvar int x in 2..10;
dvar boolean b;
dvar int bx;
maximize x;
subject to
{
// Linearization
bx<=7;
2*b<=bx;
bx<=10*b;
bx<=x-2*(1-b);
bx>=x-10*(1-b);
// if we use CP we could write directly
// b*x<=7
// or rely on logical constraints within CPLEX
// (b==1) => (bx==x);
// (b==0) => (bx==0);
}
I am new to this platform CPLEX which means a beginner of programming.
I'm trying to write the code for CPLEX for below questions.
Question 1.
∑j (X^D(i,j,t) <= min(k^P(i,t),k^A(i,t) for all i, t
enter image description here
I tried to write the code like something below
forall(i in plants,t in years)
{
sum(j in products:j!)(X[i,j,t]) <= kP[i,t];
sum(j in products:j!)(X[i,j,t]) <= kA[i,t];
}
Is this right?
First, how do you write the code (sum of j, without having specific values)
Second, is there a way to express 'min' constraint in CPLEX?
The next question is how do you write the code of sum of four values.
It's the advanced version of the upper question.
cost_t=∑s,i,m,j(c^S(s,i,m,j,t)*X^S(s,i,m,j,t)+∑i,j,t(c(i,j,t)*X(i,j,t)+∑i,r,j,t(c^D(i,r,j,t)*X^D(i,r,j,t)
enter image description here
How do you write the sum part with four or three index?
Thank you for your kind answers and you are the bests.
Regards,
range plants=1..10;
range years=2021..2022;
range products=1..4;
dvar int+ X[plants][products][years];
int kP[i in plants,t in years]=i*t;
int kA[i in plants,t in years]=i*t+2;
dvar float cost;
subject to
{
forall(i in plants,t in years)
{
sum(j in products:j!=1)(X[i,j,t]) <= minl(kP[i,t],kA[i,t]);
}
cost==sum(i,i2 in plants,t in years,j in products:i!=j) (X[i][j][t]+X[i2][j][t]);
}
works fine
minl means minimum of a few values
I have a list of elements to be searched in a dataset of variable lengths. I have tried binary search and I found it is not always efficient when the objective is to search a list of elements.
I did the following study and conclude that if the number of elements to be searched is less than 5% of the data, binary search is efficient, other wise the Linear search is better.
Below are the details
Number of elements : 100000
Number of elements to be searched: 5000
Number of Iterations (Binary Search) =
log2 (N) x SearchCount=log2 (100000) x 5000=83048
Further increase in the number of search elements lead to more iterations than the linear search.
Any thoughts on this?
I am calling the below function only if the number elements to be searched is less than 5%.
private int SearchIndex(ref List<long> entitylist, ref long[] DataList, int i, int len, ref int listcount)
{
int Start = i;
int End = len-1;
int mid;
while (Start <= End)
{
mid = (Start + End) / 2;
long target = DataList[mid];
if (target == entitylist[listcount])
{
i = mid;
listcount++;
return i;
}
else
{
if (target < entitylist[listcount])
{
Start = mid + 1;
}
if (target > entitylist[listcount])
{
End = mid - 1;
}
}
}
listcount++;
return -1; //if the element in the list is not in the dataset
}
In the code I retun the index rather than the value because, I need to work with Index in the calling function. If i=-1, the calling function resets the value to the previous i and calls the function again with a new element to search.
In your problem you are looking for M values in an N long array, N > M, but M can be quite large.
Usually this can be approached as M independent binary searches (or even with the slight optimization of using the previous result as a starting point): you are going to O(M*log(N)).
However, using the fact that also the M values are sorted, you can find all of them in one pass, with linear search. In this case you are going to have your problem O(N). In fact this is better than O(M*log(N)) for M large.
But you have a third option: since M values are sorted, binary split M too, and every time you find it, you can limit the subsequent searches in the ranges on the left and on the right of the found index.
The first look-up is on all the N values, the second two on (average) N/2, than 4 on N/4 data,.... I think that this scale as O(log(M)*log(N)). Not sure of it, comments welcome!
However here is a test code - I have slightly modified your code, but without altering its functionality.
In case you have M=100000 and N=1000000, the "M binary search approach" takes about 1.8M iterations, that's more that the 1M needed to scan linearly the N values. But with what I suggest it takes just 272K iterations.
Even in case the M values are very "collapsed" (eg, they are consecutive), and the linear search is in the best condition (100K iterations would be enough to get all of them, see the comments in the code), the algorithm performs very well.
DISCLAIMER: Rather theoretical question here, not looking for a correct answere, just asking for some inspiration!
Consider this:
A function is called repetitively and returns integers based on seeds (the same seed returns the same integer). Your task is to find out which integer is returned most often. Easy enough, right?
But: You are not allowed to use arrays or fields to store return values of said function!
Example:
int mostFrequentNumber = 0;
int occurencesOfMostFrequentNumber = 0;
int iterations = 10000000;
for(int i = 0; i < iterations; i++)
{
int result = getNumberFromSeed(i);
int occurencesOfResult = magic();
if(occurencesOfResult > occurencesOfMostFrequentNumber)
{
mostFrequentNumber = result;
occurencesOfMostFrequentNumber = occurencesOfResult;
}
}
If getNumberFromSeed() returns 2,1,5,18,5,6 and 5 then mostFrequentNumber should be 5 and occurencesOfMostFrequentNumber should be 3 because 5 is returned 3 times.
I know this could easily be solved using a two-dimentional list to store results and occurences. But imagine for a minute that you can not use any kind of arrays, lists, dictionaries etc. (Maybe because the system that is running the code has such a limited memory, that you cannot store enough integers at once or because your prehistoric programming language has no concept of collections).
How would you find mostFrequentNumber and occurencesOfMostFrequentNumber? What does magic() do?? (Of cause you do not have to stick to the example code. Any ideas are welcome!)
EDIT: I should add that the integers returned by getNumber() should be calculated using a seed, so the same seed returns the same integer (i.e. int result = getNumber(5); this would always assign the same value to result)
Make an hypothesis: Assume that the distribution of integers is, e.g., Normal.
Start simple. Have two variables
. N the number of elements read so far
. M1 the average of said elements.
Initialize both variables to 0.
Every time you read a new value x update N to be N + 1 and M1 to be M1 + (x - M1)/N.
At the end M1 will equal the average of all values. If the distribution was Normal this value will have a high frequency.
Now improve the above. Add a third variable:
M2 the average of all (x - M1)^2 for all values of xread so far.
Initialize M2 to 0. Now get a small memory of say 10 elements or so. For every new value x that you read update N and M1 as above and M2 as:
M2 := M2 + (x - M1)^2 * (N - 1) / N
At every step M2 is the variance of the distribution and sqrt(M2) its standard deviation.
As you proceed remember the frequencies of only the values read so far whose distances to M1 are less than sqrt(M2). This requires the use of some additional array, however, the array will be very short compared to the high number of iterations you will run. This modification will allow you to guess better the most frequent value instead of simply answering the mean (or average) as above.
UPDATE
Given that this is about insights for inspiration there is plenty of room for considering and adapting the approach I've proposed to any particular situation. Here are some thoughts
When I say assume that the distribution is Normal you should think of it as: Given that the problem has no solution, let's see if there is some qualitative information I can use to decide what kind of distribution would the data have. Given that the algorithm is intended to find the most frequent number, it should be fine to assume that the distribution is not uniform. Let's try with Normal, LogNormal, etc. to see what can be found out (more on this below.)
If the game completely disallows the use of any array, then fine, keep track of only, say 10 numbers. This would allow you to count the occurrences of the 10 best candidates, which will give more confidence to your answer. In doing this choose your candidates around the theoretical most likely value according to the distribution of your hypothesis.
You cannot use arrays but perhaps you can read the sequence of numbers two or three times, not just once. In that case you can read it once to check whether you hypothesis about its distribution is good nor bad. For instance, if you compute not just the variance but the skewness and the kurtosis you will have more elements to check your hypothesis. For instance, if the first reading indicates that there is some bias, you could use a LogNormal distribution instead, etc.
Finally, in addition to providing the approximate answer you would be able to use the information collected during the reading to estimate an interval of confidence around your answer.
Alright, I found a decent solution myself:
int mostFrequentNumber = 0;
int occurencesOfMostFrequentNumber = 0;
int iterations = 10000000;
int maxNumber = -2147483647;
int minNumber = 2147483647;
//Step 1: Find the largest and smallest number that _can_ occur
for(int i = 0; i < iterations; i++)
{
int result = getNumberFromSeed(i);
if(result > maxNumber)
{
maxNumber = result;
}
if(result < minNumber)
{
minNumber = result;
}
}
//Step 2: for each possible number between minNumber and maxNumber, count occurences
for(int thisNumber = minNumber; thisNumber <= maxNumber; thisNumber++)
{
int occurenceOfThisNumber = 0;
for(int i = 0; i < iterations; i++)
{
int result = getNumberFromSeed(i);
if(result == thisNumber)
{
occurenceOfThisNumber++;
}
}
if(occurenceOfThisNumber > occurencesOfMostFrequentNumber)
{
occurencesOfMostFrequentNumber = occurenceOfThisNumber;
mostFrequentNumber = thisNumber;
}
}
I must admit, this may take a long time, depending on the smallest and largest possible. But it will work without using arrays.
I have generated the coefficients using FFTW, now I want to reconstruct the original data, but using only the first numCoefs coefficients rather than all of them. At the moment I'm using the below code, which is very slow:
for ( unsigned int i = 0; i < length; ++i )
{
double sum = 0;
for ( unsigned int j = 0; j < numCoefs; ++j )
{
sum += ( coefs[j][0] * cos( j * omega * i ) ) + ( coefs[j][1] * sin( j * omega * i ) );
}
data[i] = sum;
}
Is there a faster way?
A much simpler solution would be to zero the unwanted coefficients and then do an IFFT with FFTW. This will be a lot more efficient than doing an IDFT as above.
Note that you may get some artefacts in the time domain when you do this kind of thing - you're effectively multiplying by a step function in the frequency domain, which is equivalent to convolution with a sinc function in the time domain. To reduce the resulting "ringing" in the time domain you should use a window function to smooth out the transition between non-zero and zero coeffs.
If your numCoefs is anywhere near or greater than log(length), then an IFFT, which is O(n*log(n)) in computational complexity, will most likely be faster, as well as pre-optimized for you. Just zero all the bins except for the coefficients you want to keep, and make sure to also keep their negative frequency complex conjugates as well if you want a real result.
If your numCoefs is small relative to log(length), then other optimizations you could try include using sinf() and cosf() if you don't really need more than 6 digits of precision, and pre-calculating omega*i outside the inner loop (although your compiler should do be doing that for you unless you have the optimization setting low or off).