metab0t / pyoptinterface Goto Github PK
View Code? Open in Web Editor NEWEfficient modeling interface for mathematical optimization in Python
Home Page: https://metab0t.github.io/PyOptInterface/
License: Other
Efficient modeling interface for mathematical optimization in Python
Home Page: https://metab0t.github.io/PyOptInterface/
License: Other
您好!我对于求和的部分还有些疑问,如果您能不吝赐教,我将非常感激。
第一个问题关于求和方式。我从您的文档中学到了两种求和的方法,第一种是用ExprBuilder,比如
expr = poi.ExprBuilder()
for y in range(100):
expr += block.μ[y]
这里block是一个SimpleNamespace
第二种是用poi.quicksum。从您的例子中看主要是用在tupledict中。
我的问题是,如果我想求若干变量之和,用哪种方法比较好?
比如 x = [model.add_variable(lb=0) for _ in range(10)],然后我想限制这些x的和小于等于10。那我应该写:
expr = poi.ExprBuilder()
for i in range(10):
expr += x[i]
model.add_linear_constraint(expr, poi.Eq, 10)
还是:
model.add_linear_constraint(poi.quick_sum(x), poi.Eq, 10)
第二个问题是quicksum与quicksum_,这两个很相似函数的适用类型分别是什么?他们有什么区别?
谢谢!
Hey, nice library! I'm having a play with it to see how it performs with a very simple model with varying numbers of variables.
With the script below I'm getting these results
100 vars: 2.49 ms
1000 vars: 9.67 ms
10000 vars: 717.49 ms
20000 vars: 2794.24 ms
50000 vars: 17723.34 ms
It's looking like the runtime is very non-linear. The same problem in other similar solvers tends to increase quite linearly.
Profiling this via kernprof -l -v example.py
(pip install line_profiler
first and decorate run
with @profile
) I see that the bulk of the time seems to be spent in the optimisation:
Timer unit: 1e-06 s
Total time: 20.9067 s
File: example.py
Function: run at line 6
Line # Hits Time Per Hit % Time Line Contents
==============================================================
6 @profile
7 def run(n_vars: int):
8 5 2.0 0.4 0.0 t1 = time.perf_counter()
9 5 604.0 120.8 0.0 model = highs.Model()
10 5 184.0 36.8 0.0 model.set_raw_parameter("log_to_console", False)
11 5 6.0 1.2 0.0 model.set_raw_parameter("output_flag", False)
12
13 5 129217.0 25843.4 0.6 x = model.add_variables(list(range(n_vars)), lb=0.0, ub=1.0)
14 5 10588.0 2117.6 0.1 model.add_linear_constraint(poi.quicksum(x), poi.Eq, 1.0)
15 5 31477.0 6295.4 0.2 objective = poi.quicksum([i * x[i] for i in range(n_vars)])
16 5 1035.0 207.0 0.0 model.set_objective(objective, poi.ObjectiveSense.Maximize)
17 5 20733173.0 4146634.6 99.2 model.optimize()
18
19 5 276.0 55.2 0.0 assert math.isclose(model.get_value(objective), n_vars - 1)
20 5 6.0 1.2 0.0 t2 = time.perf_counter()
21 5 123.0 24.6 0.0 print(f"{n_vars} vars:\t{(t2 - t1) * 1000:.2f} ms")
Do you have any advice on whether there's any change I could make to my code to improve it?
import math
import time
import pyoptinterface as poi
from pyoptinterface import highs
def run(n_vars: int):
t1 = time.perf_counter()
model = highs.Model()
model.set_raw_parameter("log_to_console", False)
model.set_raw_parameter("output_flag", False)
x = model.add_variables(list(range(n_vars)), lb=0.0, ub=1.0)
model.add_linear_constraint(poi.quicksum(x), poi.Eq, 1.0)
objective = poi.quicksum([i * x[i] for i in range(n_vars)])
model.set_objective(objective, poi.ObjectiveSense.Maximize)
model.optimize()
assert math.isclose(model.get_value(objective), n_vars - 1)
t2 = time.perf_counter()
print(f"{n_vars} vars:\t{(t2 - t1) * 1000:.2f} ms")
for n_vars_ in (100, 1_000, 10_000, 20_000, 50_000):
run(n_vars_)
import pyoptinterface as poi
from pyoptinterface import highs
model = highs.Model()
x = model.add_variable(lb=0, ub=1, domain=poi.VariableDomain.Continuous, name="x")
y = model.add_variable(lb=0, ub=1, domain=poi.VariableDomain.Integer, name="y")
con = model.add_linear_constraint(x+y, poi.Geq, 1.2, name="con")
obj = 2*x
model.set_objective(obj, poi.ObjectiveSense.Minimize)
model.set_model_attribute(poi.ModelAttribute.Silent, False)
model.optimize()
print(model.get_model_attribute(poi.ModelAttribute.TerminationStatus))
# TerminationStatusCode.OPTIMAL
x_val = model.get_value(x)
# 0.2
y_val = model.get_value(y)
# 1.0
When running this example, Jupyter kernel crashes in model.optimize()
The Kernel crashed while executing code in the current cell or a previous cell.
Please review the code in the cell(s) to identify a possible cause of the failure.
Click here for more info.
View Jupyter log for further details.
17:39:18.153 [error] Disposing session as kernel process died ExitCode: 3221225477, Reason:
Windows 11, VSCode, Python 3.11.6
Following our previous discussion on HiGHS repo, can callback be supported for HiGHs as well?
I can help with testing.
您好! 我对你的包很感兴趣,最近在尝试用。我目前发现了一个小问题。可能poi (pyoptinterface)与 pyoptinterface.highs.Model的有些属性是动态生成的?比如:
model = highs.Model()
model.add_linear_constraint(...)
这段代码中,pyplance无法识别add_linear_constraint这个类方法。可能这个方法是用C++代码动态生成的?
但这样会提高在撰写代码时的错误率,因为pylance无法识别该方法,无法自动补全,或者将之用代表类方法的颜色高亮显示。
不知道您有没有计划解决这个小问题。
同样的问题也出现在poi.VariableDomain
cplex is widely used in power and other industries, but it is not supported, will cplex be supported in future?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.