(******************************************************************* This file was generated automatically by the Mathematica front end. It contains Initialization cells from a Notebook file, which typically will have the same name as this file except ending in ".nb" instead of ".m". This file is intended to be loaded into the Mathematica kernel using the package loading commands Get or Needs. Doing so is equivalent to using the Evaluate Initialization Cells menu command in the front end. DO NOT EDIT THIS FILE. This entire file is regenerated automatically each time the parent Notebook file is saved in the Mathematica front end. Any changes you make to this file will be overwritten. ***********************************************************************) BeginPackage["behseq`"] nacts::usage="The number of activities available to the animal. This must be set by the user." \ f::usage="An indexed set of functions giving the benefit of performing a given act for a given time, so f[3][4.5] is the benefit of performing a bout of action 3 for duration 4.5. The whole set of values of f must be defined by the user." \ g::usage="An indexed set of functions giving the benefit of not performing a given act for a given time, so g[3][4.5] is the benefit of not performing a bout of action 3 for duration 4.5. The whole set of values of g must be defined by the user." \ u::usage="u[{uf1, uf2...ufnacts}] must be set by the user to deliver the overall utility when the utility fragment for activities 1, 2... nacts are uf1, uf2... ufnacts. u[i][b] must be set by the user, for each i=1,2...nacts, to give the utility fragment for a net benefit rate of b in relation to activity i." \ tstart::usage="tstart[i] must be set by the user to give the starting duration used by the maximisation routine for each bout of activity i, for each i=1,2...nacts." \ optseq::usage="optseq[{a1,a2,a3...an}] calculates the utility attained and the required optimal durations when the sequence of activities is a1, a2... an. Not all activities need be included in the ai, and activities can be repeated." \ sortedseqs::usage="sortedseqs[len] takes all unique permitted sequences of activities of length len, applies optseq to them, and sorts the results in order of utility. sortedseqs[{minlen, maxlen}] does the same for all sequences with lengths between minlen and maxlen." \ paramlist::usage="paramlist is a list of rules specified by the user, and applied by the package when the utility function and starting durations are calculated numerically. It thus allows parameters of the f- and g-functions, and of u, and also the values of tstart, to be conveniently set by the user." \ follow::usage= "follow[i] for i=1,2...nacts must be set by the user before using sortedseqs. It must be set to deliver the list of activities that are permitted to follow activity i." Unprotect[optseq, sortedseqs] Begin["`Private`"] fillu[l_List]:= Map[Append[l[[#]], addrest[l, l[[#,1]], If[#==Length[l],1,#+1], Length[l]]]&, Range[Length[l]]]; addrest[l_List, oractivity_Integer, startpos_Integer, len_Integer]:= If[l[[startpos,1]]==oractivity, 0, l[[startpos,2]]+ addrest[l,oractivity, If[startposToString[#]]&,Range[nt]] checklist:={{Hold[checknacts], "nacts must be set to an integer greater than or equal to 2"}, {Hold[ checkfgs], "One of the f or g functions is not delivering a real number when it should. Check the definitions and that all parameters have been assigned values."}\ ,{Hold[checkui], "One of the u[i] functions is not delivering a real number when it should. Check the definitions and that all parameters have been assigned values."}\ ,{Hold[checku], "The function u[{uf1,uf2...ufnacts}] is not delivering a real number when it should. Check the definition and that all parameters have been assigned values."}\ , {Hold[checktstart], "One of the tstart[i] is not delivering a real number. Check the definition and that all parameters have been assigned values."}\ }; dochecks[{}]:=True; dochecks[l_]:= If[First[Release[First[l]]]===True,dochecks[Rest[l]],Print[l[[1,2]]]; False,Print[l[[1,2]]];False]; checknacts:=Head[nacts]\[Equal]Integer && 2\[LessEqual]nacts; checkfgs:=And@@(Head[N[f[#][3.4]/.paramlist]]\[Equal]Real&/@Range[nacts]) && And@@(Head[N[g[#][3.4]/.paramlist]]\[Equal]Real&/@Range[nacts]); checkui:=And@@(Head[N[u[#][3.4]/.paramlist]]\[Equal]Real&/@Range[nacts]); checku:=Head[N[u[1.0&/@Range[nacts]]]/.paramlist]\[Equal]Real; checktstart:=And@@(Head[N[tstart[#]/.paramlist]]\[Equal]Real&/@Range[nacts]); optseq[actseq_]:=If[dochecks[checklist],inoptseq[actseq,nacts]]; inoptseq[actseq_,nacts_]:= MapAt[(-#)&,fixop[myfindmin1[actseq,nacts,tsyms[Length[actseq]]]],1]; myfindmin1[actseq_,nacts_,ts_]:= (Clear[myminimand]; myminimand[{forts__?NumericQ}]=-utilr[ Transpose[{actseq,Exp/@tsyms[Length[actseq]]}],nacts]/.Join[ paramlist,{ts\[Rule]{forts}}];FindMinimum[ myminimand[ts], Evaluate[Sequence@@ starts[actseq,ts]], MaxIterations\[Rule]100]); starts[actseq_,ts_]:= Table[{ts[[ii]],Log[tstart[actseq[[ii]]]/.paramlist]},{ii,1,Length[ts]}] followcons[seq_]:= If[Length[seq]\[LessEqual]1,"Too short", If[! MemberQ[follow[Last[seq]],First[seq]],"ends problem", infollowcons[seq]]]; infollowcons[seq_]:= If[Length[seq]\[Equal]1,True, If[MemberQ[follow[seq[[1]]],seq[[2]]],infollowcons[Rest[seq]], ToString[seq]]] allseqs[nacts_,seqlen_]:= Flatten[Array[List,Table[nacts,{seqlen}]],seqlen-1] allconseqs[nacts_,seqlen_]:= Select[allseqs[nacts,seqlen],followcons] pureallconseqs[nacts_,seqlen_]:= purify[allconseqs[nacts,seqlen]] purify[l_]:=If[Length[l]<=1,l, Select[{First[l], Sequence@@purify[Rest[l],banned[First[l]]]},!(#===Null)&]]; purify[restl_,bannedl_]:= If[Length[restl]\[Equal]1, If[MemberQ[bannedl,First[restl]], Null, First[restl]], If[MemberQ[bannedl,First[restl]], purify[Rest[restl],bannedl], {First[restl], Sequence@@ purify[Rest[restl],Union[bannedl,banned[First[restl]]]]}]]; banned[el_]:= RotateLeft[el,#]&/@Range[Length[el]] scanseqs[len_,nacts_]:= Block[{pac=pureallconseqs[nacts,len]}, If[Length[pac]\[Equal]0,{},{#,inoptseq[#,nacts]}&/@pac]] checkfollows:= And@@(Head[follow[#]/.paramlist]\[Equal]List&/@Range[nacts]) && And@@Flatten[(Map[Head[#/.paramlist]\[Equal]Integer&, Table[follow[i],{i,1,nacts}],{2}])] && And@@(!MemberQ[follow[#]/.paramlist,#/.paramlist]&/@Range[nacts]) && And@@(Length[follow[#/.paramlist]]\[GreaterEqual]1&/@Range[nacts]); sortedseqs[maxlen_]:= If[dochecks[ Join[checklist, {{Hold[checkfollows], "One of the follow[i] has not been specified as a list of some integers between 1 and nacts, or one of the follow[i] contains i itself."}\ }]],insortedseqs[maxlen,nacts]]; insortedseqs[maxlen_,nacts_]:= Sort[(Flatten[scanseqs[#,nacts]&/@Range[2,maxlen],1]), (#1[[2,1]]>#2[[2,1]])&] insortedseqs[{minlen_,maxlen_},nacts_]:= Sort[(Flatten[scanseqs[#,nacts]&/@Range[minlen,maxlen],1]), (#1[[2,1]]>#2[[2,1]])&] fixop[l_]:=l/.{Rule[x_,n_]\[Rule]Rule[x,Exp[n]]} End[] SetAttributes[optseq, ReadProtected]; SetAttributes[ sortedseqs,ReadProtected]; Protect[optseq, sortedseqs]; EndPackage[]