How to access decision variables: OPL/Cplex && C++ - c++

I am using OPL/Cplex libraries in my c++ code,
In my file.mod I decalred this decision variable:
dvar int+ x[nodes][nodes][1..nb_max][lambdas];
Now I that Cplex solved the model I successfully recuperate the objective value as follows:
try {
IloCplex cplex(env);
cplex.setOut(env.getNullStream());
IloOplErrorHandler handler(env,cout);
IloOplModelSource modelSource(env, "file.mod");
IloOplSettings settings(env,handler);
IloOplModelDefinition def(modelSource,settings);
IloOplModel opl(def,cplex);
IloOplDataSource dataSource(env, "file2.dat");
opl.addDataSource(dataSource);
opl.generate();
if ( cplex.solve() ) {
cout<< opl.getCplex().getObjValue()<< endl;
}
}
My question is how can I recuperate the multidimentional array "x"?
I tried with
IloIntVar x = opl.getElement("x").asIntVar;
IloIntVar xvar =x.get(0);//first item
but the following errors occur!
error: conversion from '' to non-scalar type 'IloIntVar' requested
error: 'class IloIntVar' has no member named 'get'
I am really a beginner in OPL,
Thanks in advance!

you have an answer to this at https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014387796&ps=25
Let me quote David Gravot from ROSTUDEL
dvar float+ Commande[Items,Periodes];
Now, in my JAVA code, I wrote the following to access all solution values for these variables as following :
IloNumMap commandesMap = opl.getElement("Commande").asNumMap();
for(int i=0;i<params.getNbItems();i++) {
for(int t=0;t<params.getNbPeriodes();t++) {
double solValue = commandesMap.getSub(i+1).get(t+1);
log.info("at item "+(i+1)+" periode "+(t+1)+" : "+solValue );
}
}
regards

Related

How to fix : "FActorSpawnParameters is not defined" error in C++ in UE4?

I'm following a tutorial to make a game with UE4 and C++ and a error appear when I type the following line
FActorSpawnParameters params;
It says that the identifier FActorSpawnParameters is not defined.
I've tried to modify some of my code but it didn't change...
So I replace all the things in order.
void AUltimatePawn::Shoot()
{
if (BulletClass)
{
FActorSpawnParameters params;
params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
params.bNoFail = true;
params.Owner = this;
params.Instigator = this;
FTransform BulletSpawnTransform;
BulletSpawnTransform.SetLocation(GetActorForwardVector() * 500.f + GetActorLocation());
BulletSpawnTransform.SetRotation(GetActorRotation().Quaternion());
BulletSpawnTransform.SetScale3D(FVector(1.f));
GetWorld()->SpawnActor<ABullet>(BulletClass, BulletSpawnTransform, params);
}
}
I just want you to tell me how to fix this error,
Thank's
Make sure that you include Runtime/Engine/Classes/Engine/World.h.
I extracted this information from the official API reference.

Tkd widget not finding row and column options

I am trying to create a simple GUI application using tkd package and following code:
// modified from: https://github.com/nomad-software/tkd
import tkd.tkdapplication;
class Application : TkdApplication {
auto labellist = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", ];
override protected void initInterface() {
int ncol =0;
auto frame = new Frame(2, ReliefStyle.groove);
frame.pack(10);
foreach(lab; labellist){
auto label = new Label(frame, lab);
label.grid(row=nrow, column=0);
auto entry = new Entry(frame);
entry.grid(row=nrow, column=1);
nrow += 1;
}
auto exitButton = new Button(frame, "Exit").setCommand(&this.exitCommand).pack(10);
}
private void exitCommand(CommandArgs args) {
this.exit();
}
}
void main(string[] args){
auto app = new Application();
app.run();
}
However, it is giving following error:
$ dub run
Performing "debug" build using /usr/bin/dmd for x86_64.
x11 1.0.21: target for configuration "tcltk-import" is up to date.
tcltk 8.6.5: target for configuration "library" is up to date.
tkd 1.1.12: target for configuration "library" is up to date.
tkdgui ~master: building configuration "application"...
source/app.d(15,15): Error: undefined identifier row
source/app.d(15,25): Error: undefined identifier column
source/app.d(17,15): Error: undefined identifier row
source/app.d(17,25): Error: undefined identifier column
source/app.d(18,4): Error: undefined identifier nrow
/usr/bin/dmd failed with exit code 1.
Details about grid are mentioned here. Row and column are valid options to be entered.
Where is the problem and how can it be solved.
There are two issues in your code. Here's the first:
label.grid(row=nrow, column=0);
^^^^ ^^^^^^^
D does not support named parameters, which you're attempting to use. Instead you will need to use positional parameters:
label.grid(0, nrow);
FWIW, there are some proposals to add named parameters to D, but none are in the language as of now.
The second issue is nrow is not defined anywhere. Judging by the existence of ncol and the fact it's used nowhere, it seems you changed the code from dealing with columns to dealing with rows and did not change the name of ncol to nrow.

Validate parsed fields using Univocity Parser

I wanted to know if there is a way to check and validate a field when using the CsvRoutines package. Basically I want to process a row if the first column has only numbers and skip/possibly throw an exception otherwise. I'm guessing #Validate annotation released in 2.7.0 can be used to achieve this. But I would like to know if there is any other way to achieve the same with earlier versions like 2.5.9?
Author of the library here. There's no other way other than updating to the latest version. Is there any reason in particular why you can't upgrade?
Update: you can put the #Parsed annotations on the class' getters or setters and perform the validations in them. That is probably the cleanest way to go about it. For example:
class Test {
private Integer number;
//accepts a String here... so this is straight from the parser before it tries to convert anything into an integer - which lets you validate or throw a custom exception
#Parsed
void setNumber(String number){
try{
this.number = Integer.valueOf(number);
} catch(NumberFormatException e){
throw new IllegalArgumentException(number + " is not a valid integer");
}
}
}
Another alternative is to use a custom conversion class. Copy the code of class ValidatedConversion, used in the newest version, then create subclass like:
public static class RangeLimiter extends ValidatedConversion {
int min;
int max;
public RangeLimiter(String[] args) {
super(false, false); //not null, not blank
min = Integer.parseInt(args[0]);
max = Integer.parseInt(args[1]);
}
protected void validate(Object value) {
super.validate(value); //runs the existing validations for not null and not blank
int v = ((Number) value).intValue();
if (v < min || v > max) {
throw new DataValidationException("out of range: " + min + " >= " + value + " <=" + max);
}
}
}
Now on your code, use this:
#Parsed(field = "number")
#Convert(conversionClass = RangeLimiter.class, args = {"1", "10"}) //min = 1, max = 10
public int number;
I didn't test this against an old version. I think you may need to set flag applyDefaultConversion=false in the #Parsed annotation, and make your conversion class convert a String into an int in addition to run the validations.
All in all, that's quite a bit of work that can easily be avoided just by upgrading to the latest version.

How can I fix the bug in ACADO Toolkit when using OnlineData variables?

As exposed in this topic in ACADO forum, there is a bug when you try to use OnlineData variables. In my case I am using C++ code instead of MATLAB interface and have 7 OnlineData variables. In the topic mentioned before they propose using the function SetNOD but using C++ I can't call the function. I cannot access the official forum in sourcefourge because they have some problems and I would appreciate your help.
The abbreviated code is:
include
define N 20 //Time intervals -- 51 states
int main{
USING_NAMESPACE_ACADO
//Variables
DifferentialState x, y, z, dx, dy, dz, roll, pitch, yaw, droll, dpitch, dyaw;
Control u1, u2, u3, u4;
OnlineData yaw0, obsx, obsy, obsz, obsrx, obsry, obsrz;
. . .
//Create Optimal Control Problem object
OCP ocp(t_start, t_end, N); //50 number of discretization intervals
//Fixing the bug
//Alternatives I tried
ocp.SetNOD(7);//Error A
//ocp.ModelContainer.SetNOD(7);//Error B
//Objective Function
ocp.minimizeLSQ(Q, h);
ocp.minimizeLSQEndTerm(QN, hN);
/* Constraints */
//Model constraint
ocp.setModel( f );
/* Export OCP */
OCPexport mpc( ocp );
...
if (mpc.exportCode( "path_qp_export_oases" ) != SUCCESSFUL_RETURN)
exit( EXIT_FAILURE );
return 0;
}
Error A:
/.../path_qp_generated_oases.cpp: In function ‘int main()’:
/.../path_qp_generated_oases.cpp:321:9: error: ‘class ACADO::OCP’ has no member named ‘SetNOD’
ocp.SetNOD(7);
make[2]: [.../CMakeFiles/my_examples_path_qp_generated_oases.dir/my_examples/path_qp_generated_oases.cpp.o] Error 1
make1: .../CMakeFiles/my_examples_path_qp_generated_oases.dir/all] Error 2
make: [all] Error 2
Error B:
/.../path_qp_generated_oases.cpp: In function ‘int main()’:
/.../path_qp_generated_oases.cpp:320:10: error: invalid use of ‘ACADO::ModelContainer::ModelContainer’
ocp.ModelContainer.SetNOD(7);
make2: [examples/CMakeFiles/my_examples_path_qp_generated_oases.dir/my_examples/path_qp_generated_oases.cpp.o] Error 1
make1:[examples/CMakeFiles/my_examples_path_qp_generated_oases.dir/all] Error 2
make: all Error 2
I am using the version of acado-master downloaded today:
Branch: master
Commit: 2cde3c748856ca16a4460e05149c1e5de362526f
Remote: acado/acado
Also have the same problem with acado-stable downloaded today:
~/ACADOtoolkit$ git rev-parse HEAD
e0cc4b058e1dc60c4e57f306dc7c7db41a582451
Thank you very much!!
I had the same problem.
There is currently a bug in ACADO Toolkit where it sometimes mis-counts or completely ignores the OnlineData variables that you use to define your problem.
The method is setNOD() with a lower-case "s".
Here's some example code which may help other people...
.
First make sure you install the latest ACADO source from github.
Define your non-linear optimization problem for ACADO like shown in the documentation:
DifferentialState x;
DifferentialState y;
Control u;
DifferentialEquation f;
f << dot(x) == 1.0*u;
f << dot(y) == 2.0*u;
OnlineData d0;
OnlineData d1;
OnlineData d2;
etc.
OCP ocp(0.0, steps*Ts, steps);
ocp.minimizeLSQ(W, h);
ocp.minimizeLSQEndTerm(W, h);
ocp.setModel(f);
ocp.setNOD(3);
OCPexport mpc(ocp);
You have to manually count your number of OnlineData and put that number into setNOD().
Then compile, generate the code, etc. like described in the documentation.
Now you can search through the code in the export directory to find these OnlineData variables that were previously ignored:
grep -R "od\[" *
Also note the test.c program won't set these variables.
In your own code, you will need to set their value.
If you have 3 OnlineData, you set them to be the same for each step along the control horizon like this:
for (int i = 0; i < (N + 1); ++i)
{
acadoVariables.od[i * NOD + 0] = 1.0;
acadoVariables.od[i * NOD + 1] = 9.0;
acadoVariables.od[i * NOD + 2] = 5.0;
}
Hope this helps!

How do I create a loop in omnet++?

How do I create a loop in omnet ++ with a user defined variable.
I tried the following but it gave me an error:
network Network{
parameters:
int n #prompt("enter number") = default(2);
connections:
for i=0..n do { // here the n gives me a syntax error (unexpected NAME, expected {
//do things
}
Have a look at the OMNeT++ manual. The for syntax is without do, e.g.
for i = 0..count-2 {
node[i].port[1] <--> node[i+1].port[0];
}