Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Variable returning value 0.502.... #14

Closed
shivang94 opened this issue Jun 12, 2017 · 4 comments
Closed

Binary Variable returning value 0.502.... #14

shivang94 opened this issue Jun 12, 2017 · 4 comments

Comments

@shivang94
Copy link

implicit val problem = LQProblem(SolverLib.ojalgo)
// Data (filled randomly)
val numI = 5
val I = 0 until numI
val numJ = 5
val J = 0 until numJ
var q = Array.tabulate(numI)(x=>(x+1)0.2)
var w = Array.tabulate(numI)(_0.1)
var to = Array.tabulate(numI,numJ)((x,y)=>x+y)
var r = Array.tabulate(numI,numJ)((x,y)=>0.1
x+0.2
y)
var c = Array.tabulate(numI,numJ)((x,y)=>x+2*y)
var B = 50
var T = Array.tabulate(numJ)(x => (x+2)*3)
var M = 1000

//Variables
val x = Array.tabulate(numI,numJ)((i,j) => MPIntVar(s"x($i,$j)", 0 to 1))
val y = Array.tabulate(numI)(i=>MPFloatVar(s"y$i"))
val z = Array.tabulate(numI)(i=>MPIntVar(s"z$i", 0 to 1))

// Optimal Function
maximize(sum(I,J){ (i,j) => w(i)*y(i) })

// Constraints
for ( j <- J ) {
add(sum(I)(i => to(i)(j)x(i)(j))<:=T(j))
}
for ( i <- I ) {
add(y(i) <:= q(i) + M
z(i))
add(y(i) <:= sum(J)(j => c(i)(j)x(i)(j)) + M(1-z(i)))
add(sum(J)(j => x(i)(j)) <:= z(i))
}
add(sum(I,J){(i,j) => c(i)(j)*x(i)(j)} <:= B)

The above code has x and z as binary variables and I am getting values of 0.502 and 1

@vagmcs
Copy link
Owner

vagmcs commented Jun 12, 2017

Hello,

I think there are some things missing from the code above. For example what is the symbol Mz? Do you mean M? In the expression var w = Array.tabulate(numI)(_0.1) there is an operator missing between the placemarker _ and 0.1. There are other similar cases. Please check the code again and send me a revised version.

Thank you

@shivang94
Copy link
Author

Oh I'm sorry it got lost in Markdown styling.

Here is the correct version:

implicit val problem = LQProblem(SolverLib.ojalgo)

// Data (filled randomly)
val numI = 5
val I = 0 until numI
val numJ = 5
val J = 0 until numJ
var q = Array.tabulate(numI)(x=>(x+1) * 0.2)
var w = Array.tabulate(numI)(x=>(x+1) * 0.1)
var to = Array.tabulate(numI,numJ)((x,y)=>x+y)
var r = Array.tabulate(numI,numJ)((x,y)=>0.1 * x+0.2 * y )
var c = Array.tabulate(numI,numJ)((x,y)=>x+1+2*y)
var B = 50
var T = Array.tabulate(numJ)(x => (x+2)*3)
var M = 1000

//Variables
val x = Array.tabulate(numI,numJ)((i,j) => MPIntVar( 0 to 1))
val y = Array.tabulate(numI)(i=>MPFloatVar(s"y$i"))
val z = Array.tabulate(numI)(i=>MPIntVar(s"z$i", 0 to 1))

// Optimal Function
maximize(sum(I,J){ (i,j) => w(i)*y(i) })

// Constraints
for ( j <- J ) {
add(sum(I)(i => to(i)(j)*x(i)(j))<:=T(j))
}
for ( i <- I ) {
add(y(i) <:= q(i) + M * z(i))
add(y(i) <:= sum(J)(j => c(i)(j)*x(i)(j)) + M * (1-z(i)))
add(sum(J)(j => x(i)(j)) <:= z(i))
}
add(sum(I,J){(i,j) => c(i)(j)*x(i)(j)} <:= B)

@vagmcs
Copy link
Owner

vagmcs commented Jun 12, 2017

Hello,

The problem is that you define an LQProblem instead of an MIProblem. LQProblem objects are intended for linear programming and use continuous variables, while MIProblem objects represent mixed-integer programming problems and can handle both continuous and integer variables. Nevertheless, maybe these two objects should be merged in order to make the API simpler, or some warning should notify the user for defining float variables in an LQProblem enviroment. I will try to fix this, but for now please just change the first line of your code into:

implicit val problem = MIProblem(SolverLib.ojalgo)

this should work. :)

@vagmcs vagmcs closed this as completed Jun 12, 2017
@shivang94
Copy link
Author

Thanks a lot. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants