Finding all possible sub-optimal(not optimal!!!) solutions in optimization - algorithm

I am writing a CPLEX optimization code to generate a matrix, which takes r and n as the command line arguments, but they may be assumed 2 and 4 for now.
The condition for generating the matrix is that the sum of elements in any row or in any column should equal 10, where the elements are integers between 0 and 10. (i.e. doubly-stochastic matrix)
I turned this condition into the constraint, and generated the matrix, but it only gives a matrix with 10s and 0s.
I think it is because CPLEX always finds the "optimal" solution, but for the problem I want to solve, this is not going to help much.
I want matrices with some 6, 7, 8, 9, 10, and 0~5 for the rest.
I want to generate all possible matrices satisfying such condition (and some more condition to be added later) so that I could test all of them and exhaust the case.
How can I do that?
I am looking into this solution pool thing, and it is not easy..
Also,
cplex.out() << "number of solutions = " << cplex.getSolnPoolNsolns() << endl;
this gives 1... meaning that there is only one solution, while I know there are millions of those matrices.
If you have any ideas how to generate all the 'sub-optimal' matrices, please help me.
Thank you.
I attached my code in IPGenMat.cpp, and aa.sol was the solution it gave me.
I also copied it here below.
(In short, two questions: 1. how can I find 'less optimal' solutions? 2. how can I find all of such solutions?)
#include<ilcplex/ilocplex.h>
#include<vector>
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main(int argc, char** argv) {
if (argc < 2) {
cerr << "Error: " << endl;
return 1;
}
else {
int r, n;
stringstream rValue(argv[1]);
stringstream nValue(argv[2]);
rValue >> r;
nValue >> n;
int N=n*r;
int ds = 10; //10 if doubly-stochastic, smaller if sub-doubly stochastic
IloEnv env;
try {
IloModel model(env);
IloArray<IloNumVarArray> m(env, N);
for (int i=0; i<N; i++) {
m[i] = IloNumVarArray(env, N, 0, 10, ILOINT);
}
IloArray<IloExpr> sumInRow(env, N);
for (int i=0; i<N; i++) {
sumInRow[i] = IloExpr(env);
}
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
sumInRow[i] += m[i][j];
}
}
IloArray<IloRange> rowEq(env, N);
for (int i=0; i<N; i++) {
rowEq[i] = IloRange(env, ds, sumInRow[i], 10); //doubly stochastic
}
IloArray<IloExpr> sumInColumn(env, N);
for (int i=0; i<N; i++) {
sumInColumn[i] = IloExpr(env);
}
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
sumInColumn[i] += m[j][i];
}
}
IloArray<IloRange> columnEq(env, N);
for (int i=0; i<N; i++) {
columnEq[i] = IloRange(env, ds, sumInColumn[i], 10); //doubly stochastic
}
for (int i=0; i<N; i++) {
model.add(rowEq[i]);
model.add(columnEq[i]);
}
IloCplex cplex(env);
cplex.extract(model);
cplex.setParam(IloCplex::SolnPoolAGap,0.0);
cplex.setParam(IloCplex::SolnPoolIntensity,4);
cplex.setParam(IloCplex::PopulateLim, 2100000000);
cplex.populate();//.solve();
cplex.out() << "solution status = " << cplex.getStatus() << endl;
cplex.out() << "number of solutions = " << cplex.getSolnPoolNsolns() << endl;
cplex.out() << endl;
cplex.writeSolutions("aa.sol");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cplex.out() << cplex.getValue(m[i][j]) << " | ";
}
cplex.out() << endl;
}
cplex.out() << endl;
}
catch(IloException& e) {
cerr << " ERROR: " << e << endl;
}
catch(...) {
cerr << " ERROR: " << endl;
}
env.end();
return 0;
}
}

You might try using PORTA's vint utility or PPL for this instead. CPLEX is geared for optimissation problems, not enumeration problems.
I'd add that, while your problem is a tiny optimisation problem, it's a really huge enumeration problem. There are likely to be far more solutions that you'd know what to do with. You might try narrowing down what you want and trying to express that using linear inequalities.

SolnPoolAGap Sets an absolute tolerance on the objective value for the solutions in the solution pool. Solutions that are worse (either greater in the case of a minimization, or less in the case of a maximization) than the objective of the incumbent solution according to this measure are not kept in the solution pool.
So, to obtain sub-optimal solutions you should put a higher value than 0.0
in this parameter

Let's just assume your solution is some matrix with entries m_i_j. Express your problem in terms of a set of binary decision variables, e.g. m_i_j_v meaning "the matrix at row i and column i has value v". Then after you solve the problem, you can take add another constraint that sums over all the decision variables that are set, and force them to be N-1. This will exclude this as the solution. Rinse an Repeat until the problem becomes infeasible.

Related

Finding number of occurrences of elements in array and printing them in in ascending order of elements

So, like the question says I want to arrange the occurrences of these elements in an ascending order of those elements. For example- if I input 7-3 times and 3-2 times, then output should be printed with 3-2 first and then (next line) 7-3. If you see the for loop with the comment to sort through the array, without that for loop the code works fine but doesn't print the elements in an ascending order. Let me know what you think about this and why that for loop isn't working?
#include<stdio.h>
int x;
int main()
{ int a[10000],b[10000],i,j,n,x,c=0,p,q ;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{ c=1;
if(a[i]!=-1)
{ for(j=i+1; j<n; j++)
{
if(a[i]==a[j])
{ c++;
a[j]=-1;
}
}
b[i]=c;
}
}
for (i = 0; i < n; ++i) \\for loop to sort a[i] elements in ascending order
{ for (j = i + 1; j < n; ++j)
{
if (a[i] > a[j])
{
x = a[i];
a[i] = a[j];
a[j] = x;
}
}
}
for(i=0; i<n; i++)
{
if(a[i]!=-1 && b[i]>1)
{
printf("%d-%d\n",a[i],b[i]);
}
}
return 0;
}
You can do it either in O(n * lg n) e.g. using sorting or in expected linear time using std::map, I'm not sure if there is something like this in C.
Example impl. w/ sorting:
#include <iostream>
#include <vector>
#include <algorithm>
std::vector<int> v = {3,7,7,7,3,7,7};
std::sort(v.begin(), v.end());
for (int i = 0; i < v.size(); ++i) {
int number = v[i];
int count = 1;
while (v[i + count] == number) ++count;
i = i + count;
std::cout << number << " " << count << std::endl;
}
If you know that range of elements in the array is small enough you can use radix sort and so get it done in linear time.
About your implementation.
You are good with the first loop.
In the second loop, you need to take into account -1 entries. Also you need to swap not only a but b entries as well.
Check for b[i] equals to 1. You can initialize it to 0 before c=1; and drop b[i] > 1 check.
Few more comments not related to correctness. Do not use magic number -1, give it a name, and then use it. Do not declare all variables at the beginning of the function, declare every variable as close as possible to the first use.

Why inner product of same size matrix in Eigen cost quite different time?

I used Eigen to calculate inner product of two matrix, the first one is A=(BC).eval() and second one is D=(EF).eval(). Here B,C,E,F are the same size (1500 * 1500) but with different values. I find the first one cost about 200 ms while the second one cost about 6000 ms, I have no idea why this happened.
#include <iostream>
#include <time.h>
#include "Eigen/Dense"
int main() {
clock_t start, stop;
Eigen::MatrixXf mat_a(1200, 1500);
Eigen::MatrixXf mat_b(1500, 1500);
Eigen::MatrixXf mat_r(1000, 1300);
int i, j;
float c = 0;
for (i = 0; i < 1200; i++) {
for (j = 0; j < 1500; j++) {
mat_a(i, j) = (float)(c/3 * 1.0e-40);
//if (i % 2 == 0 && j % 2 == 0) mat_a(i, j);
c++;
}
}
//std::cout << mat_a.row(0) << std::endl;
c = 100;
for (i = 0; i < 1500; i++) {
for (j = 0; j < 1500; j++) {
mat_b(i, j) = (float)(c/3 * 0.5e-10);
c++;
}
}
//std::cout << mat_b.row(0) << std::endl;
start = clock();
mat_r = mat_a * mat_b;
stop = clock();
std::cout << stop - start << std::endl;
getchar();
return 0;
}
as show in above example code. I find this is caused by the value of the matrix, when mat_a has value about e-40 and mat_b has value about e-10, this problem occurs stably.
Is there anyone who can explain it?
This is because your matrix contains denormal numbers that are slow to deal with for the CPU. You should make sure that you are using reasonable units so that those can be considered as zeros, and then enable the flush-to-zero (FTZ) and denormals-as-zero flags (DAZ), for instance using the fast-math mode of your compiler or at runtime, see this SO question.

Can somebody tell me what this sorting algorithm is called?

So my teacher told me about the bubble sorting technique and it looks like it runs too many times, so I came up with this, I'm fairly sure that it's already been made and I want to know what it's called.
Here it is:
#include <iostream>
using namespace std;
int main()
{
int n, k = 0, i, min, aux;
cout << "N:";cin >> n;
int v[n];
for(i=0;i<n;i++)
cin >> v[i];
do{
for(i=k;i<n;i++){
if(i==k)
min = i;
if(v[i] < v[min])
min = i;
}
aux = v[k];
v[k] = v[min];
v[min] = aux;
k ++;
}while(k<n-1);
cout << "\n";
for(i=0;i<n-1;i++){
cout << v[i] << ",";
}
cout << v[n-1] << ".";
}
This is called selection sort. Good job coming up with it on your own, you can read about it here.

Nested, dependant for loops: Summation formula and Big-O notation

Working under a time crunch here. Struggling to understand exactly what this problem is asking. Any help or pointers in the right direction would be greatly appreciated! Thanks in advanced.
The original problem is based on this given information:
for (int k = 0; k < 2*n; k++) {
cout << k << endl;
for (int i = k+1; i < n; i++)
{
m[i][j] = a[i][j] + b[i][j];
cout << m[i][j] << endl;
}
cout << i * k << endl;
}
For T(n) = http://www4c.wolframalpha.com/Calculate/MSP/MSP63941h503ff0a609230100002eieg6bhfe5gi70g?MSPStoreType=image/gif&s=23&w=167.&h=49.
And here is my problem:
Modify the code above to find the number of times the basic operation occurs (i.e. how many times does it go in the inner for loop?).
include
using namespace std;
int main()
{
int count = 0;
int n = 10;
for (int k = 0; k < 2*n; k++) {
cout << "outer: " << k << endl;
for (int i = k+1; i < n; i++) {
cout << "\tinner: " << i << endl;
count++;
}
}
cout << count << endl;
}
Write a summation based on the output of Step 1
Based on this, is T(n) equivalent to O(n) or O(n^2)
I'm confused about specifically what part 2 is asking for. But I found:
http://www4c.wolframalpha.com/Calculate/MSP/MSP4561hgb5f47a07e05g00000112a53ahh0670che?MSPStoreType=image/gif&s=30&w=109.&h=49.
To me this looks like O(N^2)?
I apologize for the formatting. I'm on mobile.
Let me see if I guide:
1. I think the count should be inside like this:
int main() {
int count = -1;
int n = 10;
for (int k = 0; k < 2*n; k++) {
count = 0;
cout << "outer: " << k << endl;
for (int i = k+1; i < n; i++) {
cout << "\tinner: " << i << endl;
count++;
}
cout << count << endl; //<<<here
}
}
Now collect the output (#here marker) and form a formula for the summation. I think this is Task#2.
Based on your formula (or summation) you will be able to generalize whether its o(n) or o(n^2).
This is definitely not linear.

Atomic Exchange Sorting Algorithm in MultiGPU

How can atomic exchange sorting algorithm can be implemented in MultiGPU? Is there references available??
It would help if you point out an algorithm that may be used, as a guideline to help answer this question.
So, I pulled an algorithm from: http://www.codingunit.com/exchange-sort-algorithm
Here is the basic algorithm:
int main(void)
{
int array[5]; // An array of integers.
int length = 5; // Lenght of the array.
int i, j;
int temp;
//Some input
for (i = 0; i < 5; i++)
{
cout << "Enter a number: ";
cin >> array[i];
}
//Algorithm
for(i = 0; i < (length -1); i++)
{
for (j=(i + 1); j < length; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//Some output
for (i = 0; i < 5; i++)
{
cout << array[i] << endl;
}
}
You may want to look at this page for some source code that may help:
http://www.bealto.com/gpu-sorting.html
But, if you use OpenCL and the equation above, you may want to do something like this:
Open a connection to each card.
Then, where they have the outer loop, send each of those, perhaps in a round-robin to each card.
You will need to then do a final sort on one GPU to finish, but you may want to use a different algorithm, as this algorithm is best on a single-threaded CPU.

Resources