Change array dimension when generating a new model CPLEX OPL - multidimensional-array

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.

Related

C++ add new row in datagrid view table

Using visual studio here. In code below you can see a string being a split, and I want put each split string in a new row, but program crashes not creating a new row and I got error:
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
String^ text = textBox1->Text;
cli::array<String^>^ part = text->Split('.','?','!');
for (int split = 0; split < part->Length; ++split)
{
datagrid->Rows[split]->Cells[3]->Value = part[split];
}
You probably don't have enough rows in you data grid so you'll need to add them inside your loop:
String^ text = textBox1->Text;
cli::array<String^>^ part = text->Split('.','?','!');
datagrid->Rows->Clear();
for (int split = 0; split < part->Length; ++split)
{
datagrid->Rows->Add(part[split]);
}

Function to generate randomly incrementing number over time

I'm trying to make a fake download count. It should increment randomly over time. Some download-count-like patterns would be nice.
Is this possible without using a database, or storing a counter anywhere?
My idea is to check the number of seconds that have passed since my app was released. Then just throw that into a formula which spits out the fake download count. Users can request to see the download count at any time.
Is there a math function that increments randomly? I could just pass my secondsPassed into there and scale it how I'd like.
Something like this: getDownloadCount(secondsPassed)
Edit: here's an example solution. But it gets worse performance over time.
downloadCount = 0
loop secondsPassed/60 times // Loop one more time for every minute passed
downloadCount += seededRandom(0, 10)
Making a fake download count doesn't sound like a nice thing to do. However in designing secure communication protocols, there are legitimate use cases for monotonically growing functions with some randomness in their values.
I am assuming you have:
A growth model given as a monotonically growing function providing approximate values for the desired function.
Access to a time stamp, which never decreases.
Ability to store a constant random seed along with the function definition.
No way to store any updated data upon the function being queried.
First you decide on a window length, which will control how much randomness will be in the final output. I expect you will want this to be on the order of one hour or a few.
Figure out which window the current time is within. Evaluate the reference function at the start and end of this window. Consider the rectangle given by start and end time of the window as well as min and maximum value given by the reference function. Feed the corners of this rectangle and your constant seed into a PRNG. Use the PRNG to choose a random point within the rectangle. This point will be on the final curve.
Perform the same computation for one of the neighbor windows. Which neighbor window to use depend on whether the first computed point on the curve is to the left or the right of the current time.
Now that you have two points on the curve (which are reproducible and consistent), you will have to iterate the following procedure.
You are given two points on the final curve. Consider the rectangle given by those corners. Feed the corners and your constant seed into a PRNG. Use that PRNG to chose a random point within the rectangle. This point will be on the final curve. Discard one of the outer points, which is no longer needed.
Since the Y-values are restricted to integers, this procedure will eventually terminate once your two points on the curve have identical Y-coordinate, and you will know, that the function has to be constant between those two points.
You could implement a Morris Counter.
It works like this: start off by setting the counter to 1. Each time you want to increase the count (which could be every iteration of some loop or every time an event happens, but does not need to be determined randomly), then you do a random procedure to determine the effect it has on the counter.
It can have possibly no effect, or it can have the effect of raising the order of magnitude of the count. The probability is based on whether or not n successive fair coin flips all turn up heads, where n is the number of bits needed to encode the current counter value in binary. As a result, once the counter has gotten pretty high, it's very hard to make it go even higher (the state of the counter models a phenomenon where by you are already way overestimating the count, so now you need lots of nothing-happens events to compensate, making the count more accurate).
This is used as a cheap way to store an approximate count of a very large collection, but there's no reason why you can't use it as your randomly increasing counter device.
If you want better accuracy, or you want the count outputs to be more "normal" numbers instead of always powers of 2, then you can just create several Morris Counters, and at each step average together the set of current counts across them all.
You are after a sequence which always increases by a random amount, depending on how long you last requested the sequence.
This can be done through a random sequence that is always seeded the same.
Then we iterate through the same sequence each time to get the graph.
We need a function that increments our counter, store the new Time and Count and return the count.
Ideally we would model the increases as a poisson process, but a linear one here will do.
class Counter {
private static int counter = 0;
private static int time = 0;
private static double rate = 5.0;
private Random r;
public Counter(int seed){
counter = 0;
r = new Random(seed);
}
private int poisson(double rate, int diff){
// We're gonna cheat here and sample uniformly
return r.Next(0, (int)Math.Round(rate * diff));
}
public int getNext(int t){
var diff = t - time;
time = t;
if (diff <= 0) return counter;
counter += this.poisson(rate, diff);
return counter;
}
}
void Main()
{
var c = new Counter(1024);
for(var i = 0; i< 10; i++){
Console.WriteLine(String.Format("||{0}\t|{1}\t||",i,c.getNext(i)));
}
}
This outputs (for example):
||t |hit||
||0 |0 ||
||1 |3 ||
||2 |4 ||
||3 |6 ||
||4 |6 ||
||5 |8 ||
||6 |10 ||
||7 |13 ||
||8 |13 ||
||9 |16 ||
For some deterministic function f, (perhaps f(x) = x, or if your fake app is REALLY awesome f(x) = 2^x), and a random function r which outputs random number that's sometimes negative and sometimes positive.
Your graphing function g could be:
g(x) = f(x) + r
EDIT
How about this: https://gamedev.stackexchange.com/questions/26391/is-there-a-family-of-monotonically-non-decreasing-noise-functions
Well it's not "random" but you could use A*(X/B + SIN(X/B)) (scaled by some number) to introduce some noise. You can adjust A and B to change the scale of the result and how often the "noise" cycles.
Really, any periodic function that has a first derivative within some bounds could work.
as quick solution you can use something like this (code in java):
static long f(final int x) {
long r = 0; // initial counter
long n = 36969L; // seed
for (int i = 0; i <= x; i++) {
n = 69069L * n + 1234567L; // generate Ith random number
r += (n & 0xf); // add random number to counter
}
return r;
}
by playing with numbers 36969L and 0xf you can achieve different results
numbers 69069L and 1234567L are from standard LCG
the main idea - create simple random, with the same seed and for every passed x (number of seconds) replay random additions to counter
A good model for random events like downloads is the Poisson distribution. You need to estimate the average number of downloads in a given time period (hour, say) and then invert the Poisson distribution to get the number of downloads in a time period given a uniformly distributed random number. For extra realism you can vary the average according to time of day, time of week, etc. Sample algorithms are available at http://en.m.wikipedia.org/wiki/Poisson_distribution#Generating_Poisson-distributed_random_variables.
Here is a javascript implementation of a "fake" download-counter that appears the same to everyone. This always returns the same results for everyone each time and doesn't require database or files to do that. It also gracefully handles the case where you don't ask for new data at the same time, it will still look natural next time you request a day.
https://jsfiddle.net/Lru1tenL/1/
Counter = {
time:Date.now(),
count:0,
rate:0.45
};
Counter.seed = function(seed, startTime)
{
this.time = startTime,
this.count = 0,
this.prng = new Math.seedrandom(seed);
this.prng.getRandomInt = function(min, max) {
return Math.floor(this() * (max - min)) + min;
};
};
Counter.getNext = function(t){
var diff = t - this.time;
console.log(diff);
if(diff <= 0) return this.count;
this.time = t;
var max = Math.ceil(diff/100 * this.rate);
console.log("max: " + max);
this.count += this.prng.getRandomInt(0,max);
return this.count;
};
var results = [];
var today = Date.now();
Counter.seed("My Random Seed", today);
for (var i = 0; i < 7; i++) {
if(i === 4)
{
results.push(null);
} else {
var future = today + 86400000 * i;
results.push(Counter.getNext(future));
}
}
console.log(results);
var data = {
labels: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"],
datasets: [
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: results
}
]
};
var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).Line(data);
Is the javascript. It creates a counter object that increments when requested based on the time of the previous value asked for. The repeatability comes through a thirdparty library "seedrandom" and the chart is drawn using chartjs.
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.0/seedrandom.min.js">
</script>
<body>
<canvas id="myChart" width="600" height="400"></canvas>
</body>
</html>
You can use Unix timestamp. Something like:
Downloads = constant + ( unix time / another constant )
You can vary both constants to get a reasonable number.
P.S: Thats if you want a linear function, otherwise you can do:
Downloads = (unix time) ^ constant
and so on.

Game Maker Studio: DoSet :: Invalid comparison type

___________________________________________
############################################################################################
FATAL ERROR in
action number 4
of Create Event
for object eng_Global:
DoSet :: Invalid comparison type
at gml_Script_Data_Load (line 1) - ///Data_Load()
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_Data_Load (line 1)
called from - gml_Object_eng_Global_CreateEvent_4 (line 60) - Data_Load();
I get this error on a comment, not an actual if statement, I can't bypass this without commenting out Data_Load(), which is what loads the users' data.
I recently updated to version 1.4.1567, maybe that is a bug in this version.
I should state that "Connected" and "Guest" variables are both integers (boolean) and do not get set to string at any point in the code.
Here is the Data_Load() script:
///Data_Load()
if (Connected && !Guest) {
ini_open(User_Name+"_NSD_Temp.ini");
// Base Statistics
Level = ini_read_real("Statistics","Level",Level);
Exp_Total = ini_read_real("Statistics","Experience_Total",Exp_Total);
Exp = ini_read_real("Statistics","Experience",Exp);
Exp_Left = ini_read_real("Statistics","Experience_Left",Exp_Left);
Exp_Max = ceil(Level*5);
Gold = ini_read_real("Statistics","Gold",Gold);
Gold_Total = ini_read_real("Statistics","Gold_Total",Gold_Total);
Karma = ini_read_real("Statistics","Karma",Karma);
Karma_Total = ini_read_real("Statistics","Karma_Total",Karma_Total);
Highscore = ini_read_real("Statistics","Highscore",Highscore);
Weapons_Inv_Length = ini_read_real("Statistics","Weapons_Inv_Length",Weapons_Inv_Length);
Stones_Inv_Length = ini_read_real("Statistics","Stones_Inv_Length",Stones_Inv_Length);
Stone_Slots_Owned = ini_read_real("Statistics","Stones_Slots_Owned",Stones_Slots_Owned);
// Game
Ninja_Name = ini_read_string("Game","Ninja_Name",Ninja_Name);
Ninja_Level = ini_read_real("Game","Ninja_Level",Ninja_Level);
Ninja_Health = ini_read_real("Game","Ninja_Health",Ninja_Health);
Ninja_Health_Max = ini_read_real("Game","Ninja_Health_Max",Ninja_Health_Max);
Ninja_Health_Regen_Upgrade = ini_read_real("Game","Ninja_Health_Regen_Upgrade",Ninja_Health_Regen_Upgrade);
Ninja_Health_Regen = Ninja_Health_Base*(Ninja_Health_Regen_Upgrade)/room_speed;
Ninja_Weapon = ini_read_real("Game","Ninja_Weapon",Ninja_Weapon);
Ninja_Colour = ini_read_real("Game","Ninja_Colour",Ninja_Colour);
Ninja_Power = ini_read_real("Game","Ninja_Power",Ninja_Power);
Ninja_Max_Speed = ini_read_real("Game","Ninja_Max_Speed",Ninja_Max_Speed);
Ninja_Attack_Speed = ini_read_real("Game","Ninja_Attack_Speed",Ninja_Attack_Speed);
// Weapons Inventory
for (i=0; i<Weapons_Inv_Length; i++) {
Weapons_Inv[i,0] = i;
Weapons_Inv[i,1] = ini_read_real("Weapons Inventory","Inv_Slot_"+string(i),0);
Weapons[Weapons_Inv[i,1],5] = ini_read_real("Weapons Inventory","Inv_Slot_"+string(i)+"_Owned",Weapons[Weapons_Inv[i,1],5]);
}
// Stones Inventory
for (i=0; i<Stones_Inv_Length; i++) {
Stones_Inv[i,0] = i;
Stones_Inv[i,1] = ini_read_real("Stones Inventory","Inv_Slot_"+string(i),0);
Stones[Stones_Inv[i,1],5] = ini_read_real("Stones Inventory","Inv_Slot_"+string(i)+"_Owned",Stones[Stones_Inv[i,1],5]);
}
// Equipped Stones
for (i=0; i<Stone_Slots_Owned; i++) {
Stone_Slots[i,0] = i;
Stone_Slots[i,1] = ini_read_real("Stones Equipped","Slot_"+string(i),Stone_Slots[i,1]);
}
// Costume Colours
for (i=0; i<array_height_2d(Colours); i++) {
Colours[i,5] = ini_read_real("Costume Colours",Colours[i,1],Colours[i,5]);
}
// Stats
Stat_Clouds_Clicked = ini_read_real("Stats","Clouds_Clicked",Stat_Clouds_Clicked);
Stat_Stars_Clicked = ini_read_real("Stats","Stars_Clicked",Stat_Stars_Clicked);
// Options
SoundFX = ini_read_real("Options","SoundFX",SoundFX);
// Version
Save_Version = ini_read_string("Version","Current",Save_Version);
// Resets
ForceResets = ini_read_string("External","Force_Resets",Force_Resets);
ini_close();
if (ForceResets != Force_Resets) {
Data_Erase();
}
Data_Submit();
} // If Connected & Not Guest
GM's compiler is always weird about those line errors. It often doesn't count blank lines as actual lines.
If you adjust for that issue, the real line of code that is throwing the error is this:
if (ForceResets != Force_Resets) {
Maybe it doesn't like that you're basically asking "If something is not equal to itself", which hardly makes sense. That statement will always evaluate to false, so you should probably remove it.
Seeing as you don't declare var on any of these variables, then you're manipulating the variables on the instance that called this script. If there's somehow a ForceResets script variable, and a ForceResets variable on the calling instance, then this whole thing might be a naming issue. I'm also making this assuming because you called:
ForceResets = ini_read_string("External","Force_Resets",Force_Resets);
Where that third parameter isn't declared anywhere in this script.
All in all, I'd say that you need to clean this script up a little.
Pro Tip: Use for(var i = 0; ... instead of for(i = 0 99% of the time. Otherwise you're leaving this instance with an i variable that it will never use.

How to change constraint right hand side cplex in java

I would like to know how can I change the right hand side of a constraint after I have read a model in a file with CPLEX Java API.
Let's say I have the following code:
IloCplex cplex = new IloCplex();
cplex.setParam(IloCplex.IntParam.RootAlg,IloCplex.Algorithm.Auto);
cplex.importModel(filename);
if ( cplex.solve() ) {
// solve the model
}
and I have a constraint "c0" and I want to solve the same model changing only the right hand side of this constraint. How can I change it before I solve the model again? I tried to look for examples in the installation package, but so far I missed how to do it.
There is a function setBounds(newLB, new UB), but I can't just write in my code
c0.setBounds(new LB, new UB).
Any help would be great.
This may not be the best solution, but after some research I found that, by making:
String filename = "model.lp";
IloCplex cplex = new IloCplex();
cplex.setParam(IloCplex.IntParam.RootAlg,IloCplex.Algorithm.Auto);
cplex.importModel(filename);
IloLPMatrix lp = (IloLPMatrix)cplex.LPMatrixIterator().next();
if(cplex.solve()){
double[] x = cplex.getValues(lp);
for (int j = 0; j < x.length; ++j){
System.out.println("Nome: " + lp.getNumVar(j).getName() + " Valor: " + x[j]);
}
}
IloRange [] list = lp.getRanges();
for(int g = 0; g < list.length; ++g){
System.out.println(list[g].getLB() + " <= " + list[g].getName() + " <= " + list[g].getUB());
if(list[g].getName().equals("r1")){
list[g].setBounds(6.5, 6.5);
}
}
I could get the constraint "r1" and change its limits. It gave the correct answer for a toy problem, at least.
However, when I try to solve the model for the second time, cplex gives me:
"Warning: No solution found from 1 MIP starts."
I know that the solution of the first problem will not be a solution for the second program. From the research I made I understood, possibly wrongly, that, as the solution to the first problem is infeasible for the second, it will start from scratch because I have a MIP problem. So, would it be preferable if I just wrote the model again in file and loaded it again?

Dart - Getting RangeError involving iteration over a list of numbers

I'm using Dart, trying to do some low level project euler problems; this problem is for both the prime number counter and greatest prime factor problem.
Here is the Unit Test and the error below which is produced (my other two unit tests evaluate fine):
test('evaluate if target is not factorable', () {
// Arrange
var maximum = 14;
var control_list_of_primes = <int>[2,3,5,7,11];
var test_list_of_primes = <int>[2,3,5,7,11];
// Act
prime_list_expansion(maximum, test_list_of_primes);
// Assert
expect(test_list_of_primes[-1] == 13, true);
});
ERROR: -prime factor generator- should evaluate if target is not factorable
Test failed: Caught RangeError: -1
List.[]
Here is the function prime_list_expansion (the functions it calls pass the tests I've designed for them so I don't think they are the problem):
prime_list_expansion (int maximum, List prime_list) {
var start = prime_list[-1];
for (var i = start; i < maximum; i = i+2) {
if (target_not_factorable (i, prime_list)) {
prime_list.add(i);
}
}
}
expect(test_list_of_primes[-1] == 13, true);
and
var start = prime_list[-1];
-1 will never be a valid index for a List, hence out of range error.
I assume you are trying to get the last element in the list, instead use list.last

Resources