scipy.optimize.fminbound和scipy.optimize.minimize_scalar(bounds =(0,1))之间的区别是什么?

时间:2016-04-12 23:17:15

标签: python optimization scipy

1 个答案:

答案 0 :(得分:1)

有时它是,有时它不是。这取决于您定义的功能和您选择的方法。主要区别在于定义的using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace LOGINPAGE { public partial class Room : Form { public Room() { InitializeComponent(); PassText.PasswordChar = '*'; } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void pictureBox1_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { this.Hide(); FloorSelection ss = new FloorSelection(); ss.Show(); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { } private void EXIT_Click(object sender, EventArgs e) { this.Close(); Application.Exit(); } private void xButton1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Mohamed\Documents\UserData.mdf;Integrated Security=True;Connect Timeout=30"); SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From dbo.[LOGIN] where username='" + UserText.Text + "' and Password ='" + PassText.Text + "'", con); FloorSelection ss = new FloorSelection(); DataTable dt = new DataTable(); sda.Fill(dt); if (dt.Rows[0][0].ToString() == "1") { SqlDataAdapter sda1 = new SqlDataAdapter("Select TYPE From dbo.[LOGIN] where username='" + UserText.Text + "' and Password ='" + PassText.Text + "'", con); FloorSelection ss1 = new FloorSelection(); DataTable dt1 = new DataTable(); sda1.Fill(dt1); if (dt1.Rows[0][0].ToString() == "FACULTY") { this.Hide(); FACULTY ff = new FACULTY(); ff.Show(); } else if (dt1.Rows[0][0].ToString() == "ADMINISTRATOR") { this.Hide(); ADMINISTRATOR Ad = new ADMINISTRATOR(); Ad.Show(); } else if (dt1.Rows[0][0].ToString() == "JANITOR") { this.Hide(); JANITOR jt = new JANITOR(); jt.Show(); } else { MessageBox.Show("Please check your username and password"); } } } private void label3_Click(object sender, EventArgs e) { label3.BackColor = Color.Transparent; } private void UserText_TextChanged(object sender, EventArgs e) { UserText.BackColor = Color.Empty; } private void PassText_TextChanged(object sender, EventArgs e) { PassText.BackColor = Color.Empty; } } } 必须返回使用func的标量。我认为这是因为它允许它更快地返回结果。

有关更具体的解释,请参阅以下内容:

minimize_scalar

如果要查看功能跟踪和最小位置,可以查看这些图。打印输出是:

from scipy import optimize

def func(x):
    return (x - 2) * (x + 2)**2

def func2(x):
    return (x - 2) * x * (x + 2)**2

min = 0
max = 1

res1 = optimize.fminbound(func, min, max)
res2 = optimize.minimize_scalar(func, bounds=(min,max))
res3 = optimize.fminbound(func2, min, max)
res4 = optimize.minimize_scalar(func2, bounds=(min,max))

print res1, res2.x
print res3, res4.x

import matplotlib.pyplot as plt
import numpy as np

xaxis = np.arange(-15,15)

plt.plot(xaxis, func(xaxis))
plt.plot(xaxis, func2(xaxis))
plt.scatter(res2.x, res2.fun)
plt.scatter(res4.x, res4.fun)
plt.show()