B @`%@sdZddlZddlmZmZddlmZddlmZm Z ddl m Z m Z ddl mZmZyddlmZWndZYnXd d ZGd d d eee Zd dZGdddeee ZdS)N)IPySPSolutionSaverExtensionIPySPSolutionLoaderExtension) indexToString) implementsSingletonPlugin)PySPConfigBlocksafe_declare_common_option)PySPConfiguredObjectPySPConfiguredExtension) zip_longestcCsdx^|D]V}||}xH|D]@\}}t|tkr@|j|t|f}n|j||f}||j|<qWqWdS)N)typelistZ_name_index_to_idtuple _solution) tree_nodesolutionvarnameZvarsolindexval variable_idrr=/tmp/pip-unpacked-wheel-bi3529v6/pyomo/pysp/plugins/jsonio.pyload_node_solutions  rc@s6eZdZeeed ddZdZddZddZ dS) JSONSolutionLoaderExtensionNcCs&|dkrt}t|dt|d|S)N input_name load_stages)rr )clsoptionsrrr_declare_options/sz,JSONSolutionLoaderExtension._declare_optionsZ jsonloader_cCst|dS)N)r __init__)selfrrrrFsz$JSONSolutionLoaderExtension.__init__c Cs|ddk rd}t|dd}t|}WdQRXd}|dt|jjkrztd|d|dt|jjf|dt|krtd|d|d|dt|fxt |jj|D]\}}|dkrP|ddks|d|dkr|dkr"t d||d|df|d7}xV|j D]H}y||j }Wn(t k rlt d ||j fYnXt||q2WqPqWtd ||dfd Std t|jd S)NrrrrzqThe value of the %s option (%s) can not be greater than the number of time stages in the local scenario tree (%s)zThe value of the %s option (%s) can not be greater than the number of time stages in the scenario tree solution stored in %s (%s)zLocal scenario tree has fewer stages (%s) than what is held by the solution loaded from file %s. Use the option %s to limit the number of stages that are loaded.z`Local scenario tree contains a tree node that was not found in the solution at time-stage %s: %sz=Loaded scenario tree solution for %s time stages from file %sTzCNo value was set for %s option 'input_name'. Nothing will be saved.F) get_optionopenjsonloadlen scenario_treestages ValueErrorZget_full_option_namer RuntimeErrornodesnameKeyErrorrprintr __name__) r managerstage_solutionsfcntrstageZstage_solutionrZ node_solutionrrrr&IsV   z JSONSolutionLoaderExtension.load)N) r0 __module__ __qualname__rr classmethodr_default_options_prefixrr&rrrrr)s   rcCsi}x|jD]x}|j|\}}||kr.g||<||jkrR||||j|fq |j|\}}|t|}td|j||fdSq Wx,t|D]}t ||ddd||<qW|S)NzD%s: node solution missing for variable with scenario tree id %s (%s)cSs|dS)Nrr)xrrrz'extract_node_solution..)key) Z_standard_variable_idsZ _variable_idsrappendrr/r-rkeyssorted)rrrrrr-Z full_namerrrextract_node_solutions   rAc@s6eZdZeeed ddZdZddZddZ dS) JSONSolutionSaverExtensionNcCs&|dkrt}t|dt|d|S)N output_name save_stages)rr )rrrrrrsz+JSONSolutionSaverExtension._declare_optionsZ jsonsaver_cCst|dS)N)r r)r rrrrsz#JSONSolutionSaverExtension.__init__c Cs|ddk rg}t|dd}d}x|jjD]z}|ddksV|d|dkr|d7}i}x:|jD]0}t|}|dkrtd|jdS|||j<qjW||q2Pq2Wt j ||dd d WdQRXtd ||dfd Std t |j dS) NrCwrrDr"zQNo solution appears to be stored in node with name %s. No solution will be saved.FT)indent sort_keysz:Saved scenario tree solution for %s time stages to file %szDNo value was set for %s option 'output_name'. Nothing will be saved.) r#r$r(r)r,rAr/r-r>r%dumpr r0) r r1r2r3r4r5Znode_solutionsrZ_node_solutionrrrsaves2   zJSONSolutionSaverExtension.save)N) r0r6r7rrr8rr9rrJrrrrrBs   rB)__all__r%Zpyomo.pysp.solutionioextensionsrrZpyomo.pysp.phutilsrZpyomo.common.pluginrrZpyomo.pysp.util.configrr Z!pyomo.pysp.util.configured_objectr r Z six.movesr rrrArBrrrr s$   V