Commit 651c34cb authored by Dr.李's avatar Dr.李

update example

parent 61bfd381
...@@ -207,8 +207,8 @@ ...@@ -207,8 +207,8 @@
"source": [ "source": [
"def time_function(py_callable, n):\n", "def time_function(py_callable, n):\n",
" start = dt.datetime.now()\n", " start = dt.datetime.now()\n",
" py_callable(n)\n", " val = py_callable(n)\n",
" return (dt.datetime.now() - start).total_seconds()" " return (dt.datetime.now() - start).total_seconds(), val"
] ]
}, },
{ {
...@@ -229,7 +229,8 @@ ...@@ -229,7 +229,8 @@
" sum_entries(w) == 1,]\n", " sum_entries(w) == 1,]\n",
"\n", "\n",
" prob = Problem(objective, constraints)\n", " prob = Problem(objective, constraints)\n",
" prob.solve(verbose=False, solver='CVXOPT', display=False)" " prob.solve(verbose=False, solver='CVXOPT', display=False)\n",
" return prob.value"
] ]
}, },
{ {
...@@ -260,7 +261,8 @@ ...@@ -260,7 +261,8 @@
" b = matrix(b)\n", " b = matrix(b)\n",
" \n", " \n",
" solvers.options['show_progress'] = False\n", " solvers.options['show_progress'] = False\n",
" sol = solvers.qp(P, q, G, h, A, b)" " sol = solvers.qp(P, q, G, h, A, b)\n",
" return sol['primal objective']"
] ]
}, },
{ {
...@@ -276,7 +278,7 @@ ...@@ -276,7 +278,7 @@
" clb = np.ones(1)\n", " clb = np.ones(1)\n",
" cub = np.ones(1)\n", " cub = np.ones(1)\n",
" qpopt = QPOptimizer(signal, sec_cov_values, lbound, ubound, cons_matrix, clb, cub, 1.)\n", " qpopt = QPOptimizer(signal, sec_cov_values, lbound, ubound, cons_matrix, clb, cub, 1.)\n",
" qpopt.feval()" " return qpopt.feval()"
] ]
}, },
{ {
...@@ -294,27 +296,16 @@ ...@@ -294,27 +296,16 @@
"for i, n in enumerate(n_steps):\n", "for i, n in enumerate(n_steps):\n",
" sec_cov_values = sec_cov_values_full[:n, :n]\n", " sec_cov_values = sec_cov_values_full[:n, :n]\n",
" signal = signal_full[:n]\n", " signal = signal_full[:n]\n",
" cvxpy_times[i] = time_function(cvxpy, n) * 1000\n", " cvxpy_times[i], val1 = time_function(cvxpy, n)\n",
" cvxopt_times[i] = time_function(cvxopt, n) * 1000\n", " cvxopt_times[i], val2 = time_function(cvxopt, n)\n",
" ipopt_times[i] = time_function(ipopt, n) * 1000\n", " ipopt_times[i], val3 = time_function(ipopt, n)\n",
" \n", " \n",
" print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12.2f}\".format(n, cvxpy_times[i], cvxopt_times[i], ipopt_times[i]))" " np.testing.assert_almost_equal(val1, val2, 4)\n",
" np.testing.assert_almost_equal(val2, val3, 4)\n",
" \n",
" print(\"{0:<8}{1:>12.4f}{2:>12.4f}{3:>12.4f}\".format(n, cvxpy_times[i], cvxopt_times[i], ipopt_times[i]))"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
...@@ -339,7 +330,7 @@ ...@@ -339,7 +330,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.3" "version": "3.6.5"
}, },
"varInspector": { "varInspector": {
"cols": { "cols": {
......
...@@ -113,8 +113,8 @@ ...@@ -113,8 +113,8 @@
" objective = cvxpy.Minimize(-w.T * er)\n", " objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n", " prob = cvxpy.Problem(objective, constraints)\n",
" \n", " \n",
" prob.solve(solver='GLPK')\n", " prob.solve()\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='GLPK')\",\n", " elasped_time2 = timeit.timeit(\"prob.solve()\",\n",
" number=number, globals=globals()) / number * 1000\n", " number=number, globals=globals()) / number * 1000\n",
"\n", "\n",
" np.testing.assert_almost_equal(x1 @ er, np.array(w.value).flatten() @ er, 4)\n", " np.testing.assert_almost_equal(x1 @ er, np.array(w.value).flatten() @ er, 4)\n",
...@@ -196,8 +196,8 @@ ...@@ -196,8 +196,8 @@
" objective = cvxpy.Minimize(-w.T * er)\n", " objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n", " prob = cvxpy.Problem(objective, constraints)\n",
" \n", " \n",
" prob.solve(solver='GLPK')\n", " prob.solve()\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='GLPK')\",\n", " elasped_time2 = timeit.timeit(\"prob.solve()\",\n",
" number=number, globals=globals()) / number * 1000\n", " number=number, globals=globals()) / number * 1000\n",
" \n", " \n",
" elasped_time3 = timeit.timeit(\"\"\"linear_builder(er,\n", " elasped_time3 = timeit.timeit(\"\"\"linear_builder(er,\n",
...@@ -529,7 +529,7 @@ ...@@ -529,7 +529,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.3" "version": "3.6.5"
}, },
"varInspector": { "varInspector": {
"cols": { "cols": {
......
...@@ -355,7 +355,7 @@ ...@@ -355,7 +355,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.3" "version": "3.6.5"
}, },
"varInspector": { "varInspector": {
"cols": { "cols": {
......
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
"\n", "\n",
" objective = cvxpy.Minimize(-w.T * er)\n", " objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n", " prob = cvxpy.Problem(objective, constraints)\n",
" prob.solve(solver='GLPK')\n", " prob.solve()\n",
" return w, prob" " return w, prob"
] ]
}, },
...@@ -113,6 +113,13 @@ ...@@ -113,6 +113,13 @@
" print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12f}{4:>12f}{5:>12f}{6:>15}\".format(n, elapsed*1000, result[1], s.min(), s.max(), s.sum(), s[0] + s[1]))" " print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12f}{4:>12f}{5:>12f}{6:>15}\".format(n, elapsed*1000, result[1], s.min(), s.max(), s.sum(), s[0] + s[1]))"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
...@@ -137,7 +144,7 @@ ...@@ -137,7 +144,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.4" "version": "3.6.5"
}, },
"varInspector": { "varInspector": {
"cols": { "cols": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment