# 変数の作成 x = solver.IntVar(0, 10, 'x') y = solver.IntVar(0, 10, 'y')
# 制約条件の追加 solver.Add(2 * x + 3 * y <= 12)
# 目的関数の設定 solver.Maximize(3 * x + 4 * y)
# 解を求める status = solver.Solve()
# 結果を表示 if status == pywraplp.Solver.OPTIMAL: print('Objective value =', solver.Objective().Value()) print('x =', x.solution_value()) print('y =', y.solution_value()) else: print('The problem does not have an optimal solution.')
if status == pywraplp.Solver.OPTIMAL: print('Objective value =', solver.Objective().Value()) print('x =', x.solution_value()) print('y =', y.solution_value()) else: print('The problem does not have an optimal solution.')
status が pywraplp.Solver.OPTIMAL であれば、最適解が見つかっています。
# 変数の作成 x = solver.NumVar(0, 1, 'x') y = solver.NumVar(0, 2, 'y')
# 制約条件の追加 solver.Add(x + y <= 2)
# 目的関数の設定 solver.Maximize(x + y)
# 解を求める status = solver.Solve()
# 結果を表示 if status == pywraplp.Solver.OPTIMAL: print('Objective value =', solver.Objective().Value()) print('x =', x.solution_value()) print('y =', y.solution_value()) else: print('The problem does not have an optimal solution.')
ここでは、GLOP(Google Linear Optimization Package)という線形最適化ソルバーを作成します。
CreateSolver('GLOP')は新しいソルバーオブジェクトを作成します。
ソルバーが作成できなかった場合、関数を終了します。
4. 変数の作成
1 2
x = solver.NumVar(0, 1, 'x') y = solver.NumVar(0, 2, 'y')
線形最適化問題の変数を作成します。ここでは、変数xとyを定義します。
xは$0$から$1$までの値を取ります。
yは$0$から$2$までの値を取ります。
5. 制約条件の追加
1
solver.Add(x + y <= 2)
変数に対する制約を追加します。ここでは、x + y <= 2という制約条件を設定しています。
これは、変数xとyの和が2以下でなければならないことを意味します。
6. 目的関数の設定
1
solver.Maximize(x + y)
最適化の目的を設定します。ここでは、x + yを最大化するように設定しています。
7. 解を求める
1
status = solver.Solve()
設定した制約条件と目的関数に基づいて、最適解を求めます。
このメソッドは、解が見つかったかどうかを示すステータスコードを返します。
8. 結果の表示
1 2 3 4 5 6
if status == pywraplp.Solver.OPTIMAL: print('Objective value =', solver.Objective().Value()) print('x =', x.solution_value()) print('y =', y.solution_value()) else: print('The problem does not have an optimal solution.')
最適解が見つかった場合、目的関数の値と変数xおよびyの値を表示します。
解が見つからなかった場合は、最適解が存在しないことを表示します。
9. 関数の呼び出し
1
linear_optimization_example()
定義した関数を呼び出し、線形最適化問題を実行します。
まとめ
このコードは、OR-Toolsを使用して簡単な線形最適化問題を解決する方法を示しています。
具体的には、2つの変数xとyを定義し、制約条件x + y <= 2のもとでx + yを最大化する問題を解いています。
# 交叉と突然変異 for child1, child2 inzip(offspring[::2], offspring[1::2]): if random.random() < cxpb: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values
for mutant in offspring: if random.random() < mutpb: toolbox.mutate(mutant) del mutant.fitness.values
# 適応度の再計算 invalid_ind = [ind for ind in offspring ifnot ind.fitness.valid] fitnesses = map(toolbox.evaluate, invalid_ind) for ind, fit inzip(invalid_ind, fitnesses): ind.fitness.values = fit
population[:] = offspring
# 統計の表示 fits = [ind.fitness.values[0] for ind in population] length = len(population) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5
for child1, child2 inzip(offspring[::2], offspring[1::2]): if random.random() < cxpb: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values
for mutant in offspring: if random.random() < mutpb: toolbox.mutate(mutant) del mutant.fitness.values
for child1, child2 in zip(offspring[::2], offspring[1::2]):2つずつペアにして交叉を行います。
if random.random() < cxpb:交叉確率に基づいて交叉を実行します。 交叉した個体の適応度を削除します(再評価のため)。
for mutant in offspring:突然変異を行います。
if random.random() < mutpb:突然変異確率に基づいて突然変異を実行します。 突然変異した個体の適応度を削除します(再評価のため)。
適応度の再計算
1 2 3 4 5 6
invalid_ind = [ind for ind in offspring ifnot ind.fitness.valid] fitnesses = map(toolbox.evaluate, invalid_ind) for ind, fit inzip(invalid_ind, fitnesses): ind.fitness.values = fit
population[:] = offspring
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]:適応度が無効な個体をリストにします。
for ind, fit in zip(invalid_ind, fitnesses):再評価された適応度を個体に設定します。
population[:] = offspring:新しい世代の個体で集団を置き換えます。
統計の表示
1 2 3 4 5 6 7
fits = [ind.fitness.values[0] for ind in population] length = len(population) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5
This privacy policy applies to the Piano BGM app (hereby referred to as “Application”) for mobile devices that was created by (hereby referred to as “Service Provider”) as a Free service. This service is intended for use “AS IS”.
Information Collection and Use
The Application collects information when you download and use it. This information may include information such as
Your device’s Internet Protocol address (e.g. IP address)
The pages of the Application that you visit, the time and date of your visit, the time spent on those pages
The time spent on the Application
The operating system you use on your mobile device
The Application does not gather precise information about the location of your mobile device.
The Application collects your device’s location, which helps the Service Provider determine your approximate geographical location and make use of in below ways:
Geolocation Services: The Service Provider utilizes location data to provide features such as personalized content, relevant recommendations, and location-based services.
Analytics and Improvements: Aggregated and anonymized location data helps the Service Provider to analyze user behavior, identify trends, and improve the overall performance and functionality of the Application.
Third-Party Services: Periodically, the Service Provider may transmit anonymized location data to external services. These services assist them in enhancing the Application and optimizing their offerings.
The Service Provider may use the information you provided to contact you from time to time to provide you with important information, required notices and marketing promotions.
For a better experience, while using the Application, the Service Provider may require you to provide us with certain personally identifiable information. The information that the Service Provider request will be retained by them and used as described in this privacy policy.
Third Party Access
Only aggregated, anonymized data is periodically transmitted to external services to aid the Service Provider in improving the Application and their service. The Service Provider may share your information with third parties in the ways that are described in this privacy statement.
Please note that the Application utilizes third-party services that have their own Privacy Policy about handling data. Below are the links to the Privacy Policy of the third-party service providers used by the Application:
The Service Provider may disclose User Provided and Automatically Collected Information:
as required by law, such as to comply with a subpoena, or similar legal process;
when they believe in good faith that disclosure is necessary to protect their rights, protect your safety or the safety of others, investigate fraud, or respond to a government request;
with their trusted services providers who work on their behalf, do not have an independent use of the information we disclose to them, and have agreed to adhere to the rules set forth in this privacy statement.
Opt-Out Rights
You can stop all collection of information by the Application easily by uninstalling it. You may use the standard uninstall processes as may be available as part of your mobile device or via the mobile application marketplace or network.
Data Retention Policy
The Service Provider will retain User Provided data for as long as you use the Application and for a reasonable time thereafter. If you’d like them to delete User Provided Data that you have provided via the Application, please contact them at angularity@msn.com and they will respond in a reasonable time.
Children
The Service Provider does not use the Application to knowingly solicit data from or market to children under the age of 13.
The Application does not address anyone under the age of 13. The Service Provider does not knowingly collect personally identifiable information from children under 13 years of age. In the case the Service Provider discover that a child under 13 has provided personal information, the Service Provider will immediately delete this from their servers. If you are a parent or guardian and you are aware that your child has provided us with personal information, please contact the Service Provider (angularity@msn.com) so that they will be able to take the necessary actions.
Security
The Service Provider is concerned about safeguarding the confidentiality of your information. The Service Provider provides physical, electronic, and procedural safeguards to protect information the Service Provider processes and maintains.
Changes
This Privacy Policy may be updated from time to time for any reason. The Service Provider will notify you of any changes to the Privacy Policy by updating this page with the new Privacy Policy. You are advised to consult this Privacy Policy regularly for any changes, as continued use is deemed approval of all changes.
This privacy policy is effective as of 2024-06-18
Your Consent
By using the Application, you are consenting to the processing of your information as set forth in this Privacy Policy now and as amended by us.
Contact Us
If you have any questions regarding privacy while using the Application, or have questions about the practices, please contact the Service Provider via email at angularity@msn.com.