Operator not available for dvar float+ * float[][range] - optimization

int NbPeriods = ...; range Periods = 1..NbPeriods;
int NbParts = ...; range Parts = 1..NbParts;
int NbSuppliers = ...; range Suppliers = 1..NbSuppliers;
int NbProcesses = ...; range Processes = 1..NbProcesses;
int NbPS[1..NbParts, 1..NbProcesses*NbSuppliers] = ...;
float Demand[Parts][Periods] = ...;
float BOH[Parts] = ...;
float Capacity[Suppliers][Processes] = ...;
float ProcessMapping[s in 1..NbSuppliers, pr in 1..NbProcesses, p in 1..NbParts] = NbPS[p, pr+NbProcesses*(s-1)];
float Price[Parts][Suppliers] = ...;
dvar float+ Supply[1..NbParts, 1..NbPeriods*NbSuppliers];
dvar float+ EOH[Parts][Periods];
dvar float+ Util[1..NbProcesses, 1..NbPeriods*NbSuppliers];
minimize
sum( t in Periods ) DOIDelta[t] ;
subject to {
forall(p in Parts)
EOH[p][0] == BOH[p];
forall(p in Parts)
forall( t in Periods)
EOH[p][t] == EOH[p][t-1] + sum(s in Suppliers) Supply[p,t+NbPeriods*(s-1)] ;
forall(t in Periods)
forall(pr in Processes)
forall(s in Suppliers)
Util[pr,t+NbPeriods*(s-1)] == sum(p in Parts) (Supply[p,t+NbPeriods*(s-1)] * ProcessMapping[p, pr+NbProcesses*(s-1)] );
}
The error message is for the last Util line: Operator not available for dvar float+ * float[][range]. I have checked other posts on this topic and the issues are on parenthesis. Even adding the parenthesis the error remains. Appreciate your help.

processMapping is a 3D array not a 2D array
forall(t in Periods)
forall(pr in Processes)
forall(s in Suppliers)
Util[pr,t+NbPeriods*(s-1)] == sum(p in Parts) (Supply[p,t+NbPeriods*(s-1) * ftoi(ProcessMapping[s,p, pr+NbProcesses*(s-1)])] );
will work better

Related

cplex error: cos float doesnot exists i.e,showing error while typing cos and sin functions

error correction. I have to include cos and sin functions in my model.But it is showing error.I have tried the expression Math.cos and Opl.cos But both won't work. The error is after the forall statement,and iam facing this error after including the cos function.
float c1[0..3]=[50,0,0,50];
float c2[0..3]=[351,0,0,389];
float c3[0..3]=[44.6,0,0,40.6];
float pd[0..3]=[50,170,200,80];
float qd[0..3]=[10,20,30,40];
float V[0..3]=[1.0,1.0,1.0,1.0];
float del[0..3]=[0,0,0,0];
/*float pg[1..4]=[10,0,0,10];*/
float p[0..3];
float q[0..3];
int i=0;
float G[0..3][0..3]=[ [5.724138, -1.724138,0,-4],
[-1.724138,4.224138,-2.5,0],
[0,-2.5,4.386792,-1.886792],
[-4,0,-1.886792,5.886792]];
float B[0..3][0..3]=[ [-12.31034,4.310345,0,8],
[4.310345,-11.810340,7.5,0],
[0,7.5,-14.10377,6.603774],
[8,0,6.603774,-14.603770]];
dvar float+ pg[0..3];
dvar float+ Qg[0..3];
minimize sum(i in 0..3)(c1[i]*pg[i]^2 + c2[i]*pg[i] + c3[i]);
subject to
{forall(i in 0..3)
p[i]==V[i]*(sum(j in 0..3)(V[j]*(G[i][j]*cos(del[i]-del[j]))));
p[i]-pg[i]+pd[i]==0;
forall(i in 0..3)
q[i]==V[i]*(sum(j in 0..3)(V[j]*(G[i][j])));
q[i]-Qg[i]+qd[i]==0;
//forall(i in 0..3)
// pg[i]<=30;
}
cos is not linear so you cannot use cos in a MIP model within CPLEX.
If you need non linear function you could either use:
CPOptimizer within CPLEX
approximate with a piecewise linear function
But in your case you use cos of data so you can write the following model that works fine:
float c1[0..3]=[50,0,0,50];
float c2[0..3]=[351,0,0,389];
float c3[0..3]=[44.6,0,0,40.6];
float pd[0..3]=[50,170,200,80];
float qd[0..3]=[10,20,30,40];
float V[0..3]=[1.0,1.0,1.0,1.0];
float del[0..3]=[0,0,0,0];
/*float pg[1..4]=[10,0,0,10];*/
float p[0..3];
float q[0..3];
int i=0;
float deltacos[0..3][0..3];
range r=0..3;
execute fill_deltacos
{
for(var i in r) for (var j in r) deltacos[i][j]=Math.cos(del[i]-del[j]);
}
float G[0..3][0..3]=[ [5.724138, -1.724138,0,-4],
[-1.724138,4.224138,-2.5,0],
[0,-2.5,4.386792,-1.886792],
[-4,0,-1.886792,5.886792]];
float B[0..3][0..3]=[ [-12.31034,4.310345,0,8],
[4.310345,-11.810340,7.5,0],
[0,7.5,-14.10377,6.603774],
[8,0,6.603774,-14.603770]];
dvar float+ pg[0..3];
dvar float+ Qg[0..3];
minimize sum(i in 0..3)(c1[i]*pg[i]^2 + c2[i]*pg[i] + c3[i]);
subject to
{forall(i in 0..3)
p[i]==V[i]*(sum(j in 0..3)(V[j]*(G[i][j]*deltacos[i][j])));
p[i]-pg[i]+pd[i]==0;
forall(i in 0..3)
q[i]==V[i]*(sum(j in 0..3)(V[j]*(G[i][j])));
q[i]-Qg[i]+qd[i]==0;
//forall(i in 0..3)
// pg[i]<=30;
}

Cplex optimization--"No Value" for my decision variable

I am new to Cplex optimization.
I am trying to implement an optimization problem with some scenarios. it is a two-stage stochastic model with 5 scenarios and the probabilty of occurrence the scenarios.
I wrote my model with 5 scenarios, parameters, and variables, and constraints. I get the following message "No Value" for my decision variable. I think my model does not work and I do not know what should I change in my Codes?. Is there somebody to help me? Thank you very much.
--Parameter--
int NbWarehause=3;
int NbRegion=138;
int NbSchool=631;
int NbScenario=5;
range Warehouse=1..NbWarehause;
range Region=1..NbRegion;
range School=1..NbSchool;
range Scenario=1..NbScenario;
int TravelDistanceWarehouseRegion[Warehouse][Region]=...;
int CapacitySchool[School] =...;
int ScenarioDemandMatrix[Scenario][Region]=...;
int Fixedcost1 = 14232;
float Transportcost1perkm=1.40;
int Fixedcost2 = 14232;
float Transportcost2perkm=3;
int Unusedcostperitem=50;
int Depriviationcost[Region]=...;
int Penaltycost=100;
float ProbabilityScenario[Scenario]=...;
--Decision variables---
dvar boolean open1[School][Region];
dvar int Allocated1[School][Region];
dvar boolean open2[School][Region];
dvar int Allocated2[School][Region];
dvar int UnusedInventory[School][Region];
dvar int LateSatisfiedDemand[Region];
dvar int UnSatisfiedDemand[Region];
--Objective function--
minimize --the first stage--
sum(j in School, r in Region) Fixedcost1 * open1[j][r] +
sum( j in School, w in Warehouse, r in Region) Allocated1[j][r] *
TravelDistanceWarehouseRegion[w][r]*Transportcost1perkm +
--the second stage--
sum(s in Scenario) ProbabilityScenario[s]*(
sum(j in School,r in Region)Fixedcost2 *open2[j][r]
+sum( j in School, w in Warehouse,r in Region) Allocated2[j]
[r]*TravelDistanceWarehouseRegion[w][r]*Transportcost2perkm
+sum( j in School,r in Region)UnusedInventory[j][r]*Unusedcostperitem
+sum(r in Region) Depriviationcost[r]*LateSatisfiedDemand[r]+
sum(r in Region)UnSatisfiedDemand[r]*Penaltycost );
--Constraint--
subject to
{
//C1: capacity of each school in its region//
forall (r in Region ) sum (j in School) (Allocated1[j]
[r]+Allocated2[j] [r]+UnusedInventory[j][r])== sum (j in
School)CapacitySchool[j];
//C2: Demand of each region //
forall (s in Scenario,r in Region) sum (j in School)(Allocated1[j]
[r]+Allocated2[j][r])+LateSatisfiedDemand[r]+UnSatisfiedDemand[r] ==
ScenarioDemandMatrix[s][r];
//C3: open a school maximal one time //
sum (j in School,r in Region ) (open1[j][r]+open2[j][r]) <= 1;
//C4: school can not supply more than its capacity in the second
stage I dont know how do I write under scenario //
forall (j in School,r in Region)Allocated2[j][r]<=CapacitySchool[j] -
Allocated1[j][r]*(open1[j][r]+open2[j][r]);
//C5: Sum of all probability is equal 1
sum (s in Scenario)ProbabilityScenario[s]==1;
// C6: Nonnegative Constraint
forall (r in Region ,j in School) Allocated1[j][r]>=0;
forall (r in Region ,j in School)Allocated2[j][r]>=0;
forall (r in Region ,j in School)UnusedInventory[j][r]>=0;
forall (r in Region)LateSatisfiedDemand[r]>=0;
forall (r in Region) UnSatisfiedDemand[r]>=0;
}
I guess your model is not feasible.
To understand why you could name your constraints and then CPLEX will provide you with some relaxations and conclicts.
As a start you could change
//C3: open a school maximal one time //
sum (j in School,r in Region ) (open1[j][r]+open2[j][r]) <= 1;
into
//C3: open a school maximal one time //
C3:sum (j in School,r in Region ) (open1[j][r]+open2[j][r]) <= 1;
And then rely on
https://www.ibm.com/support/pages/display-full-indices-or-real-map-item-name-variables-and-constraints
to get the real indexes

LP / MILP formulation with OR logic

I'm solving a LP / MILP problem using ILOG CPLEX.
int n = ...;
range time =1..n;
dvar float+ c[time] in 0..0.3;
dvar float+ d[time] in 0..0.3;
dvar float+ x[time];
int beta[time]=...;
float pc[time]=...;
float pd[time]=...;
//Expressions
dexpr float funtion = sum(t in time) (d[t]*pd[t]-c[t]*pc[t]);
//Model
maximize function;
subject to {
x[1] == 0.5;
c[1] == 0;
d[1] == 0;
forall(t in time)
const1:
x[t] <= 1;
forall(t in time: t!=1)
const2:
(x[t] == x[t-1] + c[t] - d[t]);
forall(t in time: t!=1)
const3:
( d[t] <= 0) || (c[t] <= 0);
As you can see I've forced c[t] and d[t] to never be bigger than 0 at the same time with "const3".
My question is, how would this constraint be represented in a LP/MILP mathematical formulation?
Is adding this new variable enough? :
y[t]≤c[t]+d[t]
y[t]≥c[t]
y[t]≥d[t]
0≤y[t]≤M (M is the maximum value of both c or d)
As far as I can tell, the constraints you suggested would allow this setting:
c[t] = 0.1
d[t] = 0.1
y[t] = 0.2
Which has c and d different from 0 simultaneously.
I can see these options to formulate your condition without logical constraints:
1) Use an SOS constraint that contains just c[t] and d[t]. By definition of SOS only one of the two can be non-zero in any feasible solution.
2) Use a boolean variable y[t] and add constraints
c[t] <= M * y[t]
d[t] <= M * (1 - y[t])
3) Again, use boolean y[t] and then indicator constraints
(y[t] == 0) => (c[t] == 0);
(y[t] == 1) => (d[t] == 0);
4) You can just state c[t] * d[t] == 0 but that will make the model non-linear.
In any case, a solver will probably be able to reduce your original formulation to either 2 or 3. So reformulating the constraint may not make things faster but only more obscure.

OpenCL kernel function crash

I have written a code in OpenCL in which I am not using local (shared) memory. My code crashes during execution and gives error -5. The error goes away when I replace global memory access to cvt_img buffer (in the middle of the code) with some constant values.
I do not understand why this happens, becuase I prevent accessing to out-of-the-scope memory locations using an if statement.
This code is part of a 3D pipeline, but right now, I have seperated it from my main application, and have put it in a seperate project in which all of the buffers are initialized randomly.
The size of the grid (in terms of number of threads) is the same as size of the image (img_size.x, img_size.y) and size of the block is (16, 16). The application is running for 15 images.
void compute_cost_volume(
global float3 *cvt_img,
global float8 *spixl_map,
global float *disp_level,
global int *view_subset,
global int *subset_num,
int array_width, int2 map_size,
int2 img_size, float bl_ratio,
int sp_size, int num_disp, float2 step,
int x, int y, int z, int view_count
)
{
barrier(CLK_GLOBAL_MEM_FENCE);
int idx = map_size.x * map_size.y * z + map_size.x * y + x;
float8 spixl = spixl_map[idx];
float2 center = spixl.s12;
int2 camIdx = (int2)(z % array_width, z / array_width);
float cost_est = 1000000.0, disp_est = 0.0;
for (int dl = 0 ; dl < num_disp ; dl++)
{
float d = disp_level[dl];
float min_val = 1000000.0;
for (int n = 0 ; n < subset_num[z] ; n++)
{
int view = view_subset[n];
int2 viewIdx = (int2)(view % array_width, view / array_width);
float val = 0.0;
for (int i = -2 ; i <= 2 ; i++) for (int j = -2 ; j <= 2 ; j++)
{
//int2 xy_ref = (int2)(center.x - 2*step.x + i*step.x, center.y - 2*step.y + j*step.y);
int2 xy_ref = (int2)(center.x + i*step.x, center.y + j*step.y);
int2 xy_proj = (int2)((int)(xy_ref.x - d*(viewIdx.x - camIdx.x)), (int)(xy_ref.y - bl_ratio*d*(viewIdx.y - camIdx.y) ) );
if (xy_ref.x >= 0 && xy_ref.y >= 0 && xy_proj.x >= 0 && xy_proj.y >= 0 && xy_ref.x < img_size.x && xy_ref.y < img_size.y && xy_proj.x < img_size.x && xy_proj.y < img_size.y)
{
float3 color_ref = cvt_img[img_size.x*img_size.y*z + img_size.x*xy_ref.y + xy_ref.x];
float3 color_proj = cvt_img[img_size.x*img_size.y*view + img_size.x*xy_proj.y + xy_proj.x];
val += fabs(color_ref.x - color_proj.x) + fabs(color_ref.y - color_proj.y) + fabs(color_ref.z - color_proj.z);
}
else
val += 30;
}
if (val < min_val)
min_val = val;
}
if (min_val < cost_est)
{
cost_est = min_val;
disp_est = d;
}
}
spixl_map[idx].s7 = disp_est;
}
kernel void initial_depth_estimation(
global float3 *cvt_img,
global float8 *spixl_map,
global float *disp_level,
int array_width, int2 map_size,
int2 img_size, float bl_ratio,
int sp_size, int disp_num,
global int *view_subset, global int *subset_num
)
{
int x = get_global_id(0);
int y = get_global_id(1);
if (x >= map_size.x || y >= map_size.y)
return;
//float2 step = (float2)(1, 1);
for (int z = 0 ; z < 15 ; z++){
int idx = map_size.x*map_size.y*z + map_size.x*y + x;
// Set The Bounding Box
float2 step = (float2)(1.0, 1.0);
compute_cost_volume(cvt_img, spixl_map, disp_level, view_subset, subset_num,
array_width, map_size, img_size, bl_ratio, sp_size, disp_num, step, x, y, z, 15);
barrier(CLK_LOCAL_MEM_FENCE);
}
}
From the documentation
https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/vectorDataTypes.html
" The vector data type is defined with the type name i.e. char, uchar, short, ushort, int, uint, float, long, and ulong followed by a literal value n that defines the number of elements in the vector. Supported values of n are 2, 4, 8, and 16. "
Therefore, there is no float3, maybe you can try to use float4 and make the last element zero?
Also, assuming that float3 existed, this line of code
float3 color_proj = cvt_img[img_size.x*img_size.y*view + img_size.x*xy_proj.y + xy_proj.x];
does not do what you want, this will produce ONE value that cannot be assigned to vector, you should have used something like
float3 color_proj = (float3) cvt_img[img_size.x*img_size.y*view + img_size.x*xy_proj.y + xy_proj.x];
this would copy the one value returned by the cvt_img[...] to 3 vector elements.

2nd order IIR filter, coefficients for a butterworth bandpass (EQ)?

Important update: I already figured out the answers and put them in this simple open-source library: http://bartolsthoorn.github.com/NVDSP/ Check it out, it will probably save you quite some time if you're having trouble with audio filters in IOS!
^
I have created a (realtime) audio buffer (float *data) that holds a few sin(theta) waves with different frequencies.
The code below shows how I created my buffer, and I've tried to do a bandpass filter but it just turns the signals to noise/blips:
// Multiple signal generator
__block float *phases = nil;
[audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
{
float samplingRate = audioManager.samplingRate;
NSUInteger activeSignalCount = [tones count];
// Initialize phases
if (phases == nil) {
phases = new float[10];
for(int z = 0; z <= 10; z++) {
phases[z] = 0.0;
}
}
// Multiple signals
NSEnumerator * enumerator = [tones objectEnumerator];
id frequency;
UInt32 c = 0;
while(frequency = [enumerator nextObject])
{
for (int i=0; i < numFrames; ++i)
{
for (int iChannel = 0; iChannel < numChannels; ++iChannel)
{
float theta = phases[c] * M_PI * 2;
if (c == 0) {
data[i*numChannels + iChannel] = sin(theta);
} else {
data[i*numChannels + iChannel] = data[i*numChannels + iChannel] + sin(theta);
}
}
phases[c] += 1.0 / (samplingRate / [frequency floatValue]);
if (phases[c] > 1.0) phases[c] = -1;
}
c++;
}
// Normalize data with active signal count
float signalMulti = 1.0 / (float(activeSignalCount) * (sqrt(2.0)));
vDSP_vsmul(data, 1, &signalMulti, data, 1, numFrames*numChannels);
// Apply master volume
float volume = masterVolumeSlider.value;
vDSP_vsmul(data, 1, &volume, data, 1, numFrames*numChannels);
if (fxSwitch.isOn) {
// H(s) = (s/Q) / (s^2 + s/Q + 1)
// http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
// BW 2.0 Q 0.667
// http://www.rane.com/note170.html
//The order of the coefficients are, B1, B2, A1, A2, B0.
float Fs = samplingRate;
float omega = 2*M_PI*Fs; // w0 = 2*pi*f0/Fs
float Q = 0.50f;
float alpha = sin(omega)/(2*Q); // sin(w0)/(2*Q)
// Through H
for (int i=0; i < numFrames; ++i)
{
for (int iChannel = 0; iChannel < numChannels; ++iChannel)
{
data[i*numChannels + iChannel] = (data[i*numChannels + iChannel]/Q) / (pow(data[i*numChannels + iChannel],2) + data[i*numChannels + iChannel]/Q + 1);
}
}
float b0 = alpha;
float b1 = 0;
float b2 = -alpha;
float a0 = 1 + alpha;
float a1 = -2*cos(omega);
float a2 = 1 - alpha;
float *coefficients = (float *) calloc(5, sizeof(float));
coefficients[0] = b1;
coefficients[1] = b2;
coefficients[2] = a1;
coefficients[3] = a2;
coefficients[3] = b0;
vDSP_deq22(data, 2, coefficients, data, 2, numFrames);
free(coefficients);
}
// Measure dB
[self measureDB:data:numFrames:numChannels];
}];
My aim is to make a 10-band EQ for this buffer, using vDSP_deq22, the syntax of the method is:
vDSP_deq22(<float *vDSP_A>, <vDSP_Stride vDSP_I>, <float *vDSP_B>, <float *vDSP_C>, <vDSP_Stride vDSP_K>, <vDSP_Length __vDSP_N>)
See: http://developer.apple.com/library/mac/#documentation/Accelerate/Reference/vDSPRef/Reference/reference.html#//apple_ref/doc/c_ref/vDSP_deq22
Arguments:
float *vDSP_A is the input data
float *vDSP_B are 5 filter coefficients
float *vDSP_C is the output data
I have to make 10 filters (10 times vDSP_deq22). Then I set the gain for every band and combine them back together. But what coefficients do I feed every filter? I know vDSP_deq22 is a 2nd order (butterworth) IIR filter, but how do I turn this into a bandpass?
Now I have three questions:
a) Do I have to de-interleave and interleave the audio buffer? I know setting stride to 2 just filters on channel but how I filter the other, stride 1 will process both channels as one.
b) Do I have to transform/process the buffer before it enters the vDSP_deq22 method? If so, do I also have to transform it back to normal?
c) What values of the coefficients should I set to the 10 vDSP_deq22s?
I've been trying for days now but I haven't been able to figure this on out, please help me out!
Your omega value need to be normalised, i.e. expressed as a fraction of Fs - it looks like you left out the f0 when you calculated omega, which will make alpha wrong too:
float omega = 2*M_PI*Fs; // w0 = 2*pi*f0/Fs
should probably be:
float omega = 2*M_PI*f0/Fs; // w0 = 2*pi*f0/Fs
where f0 is the centre frequency in Hz.
For your 10 band equaliser you'll need to pick 10 values of f0, spaced logarithmically, e.g. 25 Hz, 50 Hz, 100 Hz, 200 Hz, 400 Hz, 800 Hz, 1.6 kHz, 3.2 kHz, 6.4 kHz, 12.8 kHz.