CPLEX cannot extract the expression - expression

program-EV Charge Scheduling
error
Description Resource Path Location Type
CPLEX(default) cannot extract expression: forall(i in 0..11, j in 0..4) if ( soc[i][j] > 0.7 ) pevch[i][j] == soc[i][j]*100000; else true. model.mod /singlebus 36:1-38:33 E:\Haripriya\cplexoutput\singlebus\model.mod OPL Problem Marker
Program
range t=0..11;
range n=0..4;
int j=0;
//forcasted load at 0..4
float pl[t]=[10000000,7000000,9000000,6000000,12000000,6000000,4000000,15000000,9000000,12000000,6000000,8000000];
//soc of ev at 0..11
float soc[n][t] = [[0.2,0.1,0.9,0.7,0.6,0.9,0.2,0.6,0.8,0.75,0.85,0.95],
[0.2,0.1,0.9,0.7,0.6,0.9,0.2,0.6,0.8,0.75,0.85,0.95],
[0.2,0.1,0.9,0.7,0.6,0.9,0.2,0.6,0.8,0.75,0.85,0.95],
[0.2,0.1,0.9,0.7,0.6,0.9,0.2,0.6,0.8,0.75,0.85,0.95],
[0.2,0.1,0.9,0.7,0.6,0.9,0.2,0.6,0.8,0.75,0.85,0.95]];
//generation
float pg[t]=[10000000,9500000,8500000,11000000,600000,7500000,10000000,9500000,8500000,11000000,600000,7500000];
//target load at 0..11
float pt[t]=[10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000];
//bus voltage at 0..11
float v[t]=[5,4,3,2,1,5,4,3,2,1,5,4];
//bus voltage at
// target bus voltage at 0..11
float vt[t]=[4,4,4,4,4,4,4,4,4,4,4,4];
//decision variable charging power ev
dvar float pevch[t][n];
//decision variable discharging power of ev
dvar float pevdis[t][n];
//objective function
minimize sum(i in t)((pt[i]-pl[i])+sum(j in n)-pevch[i][j]+sum(j in n)pevdis[i][j]);
subject to
{
forall(i in t, j in n)
if(soc[i][j]>0.7)
pevch[i][j]==soc[i][j]*100000;
}
Solution to tackle this error

You may have mixed i and j
If you write
subject to
{
forall(i in t, j in n)
if(soc[j][i]>0.7)
pevch[i][j]==soc[j][i]*100000;
}
then your model works fine

Related

CPLEX objective function declaration

i'm a newbie using CPLEX Studio IDE 12.8.0 (built on eclipse), i'm trying to write my 1st model. i watched youtube videos and started coding an assignment workforce problem according to their tasks proficiency into 5 teams, i have 3 questions :
1-how to get rid of the errors,in the declaration of the objective function
// parameters
int n=...; //number of operators
int m=...; //number of tasks
int w=...; //number of teams
range operateurs= 1..n; //intervalle des opérateurs
range taches = 1..m; // varier les taches
range equipes = 1..w; // varier les équipes
int notation[operateurs][taches];
dvar boolean affectation[operateurs][taches][equipes];
**Maximize Sum (i in operateurs, j in taches, k in equipes) affectation[i][j][k]*notation[i][j];**
subject to {
forall (i in operateurs,j in taches)
unicite_aff_eq:
sum (k in equipes) affectation[i][j][k]== 1;
forall (k in equipes,i in operateurs)
unicite_aff_tache:
sum (j in taches)affectation[i][j][k]== 1;
forall (k in equipes,j in taches)
contenir_opp:
sum (i in operateurs) affectation[i][j][k]>= 1;
forall (i in operateurs,j in taches,k in equipes)
competence:
sum (i in operateurs,j in taches)
affectation[i][j][k]*notation[i][j]>=6;
}
2- can you give me a hand with modeling this constraint:
//constraint that i did not know to modele it
if (affectation[i][j][k]==1)
//at least there is notation[i][j]>=3;
here's the .dat file
n=56;
m=6;
w=5;
SheetConnection my_sheet ("affectation.xlsx");
notation from sheetread(my_sheet,"notation");
3-how can i make the declaration to import the notation table from excel.
i will be grateful if you can help me guys
thanks in advance
her's the error message that i get on the objective function declaration line (Maximize Sum (i in operateurs, j in taches, k in equipes) affectation[i][j][k]*notation[i][j];** )
//Description Ressource Chemin d'accès Emplacement Type
syntax error, unexpected (identifier), expecting ';' affectation-Opérateur.mod /affectation-Opérateur 20:59-67 C:/Users/ToualbiaMohamed Lies/opl/affectation-Opérateur/affectation-Opérateur.mod Problème de structure du modèle OPL//
with notation underlined in red.
so i wonder what i did wrong
and if it's possible tu multiply a matrix by a cube
(should make a declaration about the number of teams in the .dat file)
for 1) and 2) the following code works
// parameters
int n=2; //number of operators
int m=4; //number of tasks
int w=5; //number of teams
range operateurs= 1..n; //intervalle des opérateurs
range taches = 1..m; // varier les taches
range equipes = 1..w; // varier les équipes
int notation[o in operateurs][t in taches]=o*t mod 2;
dvar boolean affectation[operateurs][taches][equipes];
maximize sum (i in operateurs, j in taches, k in equipes) affectation[i][j][k]*notation[i][j];
subject to {
forall (i in operateurs,j in taches)
unicite_aff_eq:
sum (k in equipes) affectation[i][j][k]== 1;
forall (k in equipes,i in operateurs)
unicite_aff_tache:
sum (j in taches)affectation[i][j][k]== 1;
forall (k in equipes,j in taches)
contenir_opp:
sum (i in operateurs) affectation[i][j][k]>= 1;
forall (i in operateurs,j in taches,k in equipes)
competence:
sum (i in operateurs,j in taches)
affectation[i][j][k]*notation[i][j]>=6;
forall (i in operateurs,j in taches,k in equipes)
(affectation[i][j][k]==1)=> (notation[i][j]>=3);
}
for 3) in Making Decision optimization simple you could have a look at Excel spreadsheets
.mod
tuple param
{
int nbKids;
}
{param} params=...;
assert card(params)==1;
int nbKids=first(params).nbKids;
// a tuple is like a struct in C, a class in C++ or a record in Pascal
tuple bus
{
key int nbSeats;
float cost;
}
// This is a tuple set
{bus} buses=...;
// asserts help make sure data is fine
assert forall(b in buses) b.nbSeats>0;
assert forall(b in buses) b.cost>0;
// decision variable array
dvar int+ nbBus[buses];
// objective
minimize
sum(b in buses) b.cost*nbBus[b];
// constraints
subject to
{
sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;
}
tuple result
{
key int nbSeats;
int nbBuses;
}
{result} results={<b.nbSeats,nbBus[b]> | b in buses};
.dat
SheetConnection s("zoo.xlsx");
params from SheetRead(s,"params!A2");
buses from SheetRead(s,"buses!A2:B3");
results to SheetWrite(s,"buses!E2:F3");

Battery scheduling with CP optimizer

I'm a student new at CP optimizer.
I want to make battery charging/discharging scheduling in CP.
So, I want to know how to charge or discharge at each sept.
using CP;
int numEVs = ...;
range EVs = 0..numEVs-1;
int time = ...;
range times = 0..time-1;
int cost[times] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
float min_soc[EVs] = [0.4,0.4,0.4,0.4,0.4];
float max_soc[EVs] = [0.9,0.9,0.9,0.9,0.9];
float Soc[EVs] = [0.4, 0.5, 0.6, 0.7, 0.8];
int k[times,EVs];
tuple EVs2 {
key int id;
int Cpower[times];
int Dpower[times];
}
//float delSm[EVs] = Soc[EVs] - min_soc[EVs];
//float delSp[EVs] = Soc[EVs] - max_soc[EVs];
dvar interval t[i in times] optional size 1;
dvar int Pcmax[times, EVs]; // why I can't use float.
dvar int Pdmax[times, EVs];
//dvar int k[times,EVs] in 0..1;
dexpr float Cost = sum(t, j in EVs) (k[t,j]*cost[t]*Pcmax[t,j] - (1-k[t,j])*cost[t]*Pdmax[t,j]);
minimize Cost; // minimize charging/discharging price
subject to {
forall(t, j in EVs)
k[t,j]*Pcmax[t,j] - (1-k[t,j])*Pdmax[t,j] >= Soc[j]-min_soc[j] && k[t,j]*Pcmax[t,j] - (1-k[t,j])*Pdmax[t,j] <= Soc[j]-max_soc[j];
// each EV's battery state of charge must less or bigger than limits.
forall(t, j in EVs)
{
Pdmax[t][j] >=0;
Pdmax[t][j] <=10;
Pcmax[t][j] >=0;
Pcmax[t][j] <=8;
}
this is my code, but not working help me plz.
Copying a short version of the answer this question got at https://www.ibm.com/developerworks/community/forums/html/topic?id=5ffa03c4-68ed-44d4-b663-998592e22dec, where it was cross posted: The model is infeasible. You can use the conflict refiner to figure out which constraints render the problem infeasible and then fix them.

Solving a "nearly" Hermitian linear system

So I need to solve for the linear system (A + i * mu * I) x = b, where A is dense Hermitian matrix (6x6 complex numbers), mu is a real scalar, and I is identity matrix.
Obviously if mu=0, I should just use Cholesky and be done with it. With non-zero mu though, the matrix ceases to be Hermitian and Cholesky fails.
Possible solutions:
Solve normal operator using Cholesky and multiply by the conjugate
Solve directly using LU decomposition
This is in a time-critical performance routine, where I need the most efficient method. Any thoughts on the optimum approach, or if there is a specific method for solving the above shifted Hermitian system?
This is to be deployed in a CUDA kernel, where I'll be solving many linear systems in parallel, e.g., one per thread. This means that I need a solution that minimizes thread divergence. Given the small system size, pivoting can be ignored without too much issue: this removes a possible source of thread divergence. I've already implemented an in-place Cholesky normal method, and while it's working ok, the performance isn't great in double precision.
I can't vouch for the stability of the method below, but if your matrix is reasonably well conditioned, it might be worth a try.
We want to solve
A*X = B
If we pick out the first row and column, say
A = ( a y )
( z A_ )
X = ( x )
( X_)
B = ( b )
( B_ )
The requirement is
a*x + y*X_ = b
z*x + A_*X_ = B_
so
x = (b - y*X_ )/a
(A_ - zy/a) * X_ = B_ - (b/a)z
The solution goes in two stages. First use the second equation to transform A and b, then use the second to form the solution x.
In C:
static void nhsol( int dim, complx* A, complx* B, complx* X)
{
int i, j, k;
complx a, fb, fa;
complx* z;
complx* acol;
// update A and B
for( i=0; i<dim; ++i)
{ z = A + i*dim;
a = z[i];
// update B
fb = B[i]/a;
for( j=i+1; j<dim; ++j)
{ B[j] -= fb*z[j];
}
// update A
for( k=i+1; k<dim; ++k)
{ acol = A + k*dim;
fa = acol[i]/a;
for( j=i+1; j<dim; ++j)
{ acol[j] -= fa*z[j];
}
}
}
// compute x
i = dim-1;
X[i] = B[i] / A[i+dim*i];
while( --i>=0)
{
complx s = B[i];
for( j=i+1; j<dim; ++j)
{ s -= A[i+j*dim]*X[j];
}
X[i] = s/A[i+i*dim];
}
}
where
typedef _Complex double complx;
If code space is not at a premuim it might be worth unrolling the loops. Personally I would do this by writing a program whose sole job was to write the code.

TERCOM algorithm - Changing from single thread to multiple threads in CUDA

I'm currently working on porting a TERCOM algorithm from using only 1 thread to use multiple threads. Briefly explained , the TERCOM algorithm receives 5 measurements and the heading, and compare this measurements to a prestored map. The algorithm will choose the best match, i.e. lowest Mean Absolute Difference (MAD), and return the position.
The code is working perfectly with one thread and for-loops, but when I try to use multiple threads and blocks it returns the wrong answer. It seems like the multithread version doesn't "run through" the calculation in the same way as the singlethread versjon. Does anyone know what I am doing wrong?
Here's the code using for-loops
__global__ void kernel (int m, int n, int h, int N, float *f, float heading, float *measurements)
{
//Without threads
float pos[2]={0};
float theta=heading*(PI/180);
float MAD=0;
// Calculate how much to move in x and y direction
float offset_x = h*cos(theta);
float offset_y = -h*sin(theta);
float min=100000; //Some High value
//Calculate Mean Absolute Difference
for(float row=0;row<m;row++)
{
for(float col=0;col<n;col++)
{
for(float g=0; g<N; g++)
{
f[(int)g] = tex2D (tex, col+(g-2)*offset_x+0.5f, row+(g-2)*offset_y+0.5f);
MAD += abs(measurements[(int)g]-f[(int)g]);
}
if(MAD<min)
{
min=MAD;
pos[0]=col;
pos[1]=row;
}
MAD=0; //Reset MAD
}
}
f[0]=min;
f[1]=pos[0];
f[2]=pos[1];
}
This is my attempt to use multiple threads
__global__ void kernel (int m, int n, int h, int N, float *f, float heading, float *measurements)
{
// With threads
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int idy = blockIdx.y * blockDim.y + threadIdx.y;
float pos[2]={0};
float theta=heading*(PI/180);
float MAD=0;
// Calculate how much to move in x and y direction
float offset_x = h*cos(theta);
float offset_y = -h*sin(theta);
float min=100000; //Some High value
if(idx < n && idy < m)
{
for(float g=0; g<N; g++)
{
f[(int)g] = tex2D (tex, idx+(g-2)*offset_x+0.5f, idy+(g-2)*offset_y+0.5f);
MAD += abs(measurements[(int)g]-f[(int)g]);
}
if(MAD<min)
{
min=MAD;
pos[0]=idx;
pos[1]=idy;
}
MAD=0; //Reset MAD
}
f[0]=min;
f[1]=pos[0];
f[2]=pos[1];
}
To launch the kernel
dim3 dimBlock( 16,16 );
dim3 dimGrid;
dimGrid.x = (n + dimBlock.x - 1)/dimBlock.x;
dimGrid.y = (m + dimBlock.y - 1)/dimBlock.y;
kernel <<< dimGrid,dimBlock >>> (m, n, h, N, dev_results, heading, dev_measurements);
The basic problem here is that you have a memory race in the code, centered around the use of f as both some sort of thread local scratch space and an output variable. Every concurrent thread will be trying to write values into the same locations in f simultaneously, which will produce undefined behaviour.
As best as I can tell, the use of f as scratch space isn't even necessary at all and the main computational section of the kernel could be written as something like:
if(idx < n && idy < m)
{
for(float g=0; g<N; g++)
{
float fval = tex2D (tex, idx+(g-2)*offset_x+0.5f, idy+(g-2)*offset_y+0.5f);
MAD += abs(measurements[(int)g]-fval);
}
min=MAD;
pos[0]=idx;
pos[1]=idy;
}
[disclaimer: written in browser, use at own risk]
At the end of that calculation, each thread has its own values of min and pos. At a minimum these must be stored in unique global memory (ie. the output must have enough space for each thread result). You will then need to perform some sort of reduction operation to obtain the global minimum from the set of thread local values. That could be in the host, or in the device code, or some combination of the two. There is a lot of code already available for CUDA parallel reductions which you should be able to find by searching and/or looking in the examples supplied with the CUDA toolkit. It should be trivial to adapt them to your specify case where you need to retain the position along with the minimum value.

Data structures and algorithms for adaptive "uniform" mesh?

I need a data structure for storing float values at an uniformly sampled 3D mesh:
x = x0 + ix*dx where 0 <= ix < nx
y = y0 + iy*dy where 0 <= iy < ny
z = z0 + iz*dz where 0 <= iz < nz
Up to now I have used my Array class:
Array3D<float> A(nx, ny,nz);
A(0,0,0) = 0.0f; // ix = iy = iz = 0
Internally it stores the float values as an 1D array with nx * ny * nz elements.
However now I need to represent an mesh with more values than I have RAM,
e.g. nx = ny = nz = 2000.
I think many neighbour nodes in such an mesh may have similar values so I was thinking if there was some simple way that I could "coarsen" the mesh adaptively.
For instance if the 8 (ix,iy,iz) nodes of an cell in this mesh have values that are less than 5% apart; they are "removed" and replaced by just one value; the mean of the 8 values.
How could I implement such a data structure in a simple and efficient way?
EDIT:
thanks Ante for suggesting lossy compression. I think this could work the following way:
#define BLOCK_SIZE 64
struct CompressedArray3D {
CompressedArray3D(int ni, int nj, int nk) {
NI = ni/BLOCK_SIZE + 1;
NJ = nj/BLOCK_SIZE + 1;
NK = nk/BLOCK_SIZE + 1;
blocks = new float*[NI*NJ*NK];
compressedSize = new unsigned int[NI*NJ*NK];
}
void setBlock(int I, int J, int K, float values[BLOCK_SIZE][BLOCK_SIZE][BLOCK_SIZE]) {
unsigned int csize;
blocks[I*NJ*NK + J*NK + K] = compress(values, csize);
compressedSize[I*NJ*NK + J*NK + K] = csize;
}
float getValue(int i, int j, int k) {
int I = i/BLOCK_SIZE;
int J = j/BLOCK_SIZE;
int K = k/BLOCK_SIZE;
int ii = i - I*BLOCK_SIZE;
int jj = j - J*BLOCK_SIZE;
int kk = k - K*BLOCK_SIZE;
float *compressedBlock = blocks[I*NJ*NK + J*NK + K];
unsigned int csize = compressedSize[I*NJ*NK + J*NK + K];
float values[BLOCK_SIZE][BLOCK_SIZE][BLOCK_SIZE];
decompress(compressedBlock, csize, values);
return values[ii][jj][kk];
}
// number of blocks:
int NI, NJ, NK;
// number of samples:
int ni, nj, nk;
float** blocks;
unsigned int* compressedSize;
};
For this to be useful I need a lossy compression that is:
extremely fast, also on small datasets (e.g. 64x64x64)
compress quite hard > 3x, never mind if it looses quite a bit of info.
Any good candidates?
It sounds like you're looking for a LOD (level of detail) adaptive mesh. It's a recurring theme in video games and terrain simulation.
For terrain, see here: http://vterrain.org/LOD/Papers/ -- look for the ROAM video which is IIRC not only adaptive by distance, but also by view direction.
For non-terrain entities, there is a huge body of work (here's one example: Generic Adaptive Mesh Refinement).
I would suggest to use OctoMap to handle large 3D data.
And to extend it as shown here to handle geometrical properties.

Resources