Python Forum
Solve a system of linear equations with binary variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solve a system of linear equations with binary variables
#1
I'm having trouble defining an algorithm that can solve a system of linear equations with binary variables.
Example given the following system of linear equations:

x_12 + x_18 + x_28 = 0
x_12 - x_18 - x_28 = 0
x_12 + x_13 + x_28 - x_38 = 0
-x_13 + x_14 - x_38 - x_48 = - 2
-x_14 + x_15 - x_48 - x_58 = -2
x_15 + x_16 + x_58 - x_68 = 0

Knowing that the values of x_12 = 0, x_18 = 0 and x_28 = 0, it is possible to create an algorithm that solves this system with the other variables assuming only values of 0 or 1.
Reply
#2
First two equations could be dropped out, since you already know values of corresponding variables.
If I understand you correctly, you have (x_13, x_38, x_14, x_15, x_48, x_58, x_16, x_68).
You have 8 variables and their allowed values are 0 or 1. 2 ** 8 possible combinations.
So, you can use brute-force algorithm to solve these equations. If we ever consider all variables (11),
there are not too much combinations (2**11) for modern computers to check.
Reply
#3
i'm using sympy, what i needed is a way to determine a binary domain for the variables to assume only the value of 0 or 1

variables = x_12,x_13,x_14,x_15,x_16,x_18,x_28,x_38,x_48,x_58,x_68
equations = (x_12 + x_18 + x_28, 
             x_12 - x_18 - x_28, 
             x_12 + x_13 + x_28 - x_38, 
             -x_13 + x_14 - x_38 - x_48 + 2,
             -x_14 + x_15 - x_48 - x_58 + 2,
             x_15 + x_16 + x_58 - x_68)



solve((equations),variables)
Reply
#4
I didn't use SymPy for a while, maybe something FiniteSet (and solveset) will be helpful...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SOlving LInear Equations in Python(Symoy, NUmpy) - Coefficient Problem quest 3 1,729 Jan-30-2022, 10:53 PM
Last Post: quest
Heart how to solve complex equations in python HoangF 3 2,781 Dec-26-2021, 07:04 PM
Last Post: HoangF
Question Help with code to solve polynomials equations hiviera 1 1,785 Jul-31-2021, 01:56 AM
Last Post: hiviera
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,604 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Solve system of equations Sancho_Pansa 19 8,886 Oct-27-2020, 08:15 AM
Last Post: Sancho_Pansa
  How to solve equations, with groups of variables and or constraints? ThemePark 0 1,679 Oct-05-2020, 07:22 PM
Last Post: ThemePark
  Plotting 3D surface plot for non-linear multivariate regression with 5 variables khwajaosama 0 2,702 Jul-02-2020, 04:50 AM
Last Post: khwajaosama
  How to solve difficult non-linear equations? shreeniket 3 2,378 Apr-23-2020, 01:36 AM
Last Post: shreeniket
  hex file to binary or pcap to binary baran01 1 5,684 Dec-11-2019, 10:19 PM
Last Post: Larz60+
Question Difference between Python's os.system and Perl's system command Agile741 13 6,798 Dec-02-2019, 04:41 PM
Last Post: Agile741

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020