Linprog gives x values not in bounds #1414
Unanswered
Keshabl
asked this question in
Problems using YALMIP
Replies: 1 comment
-
Wrong forum (but -10^-16 is 0 to any solver using standard numerics if that what you are asking) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Im trying to make code that can solve sudoku by using this math model
i,j,k are raws, columns and values
so i wrote code using it
`
import numpy as np
from scipy.optimize import linprog
import networkx as nx
import matplotlib.pyplot as plt
S=1
sud1 = [[2, 0, 5, 0, 0, 9, 0, 0, 4],
[0, 0, 0, 0, 0, 0, 3, 0, 7],
[7, 0, 0, 8, 5, 6, 0, 1, 0],
[4, 5, 0, 7, 0, 0, 0, 0, 0],
[0, 0, 9, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 2, 0, 8, 5],
[0, 2, 0, 4, 1, 8, 0, 0, 6],
[6, 0, 8, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 2, 0, 0, 7, 0, 8]]
c = [0]999S
for i in range (0,9):
for j in range(0, 9):
if sud1[i][j] != 0:
c[i81+j9+sud1[i][j]-1] = 1
Aeq10 = np.zeros((81,999*S))
for j in range (1,10):
for k in range (1,10):
for i in range (1,10):
Aeq10[(j-1)*9+(k-1)][(i-1)*81+(j-1)*9+(k-1)] = 1
Aeq11 = np.zeros((81,999*S))
for i in range (1,10):
for k in range (1,10):
for j in range (1,10):
Aeq11[(i-1)*9+(k-1)][(i-1)*81+(j-1)*9+(k-1)] = 1
Aeq12 = np.zeros((81,999*S))
for i in range (1,10):
for j in range (1,10):
for k in range (1,10):
Aeq12[(i-1)*9+(j-1)][(i-1)*81+(j-1)*9+(k-1)] = 1
Aeq13 = np.zeros((9,999S))
for u in range (1,4):
for v in range(1,4):
for i in range (u3-2,u3+1):
for j in range (v3-2,v*3+1):
for k in range (1,10):
Aeq13[(u-1)*3+(v-1)][(i-1)*81+(j-1)*9+(k-1)] = 1
Aeq = np.vstack((Aeq10,Aeq11,Aeq12,Aeq13))
Beq = np.zeros(81+81+81+9)
for i in range (1,81+81+81+1):
Beq[i-1] = 1
for i in range (81+81+81+1,81+81+81+9+1):
Beq[i-1] = 9
bounds = (0,1)
res = linprog(c=c, A_eq=Aeq, b_eq=Beq, bounds=bounds, integrality=np.ones(999))
x = res['x']
sud = np.zeros((9,9))
for i in range (0,9):
for j in range (0,9):
for k in range (0,9):
if x[i81+j9+k] == 1:
sud [i][j] = k+1
print(sud)
print(x)
`
and everithing looks allright, but in the end im noticing that despite i put bounds to (0,1) and integrality to 1, my x values can vary above and below bounds
like that
![image](https://private-user-images.githubusercontent.com/83855659/335877198-d001c186-1040-4a0a-b528-84762a768571.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxMzcyODgsIm5iZiI6MTczOTEzNjk4OCwicGF0aCI6Ii84Mzg1NTY1OS8zMzU4NzcxOTgtZDAwMWMxODYtMTA0MC00YTBhLWI1MjgtODQ3NjJhNzY4NTcxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA5VDIxMzYyOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQxMjliMGQyYjE2ZTU3OWEyODdhODZmYThiZDcwZDI4N2RlMzM0MzllZTdlMjAzMzdmY2E2NTAwNGI4NmYwOGImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.4BtPZyxGW4WirzKz2swrM720526OBSomC5GlKY6gEYM)
can somebody help me to understand what am i doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions