Error in Gurobi for optimization? - ampl

sw: ampl
ampl: include Availability1.ampl
Gurobi 5.5.0: mipgap = 0.00000000000000000000000001
outlev = 1
Optimize a model with 68298 rows, 1934 columns and 28751 nonzeros
Presolve removed 1934 rows and 68298 columns
Presolve time: 0.02s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 9.9948451e-01 0.000000e+00 0.000000e+00 0s
179 9.9948451e-01 0.000000e+00 0.000000e+00 0s
Solved in 179 iterations and 0.06 seconds
Optimal objective 9.994845101e-01
Gurobi 5.5.0: optimal solution; objective 0.9994845101
179 simplex iterations
Above is my output: No error but is not the optimal answer.
What's the matter? I can't get the right answer. Please help me.

Related

Create arrays of variables in loop GEKKO

I'm wondering if it is possible to create arrays of variables with different lenghts in a loop in GEKKO.
Below is just a simple example of what I mean. Parameters in the list "lengths" define what length each GEKKO array should have:
lengths = [10,20,30]
m = GEKKO()
for i in lengths:
# something...
So from this I would like to get something like:
array1 = m.Array(m.Var,10)
array2 = m.Array(m.Var,20)
array3 = m.Array(m.Var,30)
In the real problem that I'm trying to solve there will be quite many arrays that I want to include in the optimization, and they might be different depending on the situation. So it is not a good option to manually create them every time.
There is no problem to define arrays in a loop. Here is an example:
from gekko import GEKKO
model = GEKKO(remote=True)
lengths = [10,20,30]
m = GEKKO()
x = []
for i in lengths:
x.append(m.Array(m.Var,i))
for j in range(i):
m.Minimize((x[-1][j]-j)**2)
m.solve()
for xi in x:
print(xi)
This gives a unique solution where the value is equal to the index.
This is Ipopt version 3.12.10, running with linear solver ma57.
Number of nonzeros in equality constraint Jacobian...: 0
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 60
Total number of variables............................: 60
variables with only lower bounds: 0
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 0
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 1.1310000e+04 0.00e+00 5.80e+01 0.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 0.0000000e+00 0.00e+00 0.00e+00 -11.0 2.90e+01 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 1
(scaled) (unscaled)
Objective...............: 0.0000000000000000e+00 0.0000000000000000e+00
Dual infeasibility......: 0.0000000000000000e+00 0.0000000000000000e+00
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 0.0000000000000000e+00 0.0000000000000000e+00
Overall NLP error.......: 0.0000000000000000e+00 0.0000000000000000e+00
Number of objective function evaluations = 2
Number of objective gradient evaluations = 2
Number of equality constraint evaluations = 0
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 0
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 1
Total CPU secs in IPOPT (w/o function evaluations) = 0.001
Total CPU secs in NLP function evaluations = 0.000
EXIT: Optimal Solution Found.
The solution was found.
The final value of the objective function is 0.000000000000000E+000
---------------------------------------------------
Solver : IPOPT (v3.12)
Solution time : 4.600000000209548E-003 sec
Objective : 0.000000000000000E+000
Successful solution
---------------------------------------------------
[[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0]]
[[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0] [10.0] [11.0]
[12.0] [13.0] [14.0] [15.0] [16.0] [17.0] [18.0] [19.0]]
[[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0] [10.0] [11.0]
[12.0] [13.0] [14.0] [15.0] [16.0] [17.0] [18.0] [19.0] [20.0] [21.0]
[22.0] [23.0] [24.0] [25.0] [26.0] [27.0] [28.0] [29.0]]

Find longest segment

I have a dataframe (or a series) of measured voltage (in V) indexed by timestamps (in seconds). I want to know the duration of the longest segment (=consecutive values) of voltage greater than a threshold.
Example:
time voltage
0.0 1.2
0.1 1.8
0.2 2.2
0.3 2.3
0.4 1.9
0.5 1.5
0.6 2.1
0.7 2.3
0.8 2.2
0.9 1.9
1.0 1.6
In this example, threshold is 2.0 V, and desired answer is 0.3 seconds
Real data is made of 10k or more samples, and number of segments of values above the threshold is completly random, there is even the possibility of having only one segment with all values above the threshold.
I think the first step is too identified these segments et separate them, then perform calculation of duration.
You can create a True and False sequence with boolean indexing. Then use value_counts and max to get the longest sequence:
s = df.voltage > 2
(~s).cumsum()[s].value_counts().max()
Output
3
IIUC
n=2
s=df.voltage.gt(n)
df.time[s].groupby((~s).cumsum()).diff().sum()
Out[1218]: 0.30000000000000004
And if you need the longest duration , Notice here is from 0.6 to 0.8 which should be 0.2 second ..
df.time[s].groupby((~s).cumsum()).apply(lambda x : x.diff().sum()).max()
Out[1221]: 0.20000000000000007

How DetectionBoxes_Recall is calculated for values 1 / 10 / 100?

When we are training Tensorflow Object Detection API, in Tensorboard there are 3 metrics for recall:
'DetectionBoxes_Recall/AR#1': average recall with 1 detection.
'DetectionBoxes_Recall/AR#10': average recall with 10 detections.
'DetectionBoxes_Recall/AR#100': average recall with 100 detections.
How are those calculated? What 'with n detections' exactly mean?

Why does CPlex solve this mixed integer linear program so incredibly fast?

I am working on an optimization problem where I want to find resource-activity assignment based on skill restrictions (not all resources have all skills for the demands d), resource restrictions (resources have a limited presence p) and an assignment restriction l limiting the number of resources assigned to an activity. I want to maximize the sum of weights w of all selected activities. The model is depicted here:
Now I fed this into CPlex and it solves gigantic instances in a very short amount of time as long as I allow heuristics (1000 activities, 50 resources, 5 skills within about 10 sec), even though there is a huge possible number of selected issues AND a huge number of possible assignments for each activity.
Why is this problem so easy for CPlex? Is there some kind of easily solvable underlying problem that I am missing?
edit: typical output of one of my solver runs:
Tried aggregator 1 time.
MIP Presolve eliminated 3051 rows and 64954 columns.
MIP Presolve modified 49950 coefficients.
Reduced MIP has 52001 rows, 236046 columns, and 662190 nonzeros.
Reduced MIP has 47952 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.61 sec. (276.60 ticks)
Probing time = 0.38 sec. (12.12 ticks)
Tried aggregator 1 time.
MIP Presolve eliminated 1323 rows and 62181 columns.
MIP Presolve modified 3366 coefficients.
Reduced MIP has 50678 rows, 173865 columns, and 474324 nonzeros.
Reduced MIP has 47952 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.78 sec. (334.49 ticks)
Probing time = 0.26 sec. (9.19 ticks)
Tried aggregator 1 time.
Reduced MIP has 50678 rows, 173865 columns, and 474324 nonzeros.
Reduced MIP has 47952 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.49 sec. (220.07 ticks)
Probing time = 0.36 sec. (10.46 ticks)
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 8 threads.
Root relaxation solution time = 2.86 sec. (1101.00 ticks)
Nodes Cuts/
Node Left Objective IInf Best Integer Best Bound ItCnt Gap
* 0+ 0 7.0000 5631.0000 3104 ---
0 0 2265.4000 2 7.0000 2265.4000 3104 ---
* 0+ 0 2265.0000 2265.4000 3107 0.02%
* 0 0 integral 0 2265.0000 2265.4000 3107 0.02%
Elapsed time = 7.59 sec. (2779.25 ticks, tree = 0.00 MB, solutions = 2)
Cover cuts applied: 1
Root node processing (before b&c):
Real time = 7.61 sec. (2792.47 ticks)
Parallel b&c, 8 threads:
Real time = 0.00 sec. (0.00 ticks)
Sync time (average) = 0.00 sec.
Wait time (average) = 0.00 sec.
------------
Total (root+branch&cut) = 7.61 sec. (2792.47 ticks)
I think that's because the form of your model is relatively simple, such as the objective function, and the model scale is rather small. You can test CPLEX with more complicated examples. I used to solve a MIP model with more than 800 thousand constraints and 300 thousand variables.

Arc4random modulo biased

According to this documentation,
arc4random_uniform() is recommended over constructions like arc4random() % upper_bound as it avoids "modulo bias" when the upper bound is not a power of two.
How bad is the bias? For example if I generate random numbers with an upper bound of 6, what's the difference between using arc4random with % and arc4random_uniform()?
arc4random() returns an unsigned 32-bit integer, meaning the values are between
0 and 2^32-1 = 4 294 967 295.
Now, the bias results from the fact that the multiple subintervals created with
modulo are not fitting exactly into the random output range.
Lets imagine for clarity a random generator that creates numbers from 0 to 198
inclusive. You want numbers from 0 to 99, therefore you calculate random() % 100,
yielding 0 to 99:
0 % 100 = 0
99 % 100 = 99
100 % 100 = 0
198 % 100 = 98
You see that 99 is the only number which can occur only once while all
others can occur twice in a run. That means that the probability for 99
is exactly halved which is also the worst case in a bias where at least
2 subintervals are involved.
As all powers of two smaller than the range interval fits nicely into the
2^32 interval, the bias disappears in this case.
The implications are that the smaller the result set with modulo and the higher
the random output range, the smaller the bias. In your example, 6 is your upper
bound (I assume 0 is the lower bound), so you use % 7, resulting that 0-3
occurs 613 566 757 times while 4-6 occurs 613 566 756 times.
So 0-3 is 613 566 757 / 613 566 756 = 1.0000000016298 times more probable
than 4-6.
While it seems easy to dismiss, some experiments (especially Monte-Carlo
experiments) were flawed exactly because these seemingly incredible small
differences were pretty important.
Even worse is the bias if the desired output range is bigger than
the random target range. Please read the Fisher-Yates shuffle entry
because many poker sites learned the hard way that normal linear
congruential random generators and bad shuffling algorithms resulted in
impossible or very probable decks or worse, predictable decks.