Home > Software engineering >  I am trying to draw feasible region of these constraints, but I am stuck
I am trying to draw feasible region of these constraints, but I am stuck

Time:10-12

my constraints are x_1 x_2 <= 120, x_1 0.5x_2 <= 65, 15 <= x_1 <= 80, x_1 >= 0 and x_2 >= 0

x_1 = np.linespace(0,30,1000)
x_2 = np.linespace(0,30,1000)

#plot
fig, ax = plt.subplots()
fig.set_size_inches(14.7, 8.27)

# draw constraints
plt.axvline(80, color='g', label=r'$x_1 \geq 80$') # constraint 1
plt.axvline(15, color='g', label=r'$x_1 \geq 15$') # constraint 2
plt.plot(x_1, (120 - x_1) / 3) # constraint 3
plt.plot(x_1, (65 - x_1) / 0.5) # constraint 4
plt.xlim((0, 120))
plt.ylim((0,130))
plt.ylabel(r'x1')
plt.ylabel(r'x2')

# fill in the fesaible region
plt.fill_between(x_1, np.minimum((65 - x_1) / 0.5, (120 - x_1) / 3), np.minimum((65 - x_1) / 0.5, 0),
x_1 >= 15,
color='green', alpha=0.25)
plt.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0.)

# Hide the right and top spines
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()

enter image description here

As you can see from the picture, line does not touch the x axis

CodePudding user response:

Your lines do not reach the x, axis because the upper limit for x_1 and x_2 is 30: x_1 = np.linspace(0,30,1000).

You can extend the range and set plt.ylim(bottom=0) to restrict the visible area to x_2>0:

x_1 = np.linspace(0,80,1000)
x_2 = np.linspace(0,80,1000)

#plot
fig, ax = plt.subplots()
fig.set_size_inches(14.7, 8.27)

# draw constraints
plt.axvline(80, color='g', label=r'$x_1 \geq 80$') # constraint 1
plt.axvline(15, color='g', label=r'$x_1 \geq 15$') # constraint 2
plt.plot(x_1, (120 - x_1) / 3) # constraint 3
plt.plot(x_1, (65 - x_1) / 0.5) # constraint 4
plt.xlim((0, 120))
plt.ylabel(r'x1')
plt.ylabel(r'x2')

# fill in the fesaible region
plt.fill_between(x_1, np.minimum((65 - x_1) / 0.5, (120 - x_1) / 3), np.minimum((65 - x_1) / 0.5, 0),
x_1 >= 15,
color='green', alpha=0.25)
plt.ylim(bottom=0)
plt.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0.)

# Hide the right and top spines
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()

Is this what you are looking for? enter image description here

  • Related