パイソニスタの方へ
・ModuleNotFoundError: No module named 'deap'
・私は、投げ出しました。問題の意味も理解していません。
オリジナル
1973 東京大学 2次試験文科・理科共通【1】
j_suzuki29 i様
http://server-test.net/ 様
http://server-test.net/math/php.php?name=tokyo&v1=1&v2=1973&v3=1&v4=3&y=1973&n=3
大学入試数学問題集成 様
https://mathexamtest.web.fc2.com/1973/197310261/1973102610200mj.html
ChatGPT-3.5先生へ。3回目?オリジナル様の結果と、異なりました。
大学入試数学問題集成 様> 【3】テキスト
・ページのソースを表示です。MathMLです。
省略しようと思いましたが、大学入試数学問題集成 様 の全文から必要部分の抜粋です。
<div class="totaldiv">
<div class="qref">
<div class="refnum">
<a name="top-0201"
id="top-0201">
1973-10261-0201</a></div>
<div class="refinfo"><p>
1973 東京大学 2次試験</p></div>
<div class="refinfo2"><p>文科・理科共通</p>
<p>理科は【3】</p>
</div>
<div class="diff">易□ 並□ 難□</div></div>
<div class="qbody"><a name="q-0201"
id="q-0201"></a>
<p class="slevel"><span class="qnum">【1】</span> 区間<math>
<mspace width=".2em"></mspace><mn>1</mn><mo>≦</mo>
<mi>x</mi><mo>≦</mo><mn>3</mn><mspace width=".2em"></mspace>
</math>において次のように定義された関数<math>
<mspace width=".2em"></mspace><mi>f</mi><mo>⁡</mo>
<mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mspace width=".2em"></mspace>
</math>がある.</p>
<p class="equation"><math>
<mi>f</mi><mo>⁡</mo><mo stretchy="false">(</mo><mi>x</mi>
<mo stretchy="false">)</mo><mo>=</mo><mrow><mo>{</mo>
<mtable class="rnrt" columnalign="left"><mtr><mtd><mn>1</mn></mtd>
<mtd><mtext>(</mtext><mspace width=".2em"></mspace>
<mn>1</mn><mo>≦</mo><mi>x</mi><mo>≦</mo>
<mn>2</mn><mspace width=".2em"></mspace><mtext>)</mtext></mtd></mtr>
<mtr><mtd><mi>x</mi><mo>-</mo><mn>1</mn></mtd><mtd>
<mtext>(</mtext><mspace width=".2em"></mspace><mn>2</mn>
<mo>≦</mo><mi>x</mi><mo>≦</mo><mn>3</mn>
<mspace width=".2em"></mspace><mtext>)</mtext></mtd></mtr>
</mtable></mrow></math></p>
<p class="slevel"> いま実数<math>
<mspace width=".2em"></mspace><mi>a</mi><mspace width=".2em"></mspace>
</math>に対して,区間<math>
<mspace width=".2em"></mspace><mn>1</mn><mo>≦</mo>
<mi>x</mi><mo>≦</mo><mn>3</mn><mspace width=".2em"></mspace>
</math>における関数<math>
<mspace width=".2em"></mspace><mrow><mi>f</mi><mo>⁡</mo>
<mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>-</mo><mi>a</mi>
<mo>⁢</mo><mi>x</mi></mrow><mspace width=".2em"></mspace>
</math>の最大値から最小値を引いた値を<math>
<mspace width=".2em"></mspace><mrow><mi>V</mi><mo>⁡</mo>
<mo stretchy="false">(</mo><mi>a</mi><mo stretchy="false">)</mo></mrow><mspace width=".2em"></mspace>
</math>とおく.このとき次の問に答えよ.</p>
<p class="s1level">(1) <math>
<mi>a</mi><mspace width=".2em"></mspace>
</math>がすべての実数にわたって動くとき,<math>
<mi>V</mi><mo>⁡</mo><mo stretchy="false">(</mo><mi>a</mi>
<mo stretchy="false">)</mo><mspace width=".2em"></mspace>
</math>の最小値を求めよ.</p>
<p class="s1level">(2) <math>
<mi>V</mi><mo>⁡</mo><mo stretchy="false">(</mo><mi>a</mi>
<mo stretchy="false">)</mo><mspace width=".2em"></mspace>
</math>の最小値を与えるような<math>
<mspace width=".2em"></mspace><mi>a</mi><mspace width=".2em"></mspace>
</math>の値を求めよ.
</p>
</div>
</div>
・1回目 以下の1行追加して。
遺伝的アルゴリズムで。
・2回目 以下の1行追加して。
遺伝的アルゴリズムを使ったpythonコードを教えて下さい。。
import random
import numpy as np
from deap import base, creator, tools, algorithms
# 関数f(x)の定義
def f(x):
if 1 <= x <= 2:
return 1
elif 2 < x <= 3:
return x - 1
else:
raise ValueError("x is out of the interval [1, 3]")
# V(a)を計算する関数
def V(a):
x_values = np.linspace(1, 3, 100)
f_minus_ax_values = [f(x) - a * x for x in x_values]
return max(f_minus_ax_values) - min(f_minus_ax_values)
# 遺伝的アルゴリズムの設定
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
toolbox.register("attr_float", random.uniform, -10, 10)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, 1)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("mate", tools.cxBlend, alpha=0.5)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", V)
# メイン関数
def main():
random.seed(42)
pop = toolbox.population(n=300)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("std", np.std)
stats.register("min", np.min)
stats.register("max", np.max)
algorithms.eaSimple(pop, toolbox, cxpb=0.7, mutpb=0.2, ngen=40, stats=stats, halloffame=hof, verbose=True)
return pop, stats, hof
if __name__ == "__main__":
pop, stats, hof = main()
best_ind = hof[0]
print(f"Best individual is {best_ind}, with V(a) = {V(best_ind[0])}")
・ModuleNotFoundError: No module named 'deap'
・私には、これ以上無理です。
・3回目 追加なしで。
日本語で。
回答抜粋
??? したがって、V(a) の最小値は a=1 のときに達成されます。
??? 以上で解答が完了です。
・4回目
sympyのソースコードをお願いします。
ChatGPT-3.5先生のソースコードです。最終行のprint文は、私が直しています。
No algorithms are implemented to solve equation ...
エラーがでました。
import sympy as sp
# 変数の定義
x = sp.symbols('x')
a = sp.symbols('a')
# 関数 f(x) の定義
f = sp.Piecewise((1, (1 <= x) & (x <= 2)), (x - 1, (2 <= x) & (x <= 3)))
# 関数 g(x) = f(x) - ax の定義
g = f - a*x
# g(x) の区間内での最大値と最小値を求める
g_max = sp.Max(g.subs(x, 1), g.subs(x, 2), g.subs(x, 3))
g_min = sp.Min(g.subs(x, 1), g.subs(x, 2), g.subs(x, 3))
# 関数 V(a) の定義
V = g_max - g_min
# V(a) の最小値を求める
V_min = sp.simplify(V)
# V(a) の最小値を与える a の値を求める
a_value = sp.solve(sp.diff(V, a), a)
print("#",V_min, a_value)
・5回目
sympyのpiecewise関数でお願いします。
ValueError: The argument '(x, 1, 3)' is not comparable.
エラーがでました。
import sympy as sp
# 変数を定義
x, a = sp.symbols('x a')
# 関数を定義
f = sp.Piecewise((1, (1 <= x) & (x <= 2)), (x - 1, (2 <= x) & (x <= 3)))
g = f - a * x
# g(x) の最大値と最小値を求めるための範囲を定義
interval = (x, 1, 3)
# g(x) の最大値と最小値を求める
g_max = sp.Max(g, interval)
g_min = sp.Min(g, interval)
# V(a) を計算
V = g_max - g_min
# V(a) の最小値を求める
V_min = sp.minimize(V, a)
print("#",V, V_min)
オリジナル様の方法で
・勉強中? 私はあきらめました。
sympyで(いつもの方法で)
いつもの? sympyの実行環境と 参考のおすすめです。
いつもと違うおすすめです。
・