5.2. 関数¶
出力関数を除く関数では引数にキーワード引数を用いることはできません.
5.2.1. 条件関数¶
- pysimple.Condition(elm, cond)¶
条件式を表す Cond 型を生成する関数です.
- パラメータ
elm (
element-like
or tuple of them) -- Element の列を与えます.cond (Cond or tuple of them) -- 条件式の列を与えます.
注釈
i!=2 や (1<i) & (i<3) のような簡単な場合は Condition 関数を用いなくても構いません.
サンプル
>>> i = Element(value=[1,2,3], name='i') >>> Condition((i, i), i!=2) ((i,i), (i!=2))[i,i] in [(1, 1), (3, 3)] >>> Condition(i, (1<i, i<3)) (i, ((i>1),(i<3)))[i] in [2] >>> ij = Element(value=[(1,3), (1,4), (2,3)], name='ij') >>> Condition(ij, ij(1)!=4) (ij, (ij(1)!=4))[ij] in [(1, 3), (2, 3)]
5.2.2. 出力関数¶
- pysimple.Printf(format_string, *args, **kwds)¶
書式指定文字列を標準出力に出力する関数です. Python の書式指定文字列の文法に加えて,PySIMPLE のオブジェクトに対応しています.
- パラメータ
format_string (string) -- 書式を指定する文字列です.文法は「書式指定文字列の文法 」 を参照してください.
*args -- キーワードなし引数です.
**kwds -- キーワードあり引数です.
サンプル
>>> i = Element(value=[1,2], name='i') >>> a = Parameter(index=i, value={1: 10, 2: 20}, name='a') >>> Printf('{}[{}] = {}', a.name, i, a[i]) a[1] = 10 a[2] = 20 >>> Printf('{name}[{}] = {value}', i, name=a.name, value=a[i]) a[1] = 10 a[2] = 20 >>> x = Variable(index=i, name='x') >>> Printf('{}: ({}=){} >= {}', x[i]>=a[i], x[i], x[i].val, a[i]) x[1]>=10: (x[1]=)0 >= 10 x[2]>=20: (x[2]=)0 >= 20
- pysimple.Fprintf(file_like, format_string, *args, **kwds)¶
書式指定文字列をファイルに出力する関数です. Python の書式指定文字列の文法に加えて,PySIMPLE のオブジェクトに対応しています.
- パラメータ
file_like (file-like) -- ファイルオブジェクト,sys.stdout などの出力先を指定するオブジェクトです.
format_string (string) --
書式を指定する文字列です.文法は「書式指定文字列の文法 」 を参照してください.
*args -- キーワードなし引数です.
**kwds -- キーワードあり引数です.
サンプル
>>> import sys >>> i = Element(value=[1,2], name='i') >>> a = Parameter(index=i, value={1: 10, 2: 20}, name='a') >>> Fprintf(sys.stdout, '{}[{}] = {}', a.name, i, a[i]) a[1] = 10 a[2] = 20 >>> Fprintf(sys.stdout, '{name}[{}] = {avalue}', i, name=a.name, avalue=a[i]) a[1] = 10 a[2] = 20
5.2.3. 数学関数¶
数学関数.範囲関数,一変数関数,二変数関数,多変数関数に分かれる.
- pysimple.func.Acos(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.acos(obj) の結果を, それ以外の場合は math.acos を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Acosh(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.acosh(obj) の結果を, それ以外の場合は math.acosh を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Asin(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.asin(obj) の結果を, それ以外の場合は math.asin を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Asinh(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.asinh(obj) の結果を, それ以外の場合は math.asinh を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Atan(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.atan(obj) の結果を, それ以外の場合は math.atan を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Atan2(obj1, obj2)¶
- パラメータ
obj1 (
funcable-type
) --obj2 (
funcable-type
) --
- 戻り値
obj1, obj2 が
data-type
の場合は math.atan2(obj1, obj2) の結果を, それ以外の場合は math.atan2 を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Atanh(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.atanh(obj) の結果を, それ以外の場合は math.atanh を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Ceil()¶
__ceil__(obj)
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.ceil(obj) の結果を, それ以外の場合は math.ceil を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Cos(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.cos(obj) の結果を, それ以外の場合は math.cos を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Cosh(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.cosh(obj) の結果を, それ以外の場合は math.cosh を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Erf(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.erf(obj) の結果を, それ以外の場合は math.erf を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Exp(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.exp(obj) の結果を, それ以外の場合は math.exp を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Fabs()¶
__abs__(obj)
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.fabs(obj) の結果を, それ以外の場合は math.fabs を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Floor()¶
__floor__(obj)
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.floor(obj) の結果を, それ以外の場合は math.floor を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Fmod(x, y)¶
- pysimple.func.Hypot(obj1, obj2)¶
- パラメータ
obj1 (
funcable-type
) --obj2 (
funcable-type
) --
- 戻り値
obj1, obj2 が
data-type
の場合は math.hypot(obj1, obj2) の結果を, それ以外の場合は math.hypot を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Log(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.log(obj) の結果を, それ以外の場合は math.log を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Log10(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.log10(obj) の結果を, それ以外の場合は math.log10 を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Max(obj, idx=None)¶
obj の範囲 idx 上での最大値を計算したオブジェクトを返します.
- パラメータ
obj (
funcable-type
) -- 最大値を計算する対象のオブジェクトです.idx (None or
element-like
or tuple of them) -- 最大値を計算する範囲です. 省略された場合,obj のすべての範囲についての最大値を計算します. 同じ Element を複数回含めることはできません.
- 戻り値
添字が残る場合は Parameter を,残らない場合は
data-type
を返します.- 戻り値の型
data-type
or Parameter- 例外
ValueError -- duplicate Element in range of {}
ValueError -- Max arg is an empty index
注釈
obj や idx の添字範囲が空集合の場合は ValueError を投げます. idx が空タプルの場合は obj と等価なオブジェクトを返します.
サンプル
>>> I = Set(value=[1,2]); i = Element(set=I, name='i') >>> J = Set(value='XY'); j = Element(set=J, name='j') >>> a = Parameter(index=(i,j), value={(ii, jj): ii*10+J.index(jj) for ii, jj in I*J}, name='a') >>> a a[1,'X']=10 a[1,'Y']=11 a[2,'X']=20 a[2,'Y']=21 >>> Max(a[i,j], i) Max(a[i,j], i)['X']=20 Max(a[i,j], i)['Y']=21 >>> Max(a[i,j], j) Max(a[i,j], j)[1]=11 Max(a[i,j], j)[2]=21 >>> Max(a[i,j], (i,j)) 21 >>> Max(a[i,j]) 21
- pysimple.func.MaxOf(*args)¶
オブジェクトの各最大値を返します. 例えば変数の下限を一度に設定するときに便利です.
- パラメータ
*args (
funcable-type
) -- オブジェクトの列.1 つ以上必要です.- 例外
ValueError -- max() arg is an empty sequence
サンプル
>>> i = Element(value=[1,2,3], name='i') >>> a = Parameter(index=i, value={1: 2, 2: 3, 3: 1}, name='a') >>> b = Parameter(index=i, value={1: 3, 2: 1, 3: 2}, name='b') >>> Printf('i={i}, a={a}, b={b}', i=i, a=a[i], b=b[i]) i=1, a=2, b=3 i=2, a=3, b=1 i=3, a=1, b=2 >>> MaxOf(i, a[i], b[i]) MaxOf(i, a[i], b[i])[1]=3 MaxOf(i, a[i], b[i])[2]=3 MaxOf(i, a[i], b[i])[3]=3 >>> x = Variable(index=i, name='x') >>> x[i] >= MaxOf(i, a[i], b[i]) (x[i]>=MaxOf(i, a[i], b[i])[i]): x[1]>=3 x[2]>=3 x[3]>=3
- pysimple.func.Min(obj, idx=None)¶
obj の範囲 idx 上での最小値を計算したオブジェクトを返します.
- パラメータ
obj (
funcable-type
) -- 最小値を計算する対象のオブジェクトです.idx (None or
element-like
or tuple of them) -- 最小値を計算する範囲です. 省略された場合,obj のすべての範囲についての最小値を計算します. 同じ Element を複数回含めることはできません.
- 戻り値
添字が残る場合は Parameter を,残らない場合は
data-type
を返します.- 戻り値の型
data-type
or Parameter- 例外
ValueError -- duplicate Element in range of {}
ValueError -- Min arg is an empty index
注釈
obj や idx の添字範囲が空集合の場合は ValueError を投げます. idx が空タプルの場合は obj と等価なオブジェクトを返します.
サンプル
>>> I = Set(value=[1,2]); i = Element(set=I, name='i') >>> J = Set(value='XY'); j = Element(set=J, name='j') >>> a = Parameter(index=(i,j), value={(ii, jj): ii*10+J.index(jj) for ii, jj in I*J}, name='a') >>> a a[1,'X']=10 a[1,'Y']=11 a[2,'X']=20 a[2,'Y']=21 >>> Min(a[i,j], i) Min(a[i,j], i)['X']=10 Min(a[i,j], i)['Y']=11 >>> Min(a[i,j], j) Min(a[i,j], j)[1]=10 Min(a[i,j], j)[2]=20 >>> Min(a[i,j], (i,j)) 10 >>> Min(a[i,j]) 10
- pysimple.func.MinOf(*args)¶
オブジェクトの各最小値を返します. 例えば変数の上限を一度に設定するときに便利です.
- パラメータ
*args (
funcable-type
) -- オブジェクトの列.1 つ以上必要です.- 例外
ValueError -- min() arg is an empty sequence
サンプル
>>> i = Element(value=[1,2,3], name='i') >>> a = Parameter(index=i, value={1: 2, 2: 3, 3: 1}, name='a') >>> b = Parameter(index=i, value={1: 3, 2: 1, 3: 2}, name='b') >>> Printf('i={i}, a={a}, b={b}', i=i, a=a[i], b=b[i]) i=1, a=2, b=3 i=2, a=3, b=1 i=3, a=1, b=2 >>> MinOf(i, a[i], b[i]) MinOf(i, a[i], b[i])[1]=1 MinOf(i, a[i], b[i])[2]=1 MinOf(i, a[i], b[i])[3]=1 >>> x = Variable(index=i, name='x') >>> x[i] <= MinOf(i, a[i], b[i]) (x[i]<=MinOf(i, a[i], b[i])[i]): -x[1]>=-1 -x[2]>=-1 -x[3]>=-1
- pysimple.func.Prod(obj, idx=None)¶
obj の範囲 idx 上での積を計算したオブジェクトを返します.
- パラメータ
obj (
funcable-type
) -- 積を計算する対象のオブジェクトです.idx (None or
element-like
or tuple of them) -- 積を計算する範囲です. 省略された場合,obj のすべての範囲についての積を計算します. 同じ Element を複数回含めることはできません.
- 戻り値
添字が残る場合は Parameter を,残らない場合は
data-type
を返します.- 戻り値の型
data-type
or Parameter- 例外
ValueError -- duplicate Element in range of {}
注釈
obj や idx の添字範囲が空集合の場合は 1 を返します. idx が空タプルの場合は obj と等価なオブジェクトを返します.
サンプル
>>> I = Set(value=[1,2]); i = Element(set=I, name='i') >>> J = Set(value='XY'); j = Element(set=J, name='j') >>> a = Parameter(index=(i,j), value={(ii, jj): ii*10+J.index(jj) for ii, jj in I*J}, name='a') >>> a a[1,'X']=10 a[1,'Y']=11 a[2,'X']=20 a[2,'Y']=21 >>> Prod(a[i,j], i) Prod(a[i,j], i)['X']=200 Prod(a[i,j], i)['Y']=231 >>> Prod(a[i,j], j) Prod(a[i,j], j)[1]=110 Prod(a[i,j], j)[2]=420 >>> Prod(a[i,j], (i,j)) 46200 >>> Prod(a[i,j]) 46200
- pysimple.func.Sin(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.sin(obj) の結果を, それ以外の場合は math.sin を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Sinh(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.sinh(obj) の結果を, それ以外の場合は math.sinh を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Sqrt(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.sqrt(obj) の結果を, それ以外の場合は math.sqrt を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Sum(obj, idx=None)¶
obj の範囲 idx 上での和を計算したオブジェクトを返します.
- パラメータ
obj (
operable-type
) -- 和を計算する対象のオブジェクトです.idx (None or
element-like
or tuple of them) -- 和を計算する範囲です. 省略された場合,obj のすべての範囲についての和を計算します. 同じ Element を複数回含めることはできません.
- 戻り値
obj が Variable, Expression の場合は Expression を, それ以外で添字が残る場合は Parameter を,残らない場合は
data-type
を返します.- 戻り値の型
data-type
or Parameter or Expression- 例外
ValueError -- duplicate Element in range of {}
注釈
obj や idx の添字範囲が空集合の場合は 0 を返します. idx が空タプルの場合は obj と等価なオブジェクトを返します.
サンプル
>>> I = Set(value=[1,2]); i = Element(set=I, name='i') >>> J = Set(value='XY'); j = Element(set=J, name='j') >>> a = Parameter(index=(i,j), value={(ii, jj): ii*10+J.index(jj) for ii, jj in I*J}, name='a') >>> a a[1,'X']=10 a[1,'Y']=11 a[2,'X']=20 a[2,'Y']=21 >>> Sum(a[i,j], i) Sum(a[i,j], i)['X']=30 Sum(a[i,j], i)['Y']=32 >>> Sum(a[i,j], j) Sum(a[i,j], j)[1]=21 Sum(a[i,j], j)[2]=41 >>> Sum(a[i,j], (i,j)) 62 >>> Sum(a[i,j]) 62 >>> x = Variable(index=(i,j), name='x') >>> Sum(x[i,j], i) Sum(x[i,j], i): x[1,'X']+x[2,'X'] x[1,'Y']+x[2,'Y'] >>> Sum(x[i,j], j) Sum(x[i,j], j): x[1,'X']+x[1,'Y'] x[2,'X']+x[2,'Y'] >>> Sum(x[i,j], (i,j)) Sum(x[i,j], (i,j)): x[1,'X']+x[1,'Y']+x[2,'X']+x[2,'Y']
- pysimple.func.Tan(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.tan(obj) の結果を, それ以外の場合は math.tan を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
- pysimple.func.Tanh(obj)¶
- パラメータ
obj (
funcable-type
) --- 戻り値
obj が
data-type
の場合は math.tanh(obj) の結果を, それ以外の場合は math.tanh を各要素に適用した Parameter を返す.- 戻り値の型
number or Parameter
5.2.4. 選択関数¶
- pysimple.func.Selection(obj, idx=None)¶
添字つき 0-1 整数変数の中で一つだけを 1 に固定したい場合に用います. wcsp を解法とする場合,内部的には複数の 0-1 整数変数を用意する替わりに一つの離散変数を用意するため, 内部処理が高速化されます.それ以外では Sum を用いた書き換えと等価です.
- パラメータ
obj (BinaryVariable) -- 計算する対象の 0-1 整数変数です. 添字づけられている必要があります.
idx (None or
element-like
or tuple of them) -- 計算する範囲です. 省略された場合,obj のすべての範囲を対象とします. 同じ Element を複数回含めることはできません.
- 戻り値の型
- 例外
ValueError -- duplicate Element in range of {}
ValueError -- the argument of Selection should be indexed variable
ValueError -- Selection arg is an empty index
サンプル
>>> i = Element(value=[1, 2], name='i') >>> j = Element(value=[3, 4], name='j') >>> z = BinaryVariable(index=(i,j), name='z') >>> Selection(z[i,j], i) Selection(z[i,j], i): z[1,3]+z[2,3]==1 z[1,4]+z[2,4]==1 >>> Selection(z[i,j], j) Selection(z[i,j], j): z[1,3]+z[1,4]==1 z[2,3]+z[2,4]==1 >>> Selection(z[i,j], (i,j)) Selection(z[i,j], (i,j)): z[1,3]+z[1,4]+z[2,3]+z[2,4]==1
参考
5.2.5. 制約関数¶
- pysimple.constraint.HardConstraint()¶
制約式がハード制約であることを表します.
- 戻り値の型
- pysimple.constraint.SemiHardConstraint()¶
制約式がセミハード制約であることを表します.
- 戻り値の型
- pysimple.constraint.SoftConstraint(weight, quad=None, linear=None)¶
制約式がソフト制約であることを表します.
- パラメータ
weight (non-negative int or -1 or -2) -- 重みの基本となるパラメータです.
quad (non-negative float) -- 重みの二次の係数です.
linear (non-negative float) -- 重みの一次の係数です.
- 戻り値の型