pyfemtet 0.8.8__py3-none-any.whl → 0.8.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyfemtet might be problematic. Click here for more details.

Files changed (35) hide show
  1. pyfemtet/__init__.py +1 -1
  2. pyfemtet/_message/locales/ja/LC_MESSAGES/messages.mo +0 -0
  3. pyfemtet/_message/locales/ja/LC_MESSAGES/messages.po +10 -2
  4. pyfemtet/_message/locales/messages.pot +10 -2
  5. pyfemtet/_message/messages.py +2 -1
  6. pyfemtet/_util/excel_parse_util.py +33 -15
  7. pyfemtet/opt/_femopt.py +3 -1
  8. pyfemtet/opt/_femopt_core.py +34 -26
  9. pyfemtet/opt/_test_utils/record_history.py +1 -0
  10. pyfemtet/opt/advanced_samples/excel_ui/femtet-macro.xlsm +0 -0
  11. pyfemtet/opt/advanced_samples/meta_script/meta_script.py +163 -0
  12. pyfemtet/opt/advanced_samples/meta_script/sample.yaml +14 -0
  13. pyfemtet/opt/advanced_samples/meta_script/yaml_generator.txt +0 -0
  14. pyfemtet/opt/advanced_samples/meta_script/yaml_generator.xlsm +0 -0
  15. pyfemtet/opt/advanced_samples/restart/gal_ex13_parametric.femprj +0 -0
  16. pyfemtet/opt/advanced_samples/restart/gal_ex13_parametric_restart.py +99 -0
  17. pyfemtet/opt/advanced_samples/restart/gal_ex13_parametric_restart_jp.py +102 -0
  18. pyfemtet/opt/interface/__init__.py +1 -1
  19. pyfemtet/opt/interface/_base.py +13 -4
  20. pyfemtet/opt/interface/_excel_interface.py +45 -39
  21. pyfemtet/opt/interface/_femtet.py +63 -2
  22. pyfemtet/opt/interface/_femtet_excel.py +138 -0
  23. pyfemtet/opt/interface/_surrogate/_base.py +43 -0
  24. pyfemtet/opt/interface/_surrogate_excel.py +102 -0
  25. pyfemtet/opt/optimizer/_base.py +2 -6
  26. pyfemtet/opt/optimizer/_optuna/_optuna.py +2 -2
  27. pyfemtet/opt/samples/femprj_sample/gau_ex12_parametric.femprj +0 -0
  28. pyfemtet/opt/samples/femprj_sample/gau_ex12_parametric.py +52 -0
  29. pyfemtet/opt/samples/femprj_sample_jp/gau_ex12_parametric_jp.py +52 -0
  30. pyfemtet/opt/visualization/_process_monitor/pages.py +13 -0
  31. {pyfemtet-0.8.8.dist-info → pyfemtet-0.8.10.dist-info}/METADATA +2 -1
  32. {pyfemtet-0.8.8.dist-info → pyfemtet-0.8.10.dist-info}/RECORD +35 -23
  33. {pyfemtet-0.8.8.dist-info → pyfemtet-0.8.10.dist-info}/WHEEL +1 -1
  34. {pyfemtet-0.8.8.dist-info → pyfemtet-0.8.10.dist-info}/LICENSE +0 -0
  35. {pyfemtet-0.8.8.dist-info → pyfemtet-0.8.10.dist-info}/entry_points.txt +0 -0
pyfemtet/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.8.8"
1
+ __version__ = "0.8.10"
@@ -7,7 +7,7 @@ msgid ""
7
7
  msgstr ""
8
8
  "Project-Id-Version: PROJECT VERSION\n"
9
9
  "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
10
- "POT-Creation-Date: 2025-01-29 15:47+0900\n"
10
+ "POT-Creation-Date: 2025-02-19 13:47+0900\n"
11
11
  "PO-Revision-Date: 2024-07-22 14:05+0900\n"
12
12
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13
13
  "Language: ja\n"
@@ -16,7 +16,7 @@ msgstr ""
16
16
  "MIME-Version: 1.0\n"
17
17
  "Content-Type: text/plain; charset=utf-8\n"
18
18
  "Content-Transfer-Encoding: 8bit\n"
19
- "Generated-By: Babel 2.16.0\n"
19
+ "Generated-By: Babel 2.17.0\n"
20
20
 
21
21
  #: pyfemtet/_message/messages.py:30
22
22
  msgid "hello!"
@@ -542,6 +542,14 @@ msgstr "ひとつの設計変数に対する目的関数の変化"
542
542
  msgid "The vertical axis is objective, and the horizontal axis is parameter."
543
543
  msgstr "縦軸は目的関数、横軸は設計変数です。"
544
544
 
545
+ #: pyfemtet/_message/messages.py:201
546
+ msgid "The importance of parameters evaluated by fANOVA"
547
+ msgstr "fANOVA に基づくパラメータ重要度"
548
+
549
+ #: pyfemtet/_message/messages.py:202
550
+ msgid "The normalized relative importance of parameters. Please note that the importance is calculated from the overall relationship of the input-output response, rather than from a specific solution."
551
+ msgstr "相対的なパラメータの重要度です。この重要度は特定の解におけるものではなく、入力-出力応答全体の関係から計算されていることに注意してください。"
552
+
545
553
  #~ msgid "The magnitude relationship is incorrect. "
546
554
  #~ msgstr ""
547
555
 
@@ -8,14 +8,14 @@ msgid ""
8
8
  msgstr ""
9
9
  "Project-Id-Version: PROJECT VERSION\n"
10
10
  "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
11
- "POT-Creation-Date: 2025-01-29 15:47+0900\n"
11
+ "POT-Creation-Date: 2025-02-19 13:47+0900\n"
12
12
  "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13
13
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
14
  "Language-Team: LANGUAGE <LL@li.org>\n"
15
15
  "MIME-Version: 1.0\n"
16
16
  "Content-Type: text/plain; charset=utf-8\n"
17
17
  "Content-Transfer-Encoding: 8bit\n"
18
- "Generated-By: Babel 2.16.0\n"
18
+ "Generated-By: Babel 2.17.0\n"
19
19
 
20
20
  #: pyfemtet/_message/messages.py:30
21
21
  msgid "hello!"
@@ -541,3 +541,11 @@ msgstr ""
541
541
  msgid "The vertical axis is objective, and the horizontal axis is parameter."
542
542
  msgstr ""
543
543
 
544
+ #: pyfemtet/_message/messages.py:201
545
+ msgid "The importance of parameters evaluated by fANOVA"
546
+ msgstr ""
547
+
548
+ #: pyfemtet/_message/messages.py:202
549
+ msgid "The normalized relative importance of parameters. Please note that the importance is calculated from the overall relationship of the input-output response, rather than from a specific solution."
550
+ msgstr ""
551
+
@@ -198,4 +198,5 @@ class Message:
198
198
  DETAIL_PAGE_CONTOUR_DESCRIPTION = _('The axes are parameters, and the color shows objective value.')
199
199
  DETAIL_PAGE_SLICE_HEADER = _('The response of an objective versus one parameter')
200
200
  DETAIL_PAGE_SLICE_DESCRIPTION = _('The vertical axis is objective, and the horizontal axis is parameter.')
201
-
201
+ DETAIL_PAGE_IMPORTANCE_HEADER = _('The importance of parameters evaluated by fANOVA')
202
+ DETAIL_PAGE_IMPORTANCE_DESCRIPTION = _('The normalized relative importance of parameters. Please note that the importance is calculated from the overall relationship of the input-output response, rather than from a specific solution.')
@@ -13,15 +13,15 @@ __all__ = [
13
13
  ]
14
14
 
15
15
 
16
- def parse_excel(book_path, sheet_name, keyword, required, optional) -> pd.DataFrame:
16
+ def parse_excel(book_path, sheet_name, keyword, required, optional, raise_if_no_keyword=True) -> pd.DataFrame:
17
17
  """Excel シートからパラメータを取得します。
18
18
 
19
19
  シートのパースプロセスは以下の通りです。
20
20
 
21
21
  1. シート全体 (A1 セルから、値が入力されている最終セルまで) をデータに取り込みます。
22
- 2. すべてのセルが空白である列をデータから除きます。
23
- 3. すべてのセルが空白である行をデータから除きます。
24
- 4. 最も左上(上が優先)にある keyword に一致するセルより上および左の行・列をデータから除きます。
22
+ 2. 最も左上(上が優先)にある keyword に一致するセルより上および左の行・列をデータから除きます。
23
+ 3. その時点のデータの上から順に見てすべてのセルが空白である行以下の行をデータから除きます。
24
+ 4. すべてのセルが空白である列をデータから除きます。
25
25
 
26
26
  Args:
27
27
  book_path: Excel book のパス。
@@ -29,6 +29,7 @@ def parse_excel(book_path, sheet_name, keyword, required, optional) -> pd.DataFr
29
29
  keyword (str): 必ず含まれるべき、表データの最初の列名として使う文字列。
30
30
  required (list[str]): 必ず含まれるべき、表データの列名として使う文字列のリスト。
31
31
  optional (list[str]): 表データの列名として使ってよい文字列のリスト。
32
+ raise_if_no_keyword (bool, optional): 指定されたシートに keyword が含まれない場合エラーにするかどうか。
32
33
 
33
34
  Returns:
34
35
 
@@ -41,21 +42,31 @@ def parse_excel(book_path, sheet_name, keyword, required, optional) -> pd.DataFr
41
42
  # 読み込み
42
43
  df = pd.read_excel(book_path, sheet_name, header=None)
43
44
 
44
- # NaN のみからなる列を削除する
45
- valid_columns = [col for col in df.columns if df[col].notna().sum()]
46
- df = df[valid_columns]
47
-
48
- # NaN のみからなる行を削除する
49
- valid_rows = [row for row in df.index if df.loc[row].notna().sum()]
50
- df = df.loc[valid_rows]
51
-
52
45
  # 「変数名」を左上とする表にする
53
- df: pd.DataFrame
54
46
  idx = np.where(df.values == keyword)
47
+ if len(idx[0]) == 0:
48
+ if raise_if_no_keyword:
49
+ raise RuntimeError(f'keyword "{keyword}" is lacked in {sheet_name}. ')
50
+ else:
51
+ return pd.DataFrame()
52
+
55
53
  r = idx[0][0]
56
54
  c = idx[1][0]
57
55
  df = pd.DataFrame(df.iloc[1+r:, c:].values, columns=df.iloc[r, c:].values)
58
56
 
57
+ # NaN のみからなる行を最初に見つけるまでデータに追加する
58
+ valid_rows = []
59
+ for row in df.index:
60
+ if df.loc[row].notna().sum():
61
+ valid_rows.append(row)
62
+ else:
63
+ break
64
+ df = df.loc[valid_rows]
65
+
66
+ # NaN のみからなる列を削除する
67
+ valid_columns = [col for i, col in enumerate(df.columns) if df.iloc[:, i].notna().sum()]
68
+ df = df[valid_columns]
69
+
59
70
  # パースが成功しているかチェックする
60
71
  lack = True
61
72
  for col in df.columns:
@@ -76,8 +87,15 @@ class ParseBase:
76
87
  OPTIONAL_COLUMNS = []
77
88
 
78
89
  @classmethod
79
- def parse(cls, book_path, sheet_name) -> pd.DataFrame:
80
- return parse_excel(book_path, sheet_name, cls.KEYWORD, cls.REQUIRED_COLUMNS, cls.OPTIONAL_COLUMNS)
90
+ def parse(cls, book_path, sheet_name, raise_if_no_keyword=True) -> pd.DataFrame:
91
+ return parse_excel(
92
+ book_path,
93
+ sheet_name,
94
+ cls.KEYWORD,
95
+ cls.REQUIRED_COLUMNS,
96
+ cls.OPTIONAL_COLUMNS,
97
+ raise_if_no_keyword,
98
+ )
81
99
 
82
100
 
83
101
  class ParseAsParameter(ParseBase):
pyfemtet/opt/_femopt.py CHANGED
@@ -131,7 +131,7 @@ class FEMOpt:
131
131
  # メンバーの宣言
132
132
  self.client = None
133
133
  self.status = None # actor
134
- self.history = None # actor
134
+ self.history: History = None
135
135
  self.worker_status_list = None # [actor]
136
136
  self.monitor_process_future = None
137
137
  self.monitor_server_kwargs = dict()
@@ -139,6 +139,7 @@ class FEMOpt:
139
139
  self.monitor_host_record = None
140
140
  self._hv_reference = None
141
141
  self._extra_space_dir = None
142
+ self._opt_exceptions = []
142
143
 
143
144
  # multiprocess 時に pickle できないオブジェクト参照の削除
144
145
  def __getstate__(self):
@@ -925,6 +926,7 @@ class FEMOpt:
925
926
  sleep(1) # monitor が terminated 状態で少なくとも一度更新されなければ running のまま固まる
926
927
 
927
928
  # 全ての Exception を再表示
929
+ self._opt_exceptions = opt_exceptions
928
930
  for i, opt_exception in enumerate(opt_exceptions):
929
931
  if opt_exception is not None:
930
932
  print()
@@ -325,7 +325,7 @@ class Function:
325
325
  args = self.args
326
326
  # Femtet 特有の処理
327
327
  if isinstance(fem, FemtetInterface):
328
- args = (fem.Femtet, *args)
328
+ args = (fem.object_passed_to_functions, *args)
329
329
  return float(self.fun(*args, **self.kwargs))
330
330
 
331
331
  def _restore_constants(self):
@@ -553,7 +553,7 @@ class History:
553
553
  hv_reference=None,
554
554
  ):
555
555
  # hypervolume 計算メソッド
556
- self._hv_reference = hv_reference or 'dynamic-pareto'
556
+ self._hv_reference = 'dynamic-pareto' if hv_reference is None else hv_reference
557
557
 
558
558
  # 引数の処理
559
559
  self.path = history_path # .csv
@@ -728,6 +728,14 @@ class History:
728
728
 
729
729
  return columns, meta_columns
730
730
 
731
+ def generate_hidden_infeasible_result(self):
732
+ y = np.full_like(np.zeros(len(self.obj_names)), np.nan)
733
+ c = np.full_like(np.zeros(len(self.cns_names)), np.nan)
734
+ return y, c
735
+
736
+ def is_hidden_infeasible_result(self, y):
737
+ return np.all(np.isnan(y))
738
+
731
739
  def record(
732
740
  self,
733
741
  parameters,
@@ -780,7 +788,7 @@ class History:
780
788
  feasible_list.append(is_feasible(cns_value, cns.lb, cns.ub))
781
789
 
782
790
  # feasibility
783
- row.append(all(feasible_list))
791
+ row.append(all(feasible_list) and not self.is_hidden_infeasible_result(obj_values))
784
792
 
785
793
  # the others
786
794
  row.append(-1.) # dummy hypervolume
@@ -892,7 +900,29 @@ class History:
892
900
  ret.append(row)
893
901
  return np.array(ret).max(axis=0)
894
902
 
895
- if self._hv_reference == 'dynamic-pareto':
903
+ if (
904
+ isinstance(self._hv_reference, np.ndarray)
905
+ or isinstance(self._hv_reference, list)
906
+ ):
907
+ _buff = np.array(self._hv_reference)
908
+ assert _buff.shape == (len(self.obj_names),)
909
+
910
+ ref_point = np.array(
911
+ [obj.convert(raw_value) for obj, raw_value in zip(objectives.values(), _buff)]
912
+ )
913
+
914
+ _buff = get_pareto(objective_values)
915
+
916
+ pareto_set = np.empty((0, len(objectives)))
917
+ for pareto_sol in _buff:
918
+ if all(pareto_sol < ref_point):
919
+ pareto_set = np.concatenate([pareto_set, [pareto_sol]], axis=0)
920
+
921
+ hv = compute_hypervolume(pareto_set, ref_point)
922
+ df.loc[len(df) - 1, 'hypervolume'] = hv
923
+ return
924
+
925
+ elif self._hv_reference == 'dynamic-pareto':
896
926
  pareto_set, pareto_set_list = get_pareto(objective_values, with_partial=True)
897
927
  for i, partial_pareto_set in enumerate(pareto_set_list):
898
928
  # 並列計算時など Valid な解がまだ一つもない場合は pareto_set が長さ 0 になる
@@ -943,28 +973,6 @@ class History:
943
973
  df.loc[len(df) - 1, 'hypervolume'] = hv
944
974
  return
945
975
 
946
- elif (
947
- isinstance(self._hv_reference, np.ndarray)
948
- or isinstance(self._hv_reference, list)
949
- ):
950
- _buff = np.array(self._hv_reference)
951
- assert _buff.shape == (len(self.obj_names),)
952
-
953
- ref_point = np.array(
954
- [obj.convert(raw_value) for obj, raw_value in zip(objectives.values(), _buff)]
955
- )
956
-
957
- _buff = get_pareto(objective_values)
958
-
959
- pareto_set = np.empty((0, len(objectives)))
960
- for pareto_sol in _buff:
961
- if all(pareto_sol < ref_point):
962
- pareto_set = np.concatenate([pareto_set, [pareto_sol]], axis=0)
963
-
964
- hv = compute_hypervolume(pareto_set, ref_point)
965
- df.loc[len(df) - 1, 'hypervolume'] = hv
966
- return
967
-
968
976
  else:
969
977
  raise NotImplementedError(f'Invalid Hypervolume reference point calculation method: {self._hv_reference}')
970
978
 
@@ -92,6 +92,7 @@ def is_equal_result(ref_path, dif_path, log_path=None, threashold=0.05):
92
92
  assert len(ref_columns) == len(dif_columns), "結果 csv の column 数が異なります。"
93
93
  assert len(ref_df) == len(dif_df), "結果 csv の row 数が異なります。"
94
94
  assert difference <= threashold*100, f"前回の結果との平均差異が {int(difference)}% で {int(threashold*100)}% を超えています。"
95
+ print(f"OK! 前回の結果との平均差異が {int(difference)}% で {int(threashold*100)}% 以下です。")
95
96
 
96
97
 
97
98
  def _get_simplified_df_values(csv_path, exclude_columns=None):
@@ -0,0 +1,163 @@
1
+ # for meta script
2
+ import os
3
+ import pyfemtet
4
+
5
+ if __name__ == '__main__':
6
+ print(f'pyfemtet {pyfemtet.__version__} starting.')
7
+
8
+ from pyfemtet._message.messages import encoding
9
+
10
+ from fire import Fire
11
+ import yaml
12
+
13
+ # for concrete script
14
+ from pyfemtet.opt import FEMOpt
15
+ from pyfemtet.opt.interface import *
16
+ from pyfemtet.opt.optimizer import *
17
+ from optuna.samplers import *
18
+ from pyfemtet.opt.interface._femtet_excel import FemtetWithExcelSettingsInterface
19
+
20
+
21
+ # fore debug
22
+ class ContentContext:
23
+
24
+ def __init__(self, content_):
25
+ self.content = content_
26
+
27
+ def __enter__(self):
28
+ return self.content
29
+
30
+ def __exit__(self, exc_type, exc_val, exc_tb):
31
+ pass
32
+
33
+
34
+ def main(
35
+ yaml_path: str = None,
36
+
37
+ interface_class: str = None, # including parameter definition excel
38
+ interface_kwargs: str = None, # including Parametric Analysis Output
39
+ optimizer_class: str = None,
40
+ optimizer_kwargs: str = None,
41
+ femopt_kwargs: str = None,
42
+ seed: str = 'null',
43
+ optimize_kwargs: str = None,
44
+ ):
45
+ """
46
+
47
+ Args:
48
+ yaml_path:
49
+ If this argument is passed, the other arguments will be ignored
50
+ and load by .yaml file.
51
+ The yaml file must contain the other arguments.
52
+
53
+ interface_class: FemtetWithExcelSettingsInterface or SurrogateModelInterface.
54
+ interface_kwargs: See documentation of each interface class.
55
+ optimizer_class: OptunaOptimizer or ScipyOptimizer.
56
+ optimizer_kwargs: See documentation of each optimizer class.
57
+ femopt_kwargs: See documentation of FEMOpt.
58
+ seed: int or None.
59
+ optimize_kwargs: See documentation of FEMOpt.optimize().
60
+
61
+ """
62
+
63
+ if yaml_path is not None:
64
+ if os.path.isfile(yaml_path):
65
+ context = open(yaml_path, 'r', encoding='utf-8')
66
+ else:
67
+ print('debug mode')
68
+ context = ContentContext(yaml_path)
69
+ with context as f:
70
+ d = yaml.safe_load(f)
71
+ interface_class = yaml.safe_dump(d['interface_class'], allow_unicode=True)
72
+ interface_kwargs = yaml.safe_dump(d['interface_kwargs'], allow_unicode=True)
73
+ optimizer_class = yaml.safe_dump(d['optimizer_class'], allow_unicode=True)
74
+ optimizer_kwargs = yaml.safe_dump(d['optimizer_kwargs'], allow_unicode=True)
75
+ femopt_kwargs = yaml.safe_dump(d['femopt_kwargs'], allow_unicode=True)
76
+ seed = yaml.safe_dump(d['seed'], allow_unicode=True)
77
+ optimize_kwargs = yaml.safe_dump(d['optimize_kwargs'], allow_unicode=True)
78
+
79
+ Interface = eval(yaml.safe_load(interface_class))
80
+ interface_kwargs_ = yaml.safe_load(interface_kwargs)
81
+
82
+ Optimizer = eval(yaml.safe_load(optimizer_class))
83
+ optimizer_kwargs_ = yaml.safe_load(optimizer_kwargs)
84
+
85
+ femopt_kwargs_ = yaml.safe_load(femopt_kwargs)
86
+
87
+ seed_ = yaml.safe_load(seed)
88
+
89
+ optimize_kwargs_ = yaml.safe_load(optimize_kwargs)
90
+
91
+ fem = Interface(**interface_kwargs_)
92
+ opt = Optimizer(**optimizer_kwargs_)
93
+ femopt = FEMOpt(fem=fem, opt=opt, **femopt_kwargs_)
94
+ femopt.set_random_seed(seed_)
95
+ femopt.optimize(**optimize_kwargs_)
96
+
97
+
98
+ # if __name__ == '__main__':
99
+ # Fire(main)
100
+
101
+
102
+ # for debugging file input
103
+ if __name__ == '__main__':
104
+ os.chdir(os.path.dirname(__file__))
105
+ path = 'sample.yaml'
106
+ main(yaml_path=path)
107
+
108
+
109
+ # for debugging yaml input
110
+ # if __name__ == '__main__':
111
+ # content = r"""
112
+ # interface_class: FemtetInterface
113
+ # interface_kwargs:
114
+ # femprj_path: C:\日本語ファイル.femprj
115
+ # optimizer_class: OptunaOptimizer
116
+ # optimizer_kwargs:
117
+ # sampler_class: TPESampler
118
+ # sampler_kwargs:
119
+ # n_startup_trials: 10
120
+ # femopt_kwargs:
121
+ # history_path: sample.csv
122
+ # optimize_kwargs:
123
+ # n_trials: 15
124
+ # confirm_before_exit: False
125
+ # seed: null
126
+ # """
127
+ #
128
+ # main(yaml_path=content)
129
+
130
+
131
+ # for debugging CLI input
132
+ # if __name__ == '__main__':
133
+ #
134
+ # interface_kwargs = dict(
135
+ # femprj_path=r'sample.femprj',
136
+ # parametric_output_indexes_use_as_objective={0: 0, 1: 'minimize'}
137
+ # )
138
+ # optimizer_kwargs = dict(
139
+ # sampler_class='TPESampler',
140
+ # sampler_kwargs=dict(
141
+ # n_startup_trials=10,
142
+ # )
143
+ # )
144
+ # femopt_kwargs = dict(
145
+ # history_path='sample.csv'
146
+ # )
147
+ #
148
+ # seed = None
149
+ #
150
+ # optimize_kwargs = dict(
151
+ # n_trials=15,
152
+ # confirm_before_exit=False,
153
+ # )
154
+ #
155
+ # main(
156
+ # interface_class='FemtetWithExcelSettingsInterface',
157
+ # interface_kwargs=yaml.safe_dump(interface_kwargs, allow_unicode=True),
158
+ # optimizer_class='OptunaOptimizer',
159
+ # optimizer_kwargs=yaml.safe_dump(optimizer_kwargs, allow_unicode=True),
160
+ # femopt_kwargs=yaml.safe_dump(femopt_kwargs, allow_unicode=True),
161
+ # seed=yaml.safe_dump(femopt_kwargs, allow_unicode=True),
162
+ # optimize_kwargs=yaml.safe_dump(optimize_kwargs, allow_unicode=True),
163
+ # )
@@ -0,0 +1,14 @@
1
+  interface_class: FemtetInterface
2
+ interface_kwargs:
3
+ femprj_path: C:\日本語ファイル.femprj
4
+ optimizer_class: OptunaOptimizer
5
+ optimizer_kwargs:
6
+ sampler_class: TPESampler
7
+ sampler_kwargs:
8
+ n_startup_trials: 10
9
+ femopt_kwargs:
10
+ history_path: sample.csv
11
+ optimize_kwargs:
12
+ n_trials: 15
13
+ confirm_before_exit: False
14
+ seed: null
@@ -0,0 +1,99 @@
1
+ import os
2
+ from time import sleep
3
+
4
+ from optuna.samplers import RandomSampler, NSGAIISampler, GPSampler, BaseSampler
5
+
6
+ from pyfemtet.opt import FEMOpt, FemtetInterface, OptunaOptimizer
7
+
8
+ os.chdir(os.path.dirname(__file__))
9
+
10
+
11
+ def get_res_freq(Femtet):
12
+ Galileo = Femtet.Gogh.Galileo
13
+ Galileo.Mode = 0
14
+ sleep(0.01)
15
+ return Galileo.GetFreq().Real
16
+
17
+
18
+ def main(n_trials, sampler_class: type[BaseSampler], sampler_kwargs: dict):
19
+ """Main function
20
+
21
+ length
22
+ using different algorithms for each restarting.
23
+
24
+ So this main function requires n_trials and sampler_class.
25
+
26
+ Args:
27
+
28
+ n_trials (int):
29
+ How many additional succeeded trials
30
+ to terminate optimization.
31
+
32
+ sampler_class (type[optuna.samplers.BaseSampler]):
33
+ The algorithm we use.
34
+
35
+ sampler_kwargs (dict):
36
+ The arguments of sampler.
37
+
38
+ """
39
+
40
+
41
+ # Connect to Femtet.
42
+ fem = FemtetInterface(
43
+ femprj_path='gal_ex13_parametric.femprj',
44
+ )
45
+
46
+ # Initialize the optimization object.
47
+ opt = OptunaOptimizer(
48
+ sampler_class=sampler_class,
49
+ sampler_kwargs=sampler_kwargs,
50
+ )
51
+
52
+ # To restart, it is necessary to inform the new optimization program
53
+ # about the history of the previous optimization.
54
+ # By specifying a csv for the `history_path` argument of FEMOpt,
55
+ # if it does not exist, a new csv file will be created,
56
+ # and if it exists, optimization will continue from that csv file.
57
+ #
58
+ # Note:
59
+ # When restarting, the number and names of variables,
60
+ # as well as the number and names of objective functions
61
+ # and constraints must be consistent.
62
+ # However, you can change the bounds of variables,
63
+ # direction of objective functions, and content of constraints.
64
+ #
65
+ # Note:
66
+ # When using OptunaOptimizer, the .db file with the same name
67
+ # (in this case restarting-sample.db) that is saved along with
68
+ # csv is required to be in the same folder as the csv file.
69
+ femopt = FEMOpt(
70
+ fem=fem,
71
+ opt=opt,
72
+ history_path='restarting-sample.csv'
73
+ )
74
+
75
+ # Set the design variables.
76
+ femopt.add_parameter('length', 0.1, 0.02, 0.2)
77
+ femopt.add_parameter('width', 0.01, 0.001, 0.02)
78
+ femopt.add_parameter('base_radius', 0.008, 0.006, 0.01)
79
+
80
+ # Set the objective function.
81
+ femopt.add_objective(fun=get_res_freq, name='First Resonant Frequency (Hz)', direction=800)
82
+
83
+ # Run optimization.
84
+ femopt.set_random_seed(42)
85
+ femopt.optimize(n_trials=n_trials, confirm_before_exit=False)
86
+
87
+
88
+ if __name__ == '__main__':
89
+ # First, we will perform 3 optimizations using the RandomSampler.
90
+ main(3, RandomSampler, {})
91
+
92
+ # Next, we will perform 3 optimizations using the NSGAIISampler.
93
+ main(3, NSGAIISampler, {})
94
+
95
+ # Finally, we will perform 3 optimizations using the GPSampler.
96
+ main(3, GPSampler, {'n_startup_trials': 0, 'deterministic_objective': True})
97
+
98
+ # After this program ends, you can continue further with
99
+ # restarting-sample.csv and the .db file.