Conditional summation in CPLEX using OPL - sum

I'm trying write in OPL this sum:
I did this, but it is not exactly what I need.
forall (n in cont, t in tempo, o in portos)
sum(i in colunap, j in linhap)b[i][j][n][t] + v[n][t] == 1;
I should be something like, but opl does not accept it:
forall (n in cont[o], t in tempo[o], o in portos)
sum(i in colunap[o], j in linhap[o])b[i][j][n][t] + v[n][t] == 1;

This should work:
int P=3;
int H[1..P-1] = [1 , 2];
range linhap=1..max(o in 1..P-1) H[o];

Related

Cplex optimization--"No Value" for my decision variable

I am new to Cplex optimization.
I am trying to implement an optimization problem with some scenarios. it is a two-stage stochastic model with 5 scenarios and the probabilty of occurrence the scenarios.
I wrote my model with 5 scenarios, parameters, and variables, and constraints. I get the following message "No Value" for my decision variable. I think my model does not work and I do not know what should I change in my Codes?. Is there somebody to help me? Thank you very much.
--Parameter--
int NbWarehause=3;
int NbRegion=138;
int NbSchool=631;
int NbScenario=5;
range Warehouse=1..NbWarehause;
range Region=1..NbRegion;
range School=1..NbSchool;
range Scenario=1..NbScenario;
int TravelDistanceWarehouseRegion[Warehouse][Region]=...;
int CapacitySchool[School] =...;
int ScenarioDemandMatrix[Scenario][Region]=...;
int Fixedcost1 = 14232;
float Transportcost1perkm=1.40;
int Fixedcost2 = 14232;
float Transportcost2perkm=3;
int Unusedcostperitem=50;
int Depriviationcost[Region]=...;
int Penaltycost=100;
float ProbabilityScenario[Scenario]=...;
--Decision variables---
dvar boolean open1[School][Region];
dvar int Allocated1[School][Region];
dvar boolean open2[School][Region];
dvar int Allocated2[School][Region];
dvar int UnusedInventory[School][Region];
dvar int LateSatisfiedDemand[Region];
dvar int UnSatisfiedDemand[Region];
--Objective function--
minimize --the first stage--
sum(j in School, r in Region) Fixedcost1 * open1[j][r] +
sum( j in School, w in Warehouse, r in Region) Allocated1[j][r] *
TravelDistanceWarehouseRegion[w][r]*Transportcost1perkm +
--the second stage--
sum(s in Scenario) ProbabilityScenario[s]*(
sum(j in School,r in Region)Fixedcost2 *open2[j][r]
+sum( j in School, w in Warehouse,r in Region) Allocated2[j]
[r]*TravelDistanceWarehouseRegion[w][r]*Transportcost2perkm
+sum( j in School,r in Region)UnusedInventory[j][r]*Unusedcostperitem
+sum(r in Region) Depriviationcost[r]*LateSatisfiedDemand[r]+
sum(r in Region)UnSatisfiedDemand[r]*Penaltycost );
--Constraint--
subject to
{
//C1: capacity of each school in its region//
forall (r in Region ) sum (j in School) (Allocated1[j]
[r]+Allocated2[j] [r]+UnusedInventory[j][r])== sum (j in
School)CapacitySchool[j];
//C2: Demand of each region //
forall (s in Scenario,r in Region) sum (j in School)(Allocated1[j]
[r]+Allocated2[j][r])+LateSatisfiedDemand[r]+UnSatisfiedDemand[r] ==
ScenarioDemandMatrix[s][r];
//C3: open a school maximal one time //
sum (j in School,r in Region ) (open1[j][r]+open2[j][r]) <= 1;
//C4: school can not supply more than its capacity in the second
stage I dont know how do I write under scenario //
forall (j in School,r in Region)Allocated2[j][r]<=CapacitySchool[j] -
Allocated1[j][r]*(open1[j][r]+open2[j][r]);
//C5: Sum of all probability is equal 1
sum (s in Scenario)ProbabilityScenario[s]==1;
// C6: Nonnegative Constraint
forall (r in Region ,j in School) Allocated1[j][r]>=0;
forall (r in Region ,j in School)Allocated2[j][r]>=0;
forall (r in Region ,j in School)UnusedInventory[j][r]>=0;
forall (r in Region)LateSatisfiedDemand[r]>=0;
forall (r in Region) UnSatisfiedDemand[r]>=0;
}
I guess your model is not feasible.
To understand why you could name your constraints and then CPLEX will provide you with some relaxations and conclicts.
As a start you could change
//C3: open a school maximal one time //
sum (j in School,r in Region ) (open1[j][r]+open2[j][r]) <= 1;
into
//C3: open a school maximal one time //
C3:sum (j in School,r in Region ) (open1[j][r]+open2[j][r]) <= 1;
And then rely on
https://www.ibm.com/support/pages/display-full-indices-or-real-map-item-name-variables-and-constraints
to get the real indexes

Indexed variable in AMPL

I have the following model with a variable that is a value from a vector (index of p in objective function)
But AMPL displays an error: subscript variables are not yet allowed.
How can I do to implement this kind of addressing in objective function?
Thanks in advance and best regards.
Gabriel
param dimension;
set T:={1..dimension};
set O:={0};
set V:= O union T;
param c{i in V, j in V};
param p{i in V};
set ady{i in V} within V := {j in V : i<>j and c[i,j] <> -1} ;
# Variables
var x{i in V, j in V} binary;
var u{i in V} integer;
# Objective
minimize costo: sum{i in V, j in V} p[u[i]-1] * x[i,j] * c[i,j];
# Constraints
s.t. grado_a {j in V} : sum{i in ady[j] : j <> i} x[i,j] = 1;
s.t. grado_b {i in V} : sum{j in ady[i] : i <> j} x[i,j] = 1;
s.t. origen {i in O} : u[i] = 0;
s.t. sigo_1 {i in T} : u[i] >=1;
s.t. sigo_2 {i in T} : u[i] <= card(V) -1;
s.t. precedencia {i in T, j in T : i <> j} : u[i] - u[j] + 1 <= (card(V) - 1)*(1 - x[i,j]) ;
AMPL doesn't allow variables in subscripts yet. However, the ilogcp driver for AMPL supports the element constraint, for example:
include cp.ampl;
minimize costo:
sum{i in V, j in V} element({v in V} p[v], u[i] - 1) * x[i,j] * c[i,j];
where element({v in V} p[v], u[i] - 1) is equivalent to p[u[i] - 1] and is translated into an IloElement constraint.

Summation on 1 <= i < j < k <= n in GLPK

I have been trying to solve seriation problem by using GNU. But I couldn't write a summation like the following.
param n, integer, >= 3;
set O := 1..n;
param d{i in O,j in O};
var x{i in O,j in O}, binary, i < j;
var v{i in O,j in O,k in O}, binary, i < j < k;
maximize total: sum{i in O,j in O, i<j}(d[i,j] - d[j,i])* x[i,j] + sum{i in O,j in O, i<j}d[j,i];
s.t. tran{i in O,j in O,k in O, i<j<k}: x[i,j] + x[j,i] - x[i,k] + v[i,j,k] = 1;
Thanks
You should use : instead of , in the "such that" clause i < j:
sum{i in O,j in O: i < j} ...
# ^ note ':' here

What would be the Growth Rate of the following function

What would be the growth rate of the following function in terms of Big O notation??
f (n) = Comb(1000,n) for n = 0,1,2,…
int Comb(int m, int n)
{
int pracResult = 1;
int i;
if (m > n/2) m = n-m;
for (i=1; i<= m; i++)
{
pracResult *= n-m+i;
pracResult /= i;
practicalCounter++;
}
return pracResult;
}
Recursive:
int combRecursive (int m, int n)
{
recursiveCounter++;
if (n == m) return 1;
if (m == 1) return n;
return combRecursive(n-1, m) + combRecursive(n-1, m-1);
}
I would guess n^2??? I am probably wrong though... I have always struggled to figure out how efficient things are...
Thank you in advanced.
It's O(1).
By definition, f(n) = O(g(n)) if there exists a c such that for all n, f(n) <= c*g(n)
Let c = Comb(1000,500)
For all n, Comb(1000, n) < c * 1. Hence Comb(1000, n) = O(1)
For n = 1 to 2000 there will operations proportional to n
For all n > 2000, total operations are constant.
Hence function complexity is O (1)
And I have to tell you that you gotta read some books. :)
Data-structure and algorithm by Sahni is very light read.
Algorithms by Knuth is very heavy, but amongst best.

How to optimize code for finding Amicable Pairs

Please see the code I've used to find what I believe are all Amicable Pairs (n, m), n < m, 2 <= n <= 65 million. My code: http://tutoree7.pastebin.com/wKvMAWpT. The found pairs: http://tutoree7.pastebin.com/dpEc0RbZ.
I'm finding that each additional million now takes 24 minutes on my laptop. I'm hoping there are substantial numbers of n that can be filtered out in advance. This comes close, but no cigar: odd n that don't end in '5'. There is only one counterexample pair so far, but that's one too many: (34765731, 36939357). That as a filter would filter out 40% of all n.
I'm hoping for some ideas, not necessarily the Python code for implementing them.
Here is a nice article that summarizes all optimization techniques for finding amicable pairs
with sample C++ code
It finds all amicable numbers up to 10^9 in less than a second.
#include<stdio.h>
#include<stdlib.h>
int sumOfFactors(int );
int main(){
int x, y, start, end;
printf("Enter start of the range:\n");
scanf("%d", &start);
printf("Enter end of the range:\n");
scanf("%d", &end);
for(x = start;x <= end;x++){
for(y=end; y>= start;y--){
if(x == sumOfFactors(y) && y == sumOfFactors(x) && x != y){
printf("The numbers %d and %d are Amicable pair\n", x,y);
}
}
}
return 0;
}
int sumOfFactors(int x){
int sum = 1, i, j;
for(j=2;j <= x/2;j++){
if(x % j == 0)
sum += j;
}
return sum;
}
def findSumOfFactors(n):
sum = 1
for i in range(2, int(n / 2) + 1):
if n % i == 0:
sum += i
return sum
start = int(input())
end = int(input())
for i in range(start, end + 1):
for j in range(end, start + 1, -1):
if i is not j and findSumOfFactors(i) == j and findSumOfFactors(j) == i and j>1:
print(i, j)