From a04d1df9490c7e0247783cb2a62df811788c15d8 Mon Sep 17 00:00:00 2001 From: Ling Jin <ling.jin1@rwth-aachen.de> Date: Tue, 13 Dec 2022 10:48:39 +0100 Subject: [PATCH] The first commit 1 --- .idea/.name | 1 + .idea/{pythonProject.iml => Example.iml} | 0 .idea/modules.xml | 2 +- .idea/vcs.xml | 2 +- Example.py | 35 ++++++++++++++++++++++++ main.py | 29 -------------------- 6 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 .idea/.name rename .idea/{pythonProject.iml => Example.iml} (100%) create mode 100644 Example.py delete mode 100644 main.py diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..3f79d09 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Example.py \ No newline at end of file diff --git a/.idea/pythonProject.iml b/.idea/Example.iml similarity index 100% rename from .idea/pythonProject.iml rename to .idea/Example.iml diff --git a/.idea/modules.xml b/.idea/modules.xml index e15ec35..545a376 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ <project version="4"> <component name="ProjectModuleManager"> <modules> - <module fileurl="file://$PROJECT_DIR$/.idea/pythonProject.iml" filepath="$PROJECT_DIR$/.idea/pythonProject.iml" /> + <module fileurl="file://$PROJECT_DIR$/.idea/Example.iml" filepath="$PROJECT_DIR$/.idea/Example.iml" /> </modules> </component> </project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="VcsDirectoryMappings"> - <mapping directory="$PROJECT_DIR$" vcs="Git" /> + <mapping directory="" vcs="Git" /> </component> </project> \ No newline at end of file diff --git a/Example.py b/Example.py new file mode 100644 index 0000000..c2b02ea --- /dev/null +++ b/Example.py @@ -0,0 +1,35 @@ +#导入包 +#pyomo environment +from pyomo.environ import * +#pyomo optimization +from pyomo.opt import SolverFactory + +#实例化一个具体model,一般的最优化问题用到的都是concrete model,而不是abstract model。 +#create a model 实例化类 +model = ConcreteModel() + +#declare decision variables 宣布决策变量 +#domain 域 +model.x = Var(domain = NonNegativeReals) +model.y = Var(domain = NonNegativeReals) + +#对于没有index的版本,直接列公式即可。 +#expr是objective function的公式,sense是我们要最大化还是最小化 +model.obj = Objective(expr=3 * model.x + 4 * model.y, sense=minimize) + +#declare constraint 宣布约束条件 +model.con1 = Constraint(expr=2 * model.x + model.y >= 15) + +model.con2 = Constraint(expr=model.x + 2 * model.y >= 17) + +model.con3 = Constraint(expr=model.x + model.y >= 10) + +#SolverFactory通过使用给出的求解器名称的参数用来创建执行优化的对象。 +result = SolverFactory("gurobi").solve(model) + +#pprint():Data pretty printer。模块提供了美化打印任意python数据类型的功能,让显示结果显得更加漂亮" +#print()输出结果都在一行,不方便查看,而pprint采用了分行打印输出 +model.pprint() + +#solve函数将结果加载到model中,因此以下一行写出更新后的值。 +model.display() diff --git a/main.py b/main.py deleted file mode 100644 index 8141457..0000000 --- a/main.py +++ /dev/null @@ -1,29 +0,0 @@ -<<<<<<< HEAD -from pyomo.environ import * -from pyomo.opt import SolverFactory - -model = ConcreteModel() - -model.x = Var(domain = NonNegativeReals) -model.y = Var(domain = NonNegativeReals) - -model.obj = Objective(expr=3 * model.x + 4 * model.y, sense=minimize) - -model.con1 = Constraint(expr=2 * model.x + model.y >= 15) - -model.con2 = Constraint(expr=model.x + 2 * model.y >= 17) - -model.con3 = Constraint(expr=model.x + model.y >= 10) - -result = SolverFactory("glpk").solve(model) - -model.pprint() - -model.display() -======= -l = ['a', 'x', 'b', 'x', 'c', 'x'] -print(l) -l.pop(2) # remove the first item with value 'x' -print(l) - ->>>>>>> origin/main -- GitLab