Permalink
Browse files

Поправлен вывод результатов (осуществляется через render_output_form)…

…, Добавлен выбор поиска максимума/минимума.
  • Loading branch information...
1 parent c2d51f5 commit e41d39c03afc49e21aba31037b4e7583f278f7af @kenjione kenjione committed Feb 27, 2012
View
@@ -1,4 +1,4 @@
-#Методы оптимизации (поиска минимума) функции двух переменных.
+#Методы оптимизации (поиска экстремума) функции двух переменных.
Задачей оптимизации в математике, информатике и исследовании операций называется задача нахождения экстремума (минимума или максимума) целевой функции в некоторой области конечномерного векторного пространства, ограниченной набором линейных и/или нелинейных равенств и/или неравенств. (с) [Wikipedia](http://ru.wikipedia.org/wiki/%CE%EF%F2%E8%EC%E8%E7%E0%F6%E8%FF)
@@ -11,4 +11,4 @@
##
-Пощупать проект можно [тут](http://optimize.newmen.pro)
+Пощупать проект можно [тут](http://optimize.newmen.pro)
View
@@ -9,10 +9,10 @@
class HookMethod
- attr_accessor :draw_points, :x, :func, :y
+ attr_accessor :draw_points, :x, :func, :y, :y_res
def self.find_extremum(params)
- new(params[:function].to_s, [params[:x1].to_f,params[:x2].to_f], params[:eps].to_f, params[:lambda].to_f, params[:alpha].to_f, params[:step1].to_f, params[:step2].to_f)
+ new(params[:function], [params[:x1].to_f,params[:x2].to_f], params[:eps].to_f, params[:lambda].to_f, params[:alpha].to_f, params[:step1].to_f, params[:step2].to_f)
end
def initialize(func,x0, eps, lambda, alpha, step1, step2)
@@ -26,6 +26,7 @@ def initialize(func,x0, eps, lambda, alpha, step1, step2)
@y = [x0.dup,x0.dup,x0.dup]
@draw_points = Array.new
@draw_points.push(x0.dup)
+ @y_res
step_2
end
@@ -60,6 +61,7 @@ def step_2 #explore
step_2
else
if @step[0] < @eps and @step[1] < @eps
+ @y_res = calc(@y[-1])
else
@step[0] /= @alpha
@step[1] /= @alpha
@@ -19,7 +19,7 @@ def calc
end
class NelderMeadMethod
- attr_accessor :points, :draw_points, :alpha, :beta, :gamma, :func
+ attr_accessor :points, :draw_points, :alpha, :beta, :gamma, :func , :y_res
def self.find_extremum(params)
Point.setfunc(params[:function].to_s)
@@ -56,6 +56,7 @@ def center
sum+= ((p.calc - f_c)**2)
}
if Math.sqrt(sum/3.0) < 0.0001 then
+ @y_res = @points[0].calc
return
end
point_r = Point.new([0.0, 0.0])
View
@@ -2,7 +2,7 @@
class PowellMethod
- attr_accessor :x , :draw_points, :func, :y
+ attr_accessor :x , :draw_points, :func, :y , :y_res
def self.find_extremum(params)
new([params[:x1].to_f,params[:x2].to_f], params[:function], params[:eps].to_f)
end
@@ -34,6 +34,9 @@ def step_4
@x.push(@y[-1])
norm = Math.sqrt((@x[-1][0] - @x[-2][0])**2 + (@x[-1][1] - @x[-2][1])**2)
if norm < @eps then
+
+ @y_res = calc(@y[-1])
+ puts @y_res.inspect
else
temp_d2 = Array.new(@d[2])
for i in 0..1
@@ -4,7 +4,7 @@
require "sinatra"
class RozenbrokMethod < BaseOptimizeMethod
- attr_accessor :func, :x, :d, :alpha, :beta, :eps, :i, :y, :dx, :N, :k, :n, :shitstep, :lambda, :x_res, :a, :draw_points
+ attr_accessor :func, :x, :y, :x_res, :draw_points, :y_res
def self.find_extremum(params)
new([params[:x1].to_f, params[:x2].to_f], params[:function].to_s, params[:alpha].to_f, params[:beta].to_f, params[:epsilon].to_f)
@@ -105,6 +105,8 @@ def step_4
if self.norm <= @eps then
@x_res = @x[-1]
+ puts @draw_points[-1].inspect
+ @y_res = calc(@draw_points[-1])
return
else
@lambda = Matrix[[@x[-1][0] - @x[-2][0]],[@x[-1][1] - @x[-2][1]]]
View
@@ -16,8 +16,8 @@ def print_float(float, acc = 3)
"%.#{acc}f" % float
end
- def render_output_form(result)
- erb(:output_form, locals: {result: result})
+ def render_output_form(result, params)
+ erb(:output_form, locals: {result: result, params: params})
end
end
@@ -30,8 +30,12 @@ def render_output_form(result)
end
post '/:name' do |name|
+
+ params[:function] = params[:opt_type].to_s + "*(" + params[:function].to_s + ")"
+ params[:opt_type]= params[:opt_type].to_f
+ params[:opt_type] == -1.0 ? params[:extremum] = "Max" : params[:extremum] = "Min"
result = name.camelize.constantize.find_extremum(params)
drawplot(result.draw_points, name.to_s)
- drawfunc(result.func)
+ drawfunc(result.func[4...-1])
erb :"/#{name}/handler", locals: {params: params, result: result}
end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -9,6 +9,13 @@
<td>Уравнение: </td>
<td><input id="function" name="function" type="text" size="20" value="x**2 + y**2"/></td>
</tr>
+ <tr>
+ <td>
+ <input name="opt_type" value="-1" type="radio" > Max <br/>
+ <input name="opt_type" value="+1" type="radio" checked> Min
+ </td>
+ </tr>
+
</table>
<table>
<tr>
@@ -1,27 +1,3 @@
-<html>
-<head></head>
-<body>
- <table>
- <%= render_output_form(result)%>
- <tr>
- <td>
- <img src="<%="/"+params[:name].to_s%>.png?<%= Time.now.to_i %>" />
- </td>
+<%= render_output_form(result, params)%>
- <td>
- <b>Результаты:</b>
- <table>
- <% for i in 0..1 %>
- <tr><td>
- x[<%=(i+1)%>] = <%= print_float(result.draw_points[-1][i])%>
- </td></tr>
- <% end %>
- <tr><td>Минимум функции: <%=print_float(result.y[-1])%></td></tr>
- <tr><td><form action = "/" method = "get"> <input type="submit" value="взад!" /> </form></td></tr>
- </table>
- </td>
- </tr>
- </table>
-</body>
-</html>
@@ -7,6 +7,13 @@
<td><label for="function">Уравнение:</label></td>
<td><input id="function" name="function" type="text" size="20" value="x**2 + y**2"/></td>
</tr>
+ <tr>
+ <td>
+ <input name="opt_type" value="-1" type="radio" > Max <br/>
+ <input name="opt_type" value="+1" type="radio" checked> Min
+ </td>
+ </tr>
+
</table>
<table>
<tr>
@@ -1,21 +1,2 @@
-<table>
- <%= render_output_form(result)%>
- <tr>
- <td>
- <img src="<%="/"+params[:name].to_s%>.png?<%= Time.now.to_i %>" />
- </td>
- <td>
- <b>Результаты:</b>
- <table>
- <% for i in 0...3 %>
- <tr><td>
- x[<%=(i+1)%>] = [<%= print_float(result.draw_points[-1][i][0])%>,<%= print_float(result.draw_points[-1][i][1])%>]
- </td></tr>
- <% end %>
- <tr><td>Минимум функции: <%=print_float(result.points[-1].y)%></td></tr>
- <tr><td><form action = "/" method = "get"> <input type="submit" value="взад!" /> </form></td></tr>
- </table>
- </td>
- </tr>
-</table>
+<%= render_output_form(result, params) %>
View
@@ -1,3 +1,5 @@
+<table>
+
<tr>
<td>
<img src="/function.png?<%= Time.now.to_i %>" />
@@ -8,7 +10,8 @@
<%if result.draw_points[0][0].is_a?(Array); num_of_points=result.draw_points[0].size
else num_of_points=2
end%>
- Уравнение: <%=result.func%>
+ Уравнение:<br/>
+ <%=result.func[4...-1]%>
<% for i in 0...num_of_points %>
<tr><td>
x[<%=(i+1)%>] = <%= if num_of_points==2; print_float(result.draw_points[0][i])
@@ -19,3 +22,35 @@
</table>
</tr>
+<tr>
+ <td>
+ <img src="<%="/"+params[:name].to_s%>.png?<%= Time.now.to_i %>" />
+ </td>
+
+ <td>
+ <b>Результаты:</b>
+ <table>
+
+ <% if num_of_points == 3 %>
+ <% for i in 0...3 %>
+ <tr><td>
+ x[<%=(i+1)%>] = [<%= print_float(result.draw_points[-1][i][0])%>,<%= print_float(result.draw_points[-1][i][1])%>]
+ </td></tr>
+ <% end %>
+ <% else %>
+
+ <% for i in 0..1 %>
+ <tr><td>
+ x[<%=(i+1)%>] = <%= print_float(result.draw_points[-1][i])%>
+ </td></tr>
+ <% end %>
+ <% end %>
+ <tr><td><%=params[:extremum]%> функции: <%=print_float(params[:opt_type]*result.y_res)%></td></tr>
+ <tr><td><form action = "/" method = "get"> <input type="submit" value="взад!" /> </form></td></tr>
+ </table>
+ </td>
+</tr>
+
+</table>
+
+
@@ -8,6 +8,13 @@
<td><label for="function">Уравнение:</label></td>
<td><input id="function" name="function" type="text" size="20" value="x**2 + y**2"/></td>
</tr>
+ <tr>
+ <td>
+ <input name="opt_type" value="-1" type="radio" > Max <br/>
+ <input name="opt_type" value="+1" type="radio" checked> Min
+ </td>
+ </tr>
+
</table>
<table>
@@ -1,24 +1,2 @@
-
-<table>
- <%= render_output_form(result) %>
- <tr>
- <td>
- <img src="<%="/"+params[:name].to_s%>.png?<%= Time.now.to_i %>" />
- </td>
-
- <td>
- <b>Результаты:</b>
- <table>
- <% for i in 0..1 %>
- <tr><td>
- x[<%=(i+1)%>] = <%= print_float(result.draw_points[-1][i])%>
- </td></tr>
- <% end %>
- <!--<tr><td>Минимум функции: <%=print_float(result.y[-1])%></td></tr>-->
- <tr><td><form action = "/" method = "get"> <input type="submit" value="взад!" /> </form></td></tr>
- </table>
-
- </td>
- </tr>
-</table>
+<%= render_output_form(result, params)%>
@@ -7,6 +7,12 @@
<td><label for="function">Уравнение:</label></td>
<td><input id="function" name="function" type="text" size="20" value="x**2 + y**2"/></td>
</tr>
+ <tr>
+ <td>
+ <input name="opt_type" value="-1" type="radio" > Max <br/>
+ <input name="opt_type" value="+1" type="radio" checked> Min
+ </td>
+ </tr>
</table>
<table>
@@ -1,26 +1 @@
-</head>
-<body>
- <table>
- <%= render_output_form(result)%>
- <tr>
- <td>
- <img src="<%="/"+params[:name].to_s%>.png?<%= Time.now.to_i %>" />
- </td>
-
- <td>
- <b>Результаты:</b>
- <table>
- <% for i in 0..1 %>
- <tr><td>
- x[<%=(i+1)%>] = <%= print_float(result.draw_points[-1][i])%>
- </td></tr>
- <% end %>
- <!--<tr><td>Минимум функции: <%=result.y[-1].inspect%></td></tr>-->
- <tr><td><form action = "/" method = "get"> <input type="submit" value="взад!" /> </form></td></tr>
- </table>
-
- </td>
- </tr>
- </table>
-</body>
-</html>
+<%= render_output_form(result, params)%>

0 comments on commit e41d39c

Please sign in to comment.