-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_memory_interface.py
More file actions
177 lines (136 loc) · 4.96 KB
/
example_memory_interface.py
File metadata and controls
177 lines (136 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Example demonstrating the new in-memory interface for benpy.
This example shows how to solve vector linear programs (VLPs) directly
from numpy arrays without creating temporary files.
"""
import numpy as np
import sys
sys.path.insert(0, 'src')
import benpy
def example_basic():
"""Basic example: minimize two objectives subject to linear constraints"""
print("=" * 70)
print("Example 1: Basic bi-objective optimization")
print("=" * 70)
# Problem: minimize [x1, x2] subject to:
# 2*x1 + x2 <= 4
# x1 + 2*x2 <= 4
# x1, x2 >= 0
B = np.array([[2.0, 1.0],
[1.0, 2.0]])
P = np.array([[1.0, 0.0],
[0.0, 1.0]])
b = np.array([4.0, 4.0])
l = np.array([0.0, 0.0])
print("\nProblem:")
print(" minimize [x1, x2]")
print(" subject to:")
print(" 2*x1 + x2 <= 4")
print(" x1 + 2*x2 <= 4")
print(" x1, x2 >= 0")
# Solve using new in-memory interface
sol = benpy.solve(B, P, b=b, l=l, opt_dir=1)
print(f"\nSolution:")
print(f" Status: {sol.Primal.vertex_type}")
print(f" Number of efficient points: {len(sol.Primal.vertex_value)}")
print(f" Duality parameter: {sol.c}")
def example_direct_structure_access():
"""Example showing direct access to C structures"""
print("\n" + "=" * 70)
print("Example 2: Direct access to problem structures")
print("=" * 70)
from benpy import _cVlpProblem
# Create problem
B = np.array([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]])
P = np.array([[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]])
b = np.array([10.0, 20.0])
# Initialize problem
prob = _cVlpProblem()
prob.from_arrays(B, P, b=b, opt_dir=1)
# Access problem properties directly
print("\nProblem dimensions:")
print(f" Number of constraints (m): {prob.m}")
print(f" Number of variables (n): {prob.n}")
print(f" Number of objectives (q): {prob.q}")
print(f" Non-zero entries in constraints: {prob.nz}")
print(f" Non-zero entries in objectives: {prob.nzobj}")
# Access problem matrices
print(f"\nConstraint matrix shape: {prob.constraint_matrix.shape}")
print(f"Objective matrix shape: {prob.objective_matrix.shape}")
print("\nConstraint matrix B:")
print(prob.constraint_matrix)
print("\nObjective matrix P:")
print(prob.objective_matrix)
def example_custom_ordering_cone():
"""Example with custom ordering cone"""
print("\n" + "=" * 70)
print("Example 3: Custom ordering cone")
print("=" * 70)
# Problem with custom ordering cone
B = np.array([[1.0, 1.0]])
P = np.array([[1.0, 0.0],
[0.0, 1.0]])
b = np.array([1.0])
l = np.array([0.0, 0.0])
# Define custom ordering cone (e.g., polyhedral cone)
Y = np.array([[1.0, 1.0],
[0.0, 1.0]])
print("\nProblem with custom ordering cone:")
print(f" Cone generators Y:\n{Y}")
sol = benpy.solve(B, P, b=b, l=l, Y=Y, opt_dir=1)
print(f"\nSolution with custom cone:")
print(f" Efficient points found: {len(sol.Primal.vertex_value)}")
def example_performance_comparison():
"""Compare performance of file-based vs memory-based approach"""
print("\n" + "=" * 70)
print("Example 4: Performance comparison")
print("=" * 70)
import time
# Larger problem
n = 50
m = 20
q = 3
np.random.seed(42)
B = np.random.rand(m, n)
P = np.random.rand(q, n)
b = np.random.rand(m) * 10
l = np.zeros(n)
s = np.ones(n) * 5
print(f"\nProblem size:")
print(f" {m} constraints, {n} variables, {q} objectives")
# Legacy approach (file-based)
print("\nLegacy solve_legacy() with file I/O:")
t1 = time.time()
prob = benpy.vlpProblem(B=B, P=P, b=b, l=l, s=s, opt_dir=1)
sol1 = benpy.solve_legacy(prob)
t_file = time.time() - t1
print(f" Time: {t_file:.4f} seconds")
# New approach (in-memory)
print("\nNew solve() without file I/O:")
t2 = time.time()
sol2 = benpy.solve(B, P, b=b, l=l, s=s, opt_dir=1)
t_mem = time.time() - t2
print(f" Time: {t_mem:.4f} seconds")
print(f"\nSpeedup: {t_file/t_mem:.2f}x faster!")
print(f"Results identical: {len(sol1.Primal.vertex_type) == len(sol2.Primal.vertex_type)}")
if __name__ == "__main__":
print("\n" + "=" * 70)
print("BENPY In-Memory Interface Examples")
print("bensolve 2.1.0 Cython wrapper")
print("=" * 70)
try:
example_basic()
example_direct_structure_access()
example_custom_ordering_cone()
example_performance_comparison()
print("\n" + "=" * 70)
print("All examples completed successfully!")
print("=" * 70 + "\n")
except Exception as e:
print(f"\n\nExample failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)