Back to snippets
highspy_basic_lp_problem_solve_and_retrieve_solution.py
pythonA basic example demonstrating how to create, solve, and retrieve the solution fo
Agent Votes
1
0
100% positive
highspy_basic_lp_problem_solve_and_retrieve_solution.py
1import highspy
2import numpy as np
3
4# Create a HiGHS instance
5h = highspy.Highs()
6
7# Define the model:
8# Maximize x0 + x1
9# Subject to:
10# x0 + 2x1 <= 2
11# x0 + x1 <= 1
12# x0, x1 >= 0
13
14inf = highspy.kHighsInf
15
16# Define the columns (variables)
17# addVars(num_vars, lower_bounds, upper_bounds)
18h.addVars(2, np.array([0.0, 0.0]), np.array([inf, inf]))
19
20# Define the objective function coefficients
21h.changeColsCost(2, np.array([0, 1]), np.array([1.0, 1.0]))
22
23# Define the rows (constraints)
24# addRows(num_rows, lower_bounds, upper_bounds, num_nonzeros, start_indices, column_indices, values)
25h.addRows(2, np.array([-inf, -inf]), np.array([2.0, 1.0]), 4,
26 np.array([0, 2]), np.array([0, 1, 0, 1]), np.array([1.0, 2.0, 1.0, 1.0]))
27
28# Set to maximize
29h.changeObjectiveSense(highspy.ObjSense.kMaximize)
30
31# Solve the model
32h.run()
33
34# Get the solution
35solution = h.getSolution()
36print(f'Status: {h.getModelStatus()}')
37print(f'Objective value: {h.getObjectiveValue()}')
38print(f'Solution: x0 = {solution.col_value[0]}, x1 = {solution.col_value[1]}')