#!/usr/bin/env python ''' Solves Schittkowski's TP37 Constrained Problem. min -x1*x2*x3 s.t.: x1 + 2.*x2 + 2.*x3 - 72 <= 0 - x1 - 2.*x2 - 2.*x3 <= 0 0 <= xi <= 42, i = 1,2,3 f* = -3456 , x* = [24, 12, 12] ''' # ============================================================================= # Standard Python modules # ============================================================================= import os, sys, time import pdb # ============================================================================= # Extension modules # ============================================================================= #from pyOpt import * from pyOpt import Optimization from pyOpt import PSQP from pyOpt import SLSQP from pyOpt import CONMIN from pyOpt import COBYLA from pyOpt import SOLVOPT from pyOpt import KSOPT from pyOpt import NSGA2 from pyOpt import ALGENCAN from pyOpt import FILTERSD # ============================================================================= # # ============================================================================= def objfunc(x): f = - x[ 0 ] * x[ 1 ] * x[ 2 ] g = [ 0.0 ] * 2 g[ 0 ] = x[ 0 ] + 2. * x[ 1 ] + 2. * x[ 2 ] - 72.0 g[ 1 ] = - x[ 0 ] - 2. * x[ 1 ] - 2. * x[ 2 ] fail = 0 return f,g, fail # ============================================================================= # # ============================================================================= # Instantiate Optimization Problem opt_prob = Optimization( 'TP37 Constrained Problem' ,objfunc) opt_prob.addVar( 'x1' , 'c' ,lower = 0.0 ,upper = 42.0 ,value = 10.0 ) opt_prob.addVar( 'x2' , 'c' ,lower = 0.0 ,upper = 42.0 ,value = 10.0 ) opt_prob.addVar( 'x3' , 'c' ,lower = 0.0 ,upper = 42.0 ,value = 10.0 ) opt_prob.addObj( 'f' ) opt_prob.addCon( 'g1' , 'i' ) opt_prob.addCon( 'g2' , 'i' ) print opt_prob # Instantiate Optimizer (PSQP) & Solve Problem psqp = PSQP() psqp.setOption( 'IPRINT' , 0 ) psqp(opt_prob,sens_type = 'FD' ) print (opt_prob.solution( 0 )) # Instantiate Optimizer (SLSQP) & Solve Problem slsqp = SLSQP() slsqp.setOption( 'IPRINT' , - 1 ) slsqp(opt_prob,sens_type = 'FD' ) print (opt_prob.solution( 1 )) # Instantiate Optimizer (CONMIN) & Solve Problem conmin = CONMIN() conmin.setOption( 'IPRINT' , 0 ) conmin(opt_prob,sens_type = 'CS' ) print (opt_prob.solution( 2 )) # Instantiate Optimizer (COBYLA) & Solve Problem cobyla = COBYLA() cobyla.setOption( 'IPRINT' , 0 ) cobyla(opt_prob) print (opt_prob.solution( 3 )) # Instantiate Optimizer (SOLVOPT) & Solve Problem solvopt = SOLVOPT() solvopt.setOption( 'iprint' , - 1 ) solvopt(opt_prob,sens_type = 'FD' ) print (opt_prob.solution( 4 )) # Instantiate Optimizer (KSOPT) & Solve Problem ksopt = KSOPT() ksopt.setOption( 'IPRINT' , 0 ) ksopt(opt_prob,sens_type = 'FD' ) print (opt_prob.solution( 5 )) # Instantiate Optimizer (NSGA2) & Solve Problem nsga2 = NSGA2() nsga2.setOption( 'PrintOut' , 0 ) nsga2(opt_prob) print (opt_prob.solution( 6 )) # Instantiate Optimizer (ALGENCAN) & Solve Problem algencan = ALGENCAN() algencan.setOption( 'iprint' , 0 ) algencan(opt_prob) print (opt_prob.solution( 7 )) # Instantiate Optimizer (FILTERSD) & Solve Problem filtersd = FILTERSD() filtersd.setOption( 'iprint' , 0 ) filtersd(opt_prob) print opt_prob.solution( 8 ) |
Optimization Problem - - TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Objectives: Name Value Optimum f 0 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 10.000000 0.00e + 00 4.20e + 01 x2 c 10.000000 0.00e + 00 4.20e + 01 x3 c 10.000000 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = 0.000000 < = 0.00e + 00 g2 i - 1.00e + 21 < = 0.000000 < = 0.00e + 00 PSQP Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0010 Total Function Evaluations: 42 Sensitivities: FD Objectives: Name Value Optimum f - 3456 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 24.000000 0.00e + 00 4.20e + 01 x2 c 12.000000 0.00e + 00 4.20e + 01 x3 c 12.000000 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = 0.000000 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 72.000000 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SLSQP Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0010 Total Function Evaluations: 0 Sensitivities: FD Objectives: Name Value Optimum f - 3456 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 24.000000 0.00e + 00 4.20e + 01 x2 c 12.000000 0.00e + 00 4.20e + 01 x3 c 12.000000 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = 0.000000 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 72.000000 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CONMIN Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0010 Total Function Evaluations: 88 Sensitivities: CS Objectives: Name Value Optimum f - 3456 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 23.989921 0.00e + 00 4.20e + 01 x2 c 12.002518 0.00e + 00 4.20e + 01 x3 c 12.002518 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = - 0.000006 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 71.999994 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - COBYLA Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0010 Total Function Evaluations: 112 Objectives: Name Value Optimum f - 3456 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 24.000000 0.00e + 00 4.20e + 01 x2 c 11.999999 0.00e + 00 4.20e + 01 x3 c 12.000000 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = 0.000000 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 72.000000 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SOLVOPT Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0070 Total Function Evaluations: 201 Sensitivities: FD Objectives: Name Value Optimum f - 3456 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 23.999011 0.00e + 00 4.20e + 01 x2 c 12.000860 0.00e + 00 4.20e + 01 x3 c 11.999634 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = - 0.000000 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 72.000000 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - KSOPT Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0110 Total Function Evaluations: 2187 Sensitivities: FD Objectives: Name Value Optimum f - 3453.73 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 23.995259 0.00e + 00 4.20e + 01 x2 c 11.997588 0.00e + 00 4.20e + 01 x3 c 11.996896 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = - 0.015771 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 71.984229 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - NSGA - II Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0688 Total Function Evaluations: 0 Objectives: Name Value Optimum f - 3455.25 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 24.259110 0.00e + 00 4.20e + 01 x2 c 11.858676 0.00e + 00 4.20e + 01 x3 c 12.010719 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = - 0.002100 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 71.997900 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ALGENCAN Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0030 Total Function Evaluations: 0 Lambda: [ 144.00000063 0. ] Sensitivities: FD Objectives: Name Value Optimum f - 3456 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 24.000000 0.00e + 00 4.20e + 01 x2 c 12.000000 0.00e + 00 4.20e + 01 x3 c 12.000000 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = 0.000000 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 72.000000 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - FILTERSD Solution to TP37 Constrained Problem = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Objective Function: objfunc Solution: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Total Time: 0.0010 Total Function Evaluations: 69 Lambda: [ 0. 0. ] Sensitivities: FD Objectives: Name Value Optimum f - 3456 0 Variables (c - continuous, i - integer, d - discrete): Name Type Value Lower Bound Upper Bound x1 c 24.000000 0.00e + 00 4.20e + 01 x2 c 12.000000 0.00e + 00 4.20e + 01 x3 c 12.000000 0.00e + 00 4.20e + 01 Constraints (i - inequality, e - equality): Name Type Bounds g1 i - 1.00e + 21 < = 0.000000 < = 0.00e + 00 g2 i - 1.00e + 21 < = - 72.000000 < = 0.00e + 00 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
You are using pip version * * * * * , however version * * * * * is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command |
twisted 18.7 . 0 requires PyHamcrest> = 1.9 . 0 , which is not installed. |
OpenSeesPy(有限要素法) | pip install openseespy |
VTK(可視化ライブラリ) | conda install -c anaconda vtk |
Netgen(FEM用メッシュジェネレータ) | conda install -c conda-forge netgen |
wxPython(GUI作成) | pip install wxpython |
import matplotlib.font_manager as fm fonts = fm.findSystemFonts() for font in fonts: print (fm.FontProperties(fname = font).get_name()) |
import matplotlib.pyplot as plt plt.rcParams[ 'font.family' ] = 'IPAexGothic' #全体のフォントを設定 |
ax.set_title( 'title' , * * { 'family' : 'IPAexGothic' }) #タイトルのフォントのみをIPAexGothicに指定 ax.set_xlabel( 'x label' , * * { 'family' : 'IPAexGothic' }) #X軸ラベルのフォントのみをIPAexGothicに指定 |
# The font.size property is the default font size for text, given in pts. # 10 pt is the standard value. # font.family : sans - serif #font.style : normal #font.variant : normal #font.weight : medium #font.stretch : normal |
# The font.size property is the default font size for text, given in pts. # 10 pt is the standard value. # #font.family : sans-serif font.family : IPAexGothic #font.style : normal #font.variant : normal #font.weight : medium #font.stretch : normal |
import matplotlib print (matplotlib.matplotlib_fname()) |
X,Y,Z 0 , 0 , 0 1 , 1 , 1 2 , 2 , 2 ,, I,J, 0 , 1 , 1 , 2 , |
import csv filename = 'test.csv' foldername = 'datain' reader = csv.reader( open (foldername + '/' + filename)) r,ij = [],[] #rとijのリスト定義 next (reader) #ラベル行を読み飛ばす for row in reader: if row[ 0 ] = = '': break #空欄になるまで読み込む r.append([ float (row[ 0 ]), float (row[ 1 ]), float (row[ 2 ])]) #座標を実数として読み込み next (reader) #ラベル行を読み飛ばす for row in reader: if row[ 0 ] = = '': break #空欄になるまで読み込む ij.append([ int (row[ 0 ]), int (row[ 1 ])]) #要素節点関係を整数として読み込み print (r) #試しに出力してみる print (ij) #試しに出力してみる |
[[ 0.0 , 0.0 , 0.0 ], [ 1.0 , 1.0 , 1.0 ], [ 2.0 , 2.0 , 2.0 ]] [[ 0 , 1 ], [ 1 , 2 ]] |
import os output_filename = 'test2.csv' output_foldername = 'dataout' try :os.stat(output_foldername) #dataoutという名前のフォルダの有無を調査 except :os.mkdir(output_foldername) #なければ新規にdataoutという名前のフォルダを作成する writer = csv.writer( open (output_foldername + '/' + output_filename, 'w' ,newline = '')) writer.writerow([ 'X' , 'Y' , 'Z' ]) #ラベル行を出力 for ri in r: writer.writerow([ri[ 0 ],ri[ 1 ],ri[ 2 ]]) #rの値を1行ずつ出力 writer.writerow([' ',' ',' ']) #改行 writer.writerow([ 'I' , 'J' ]) #ラベル行を出力 for eij in ij: writer.writerow([eij[ 0 ],eij[ 1 ]]) #ijの値を1行ずつ出力 |
X,Y,Z 0.0 , 0.0 , 0.0 1.0 , 1.0 , 1.0 2.0 , 2.0 , 2.0 ,, I,J 0 , 1 1 , 2 |
import matplotlib.pyplot as plt #2次元グラフ描画のためのモジュールの読み込み x = [ 0 , 1 , 2 , 3 , 4 , 5 ] #x座標 y = [ 3 , 5 , 1 , 4 , 2 , 8 ] #y座標 plt.plot(x,y) #グラフの書込み plt.show() #描画 |
import matplotlib.pyplot as plt #2次元グラフ描画のためのモジュールの読み込み x = [ 0 , 1 , 2 , 3 , 4 , 5 ] #x座標 y = [ 3 , 5 , 1 , 4 , 2 , 8 ] #y座標 plt.plot(x,y,linestyle = 'dashed' ,linewidth = 0.5 ,color = 'black' , marker = '*' ,markeredgewidth = 1 ,markeredgecolor = 'blue' , markersize = 10 ,markerfacecolor = 'pink' ) #グラフの書込み(描画オプション付き) plt.show() #描画 |
import matplotlib.pyplot as plt #2次元グラフ描画のためのモジュールの読み込み x = [ 0 , 1 , 2 , 3 , 4 , 5 ] #x座標 y = [ 3 , 5 , 1 , 4 , 2 , 8 ] #y座標 plt.axes().set_aspect( 'equal' ) #縦横比を揃える plt.plot(x,y,linestyle = 'dashed' ,linewidth = 0.5 ,color = 'black' , marker = '*' ,markeredgewidth = 1 ,markeredgecolor = 'blue' , markersize = 10 ,markerfacecolor = 'pink' ) #グラフの書込み(描画オプション付き) plt.show() #描画 |
import matplotlib.pyplot as plt #2次元グラフ描画のためのモジュールの読み込み x = [ 0 , 1 , 2 , 3 , 4 , 5 ] #x座標 y = [ 3 , 5 , 1 , 4 , 2 , 8 ] #y座標 plt.axes().set_aspect( 'equal' ) #縦横比を揃える plt.plot(x,y,linestyle = 'dashed' ,linewidth = 0.5 ,color = 'black' , marker = '*' ,markeredgewidth = 1 ,markeredgecolor = 'blue' , markersize = 10 ,markerfacecolor = 'pink' ,label = 'test' ) #グラフの書込み(描画オプション付き) plt.title( 'graph example' ) #グラフタイトル plt.xlabel( 'X-axis' ) #X軸ラベル plt.ylabel( 'Y-axis' ) #Y軸ラベル plt.legend() #凡例の描画(plt.plotでlabelを指定する必要がある) plt.show() #描画 |
import matplotlib.pyplot as plt #2次元グラフ描画のためのモジュールの読み込み x = [ 0 , 1 , 2 , 3 , 4 , 5 ] #x座標 y = [ 3 , 5 , 1 , 4 , 2 , 8 ] #y座標 plt.axes().set_aspect( 'equal' ) #縦横比を揃える plt.plot(x,y,linestyle = 'dashed' ,linewidth = 0.5 ,color = 'black' , marker = '*' ,markeredgewidth = 1 ,markeredgecolor = 'blue' , markersize = 10 ,markerfacecolor = 'pink' ,label = 'test' ) #グラフの書込み(描画オプション付き) plt.tick_params(labelbottom = False ,bottom = False ) # x軸の削除 plt.tick_params(labelleft = False ,left = False ) # y軸の削除 plt.box( False ) #枠の消去 plt.show() #描画 |