Define equations and binary variables - glpk

var x11>=0; var x21>=0;
var x11b binary; var x12b binary;
s.t. eq1: (50 + 50*x11)*x11b + (40 + 40*x21)*x12b<=500;
And it prompts me as error message
x11b not defined
I looked in several pages and its the same format. Any idea why is that¿

The correct way to define x11b would be
var x11b >= 0, binary
But as pointed in the comments, this is not a linear program.

Systaxis error, it is:
var x11b, binary; var x12b, binary;

Related

Change array dimension when generating a new model CPLEX OPL

I have an optimization model that I want to implement in IBM CPLEX Optimization Studio 12.10.
I wrote the model code in OPL and the first implementation is working. What I would like to do now is to iterate the model multiple times to see how the resolution time changes depending on the dimension of the parameters.
In the .mod file I have defined three sets:
int numSet1=...;
int numSet2=...;
int numSet3=...;
range Set1 = 1..numSet1;
range Set2 = 1..numSet2;
range Set3 = 1..numSet3;
And four parameters:
float Par1[Set1]=...;
float Par2[Set1][Set2]=...;
float Par3[Set1]=...;
float Par4[Set1][Set2][Set3]=...;
In the .dat file, I have defined the initial values for these sets and parameters.
What I would like to do now is to define, in the flow control, a code that allows me to change the dimensions fo the sets, and thus, of the parameters, and save the resolution time for each resolution:
main {
var mod = thisOplModel.modelDefinition;
var dat = thisOplModel.dataElements;
for (var sizenumSet1 = 2; sizenumSet1 <= 10; sizenumSet1 += 2) {
for (var sizenumSet2 = 1; sizenumSet2 <= 5; sizenumSet2 +=1) {
for (var sizenumSet3 = 1; sizenumSet3 <=5; sizenumSet3 +=1) {
var MyCplex = new IloCplex();
var opl = new IloOplModel(mod, MyCplex);
dat.changenumSet1=sizenumSet1;
dat.changenumSet2=sizenumSet2;
dat.changenumSet3=sizenumSet3;
opl.addDataSource(dat);
opl.generate();
if (MyCplex.solve()) {
writeln("Solution: ", MyCplex.getObjValue(),
" / sizeSet1: ", sizenumSet1,
" / sizeSet2: ", sizenumSet2,
" / sizeSet3: ", sizenumSet3,
" / time: ", MyCplex.getCplexTime());
}
opl.end();
MyCplex.end();
}
}
}
}
When I launch this code what I obtain is the following list of errors:
Execution of main failed. Processing OPL model failed
Index out of bound for array Par4(1)(1):3
Scripting runtime error: (in generate) Processing OPL model failed
How can I solve this?
Thank you for your help.
In
dat.changenumSet1=sizenumSet1;
dat.changenumSet2=sizenumSet2;
dat.changenumSet3=sizenumSet3;
you are changing the wrong elements. You should be changing
dat.numSet1=sizenumSet1;
dat.numSet2=sizenumSet2;
dat.numSet3=sizenumSet3;
Moreover, it seems you are missing updates to the Par arrays. These arrays become larger in each iteration, so need to provide more data for them.

Is it possible to call 2 API's and displaying a combined answer string at the same time?

I am having trouble doing just that because of the await and async functions.
I want to have an app that analize a face in real time and is displaing the rectangle on his face and above it should say gender, age, emotion, emotion confidance.
So I want to use the Face API and Emotion API at the same time.
Thanks.
Duplicate with Calling the face and emotion API at the same time.
Please refer to that thread for more information.
Assuming you're using the C# SDKs, you can wait for both tasks to complete. The code would like something like this:
static bool SameFace(Microsoft.ProjectOxford.Face.Contract.FaceRectangle r1,
Microsoft.ProjectOxford.Common.Rectangle r2)
{
// Fuzzy match of rectangles...
return Math.Abs((r1.Top + r1.Height / 2) - (r2.Top + r2.Height / 2)) < 3 &&
Math.Abs((r1.Left + r1.Width / 2) - (r2.Left + r2.Width / 2)) < 3;
}
void Test(string imageUrl)
{
var faceClient = new FaceServiceClient(FACE_API_KEY);
var emotionClient = new EmotionServiceClient(EMOTION_API_KEY);
var faceTask = faceClient.DetectAsync(imageUrl, false, false, new FaceAttributeType[] { FaceAttributeType.Age, FaceAttributeType.Gender });
var emotionTask = emotionClient.RecognizeAsync(imageUrl);
Task.WaitAll(faceTask, emotionTask);
var people = from face in faceTask.Result
from emotion in emotionTask.Result
where SameFace(face.FaceRectangle, emotion.FaceRectangle)
select new {
face.FaceAttributes.Gender,
face.FaceAttributes.Age,
emotion.Scores
};
// Do something with 'people'
}
The tricky part is that the two APIs don't have the same rectangle type, and give slightly different values, hence a fuzzy match.

Crossfilter: how to build custom reduce functions when I want to access a specific array-value?

I have constructed my crossfilter-setup a bit different than in most examples I can find, namely:
I have data-array d with multiple data-sources included, among which is data1.
var cf = crossfilter(d3.range(0, d.data1.length));
Then I construct my dims like:
var dim = cf.dimension(function(i) { return d.data1[i].id; });
And I construct my groups like:
var group = dim.group().reduceSum(function(i) { return d.data1[i].total;});
This all works fine, but when I want to create custom reduce functions, the extra parameter i is giving me trouble.
var reduceAddPerc = function(p,v) {
p.sumOfSub += d.data1[i].var1;
p.sumOfTotal += d.data1[i].total;
p.finalVal = p.sumOfSub / p.sumOfTotal;
return p;
};
var reduceRemovePerc = function(p,v) {
p.sumOfSub -= d.data1[i].var1;
p.sumOfTotal -= d.data1[i].total;
p.finalVal = p.sumOfSub / p.sumOfTotal;
return p;
};
var reduceInitialPerc = function() {
return {sumOfSub:0, sumOfTotal:0, finalVal:0 };
};
And then defining the group with:
var group = dim.group().reduce(reduceAddPerc,reduceRemovePerc,reduceInitialPerc);
This doesn't work obviously, since the parameter i is now not known within the function. But I've tried adding the parameter (p,v,i), or nesting the functions by creating an additional function with parameter i around the (p,v) function, and also creating an additionao function(i) within the (p,v) function, but I cannot get this to work.
Does anyone have any help to offer?
In the custom reduce functions, the v parameter is the record currently being "reduced". In this case, it should be your counter, so just use it where you would normally use i. Is that not working?

ThreeJS: 3D Object Area Calculation (Triangulated)

I need to calculate the area/surface of a whole object in threeJS. Thats what I have:
var _len = object.geometry.faces.length,
_area = 0.0;
if (!_len) return 0.0;
for (var i = 0; i < _len; i++) {
var va = object.geometry.vertices[object.geometry.faces[i].a];
var vb = object.geometry.vertices[object.geometry.faces[i].b];
var vc = object.geometry.vertices[object.geometry.faces[i].c];
var ab = vb.clone().sub(va);
var ac = vc.clone().sub(va);
var cross = new THREE.Vector3();
cross.crossVectors( ab, ac );
_area += cross.lengthSq() / 2;
}
The results are kind of wrong. I get a floating value, fine, but comparing a very small object with a big object. The smaller could have a bigger surface with the provided code. I checked on many different objects and got not realistic values, when comparing them.
Actually the objects having the biggest faces, but being the smallest in the overall surface, seem to have to highest values with the current version of the code.
I hope someone could have a look at the code and see whats wrong. Very much appreciated!
You are using lengthSq(), is that right? I guess you need the length of the cross vector, not the length squared.

How to fix Adobe Flex simple multiplication error

We are using version 3.6. We call a rounding function to clean up the decimal part. Something like this...
private function ceilingRounding(value:Number, power:Number):Number
{
var scale:Number = Math.pow(10, power);
return (Math.ceil(value * scale) / scale);
}
The function result is unexpected for the following values:
value = 76.7549, scale = 10000.
The result should be 76.7549 but we get 76.7550
Using the debugger, we see that value * scale = 767549.0000000001. Of course this would be rounded up to 76.7550, but why are we getting .0000000001 and how can we fix this?
The "NUMBERS" values ​​hold this approximation operation, you can modify your function and add the following into operation, plus I let the function that I use regularly to do the rounding.
public static function roundToPrecision(numberVal:Number, precision:int = 0):Number
{
var decimalPlaces:Number = Math.pow(10, precision);
return Math.round(decimalPlaces * numberVal) / decimalPlaces;
}
If you need Fixed decimal Number try
var myNum:Number = 1.123556789
myNum.toFixed(3);
trace(myNum); // 1.123
If you need Precision decimal Number try
myNum.toPrecision(3);
trace(myNum); // 1.124;
For more options see rounding in link.
Reference NumberFormatter

Resources