├── .gitignore ├── LICENSE.txt ├── NOTICE.txt ├── README.txt ├── docs ├── _config.yml ├── images │ ├── checker.png │ └── jsdp_logo.png ├── index.html ├── jar │ ├── jsdp-1.0.1-javadoc.jar │ ├── jsdp-1.0.1-shaded.jar │ ├── jsdp-1.0.1-sources.jar │ └── jsdp-1.0.1.jar ├── javadoc │ ├── allclasses-frame.html │ ├── allclasses-noframe.html │ ├── constant-values.html │ ├── deprecated-list.html │ ├── help-doc.html │ ├── index-all.html │ ├── index.html │ ├── jsdp │ │ ├── app │ │ │ ├── control │ │ │ │ └── clqg │ │ │ │ │ └── univariate │ │ │ │ │ ├── CLQG.html │ │ │ │ │ ├── class-use │ │ │ │ │ └── CLQG.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ └── package-use.html │ │ │ ├── inventory │ │ │ │ ├── capital │ │ │ │ │ ├── CF_Action.html │ │ │ │ │ ├── CF_ForwardRecursion.html │ │ │ │ │ ├── CF_State.html │ │ │ │ │ ├── CF_StateDescriptor.html │ │ │ │ │ ├── CF_StateSpace.html │ │ │ │ │ ├── CF_TransitionProbability.html │ │ │ │ │ ├── CapitalFlow.html │ │ │ │ │ ├── CapitalFlowBatch.html │ │ │ │ │ ├── ImmediateValueFunction.html │ │ │ │ │ ├── class-use │ │ │ │ │ │ ├── CF_Action.html │ │ │ │ │ │ ├── CF_ForwardRecursion.html │ │ │ │ │ │ ├── CF_State.html │ │ │ │ │ │ ├── CF_StateDescriptor.html │ │ │ │ │ │ ├── CF_StateSpace.html │ │ │ │ │ │ ├── CF_TransitionProbability.html │ │ │ │ │ │ ├── CapitalFlow.html │ │ │ │ │ │ ├── CapitalFlowBatch.html │ │ │ │ │ │ └── ImmediateValueFunction.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ └── package-use.html │ │ │ │ ├── multivariate │ │ │ │ │ ├── ActionIteratorImpl.html │ │ │ │ │ ├── ActionSampleIteratorImpl.html │ │ │ │ │ ├── MultiItemStochasticLotSizing.html │ │ │ │ │ ├── class-use │ │ │ │ │ │ ├── ActionIteratorImpl.html │ │ │ │ │ │ ├── ActionSampleIteratorImpl.html │ │ │ │ │ │ └── MultiItemStochasticLotSizing.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ └── package-use.html │ │ │ │ └── univariate │ │ │ │ │ ├── CapacitatedStochasticLotSizing.html │ │ │ │ │ ├── StochasticLotSizing.html │ │ │ │ │ ├── class-use │ │ │ │ │ ├── CapacitatedStochasticLotSizing.html │ │ │ │ │ └── StochasticLotSizing.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ ├── package-use.html │ │ │ │ │ └── simulation │ │ │ │ │ ├── SimulatePolicies.html │ │ │ │ │ ├── class-use │ │ │ │ │ ├── SimulatePolicies.html │ │ │ │ │ └── sS_Policy.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ ├── package-use.html │ │ │ │ │ └── sS_Policy.html │ │ │ ├── lotsizing │ │ │ │ ├── class-use │ │ │ │ │ ├── sS_Action.html │ │ │ │ │ ├── sS_BackwardRecursion.html │ │ │ │ │ ├── sS_CostRepository.html │ │ │ │ │ ├── sS_ForwardRecursion.html │ │ │ │ │ ├── sS_SequentialBackwardRecursion.html │ │ │ │ │ ├── sS_State.html │ │ │ │ │ ├── sS_StateDescriptor.html │ │ │ │ │ ├── sS_StateSpace.html │ │ │ │ │ ├── sS_StateSpaceIterator.html │ │ │ │ │ ├── sS_StateSpaceSampleIterator.html │ │ │ │ │ ├── sS_TransitionProbability.html │ │ │ │ │ └── sS_jsdp.html │ │ │ │ ├── package-frame.html │ │ │ │ ├── package-summary.html │ │ │ │ ├── package-tree.html │ │ │ │ ├── package-use.html │ │ │ │ ├── sS_Action.html │ │ │ │ ├── sS_BackwardRecursion.html │ │ │ │ ├── sS_CostRepository.html │ │ │ │ ├── sS_ForwardRecursion.html │ │ │ │ ├── sS_SequentialBackwardRecursion.html │ │ │ │ ├── sS_State.html │ │ │ │ ├── sS_StateDescriptor.html │ │ │ │ ├── sS_StateSpace.html │ │ │ │ ├── sS_StateSpaceIterator.html │ │ │ │ ├── sS_StateSpaceSampleIterator.html │ │ │ │ ├── sS_TransitionProbability.html │ │ │ │ ├── sS_jsdp.html │ │ │ │ └── simulation │ │ │ │ │ ├── SimulatePolicies.html │ │ │ │ │ ├── class-use │ │ │ │ │ └── SimulatePolicies.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ └── package-use.html │ │ │ ├── maintenance │ │ │ │ ├── MaintenanceScheduling.html │ │ │ │ ├── class-use │ │ │ │ │ └── MaintenanceScheduling.html │ │ │ │ ├── package-frame.html │ │ │ │ ├── package-summary.html │ │ │ │ ├── package-tree.html │ │ │ │ └── package-use.html │ │ │ ├── routing │ │ │ │ ├── deterministic │ │ │ │ │ ├── BR_Action.html │ │ │ │ │ ├── BR_ForwardRecursion.html │ │ │ │ │ ├── BR_State.html │ │ │ │ │ ├── BR_StateDescriptor.html │ │ │ │ │ ├── BR_StateSpace.html │ │ │ │ │ ├── BR_TransitionProbability.html │ │ │ │ │ ├── BowserRouting.html │ │ │ │ │ ├── BowserRoutingBatch.html │ │ │ │ │ ├── class-use │ │ │ │ │ │ ├── BR_Action.html │ │ │ │ │ │ ├── BR_ForwardRecursion.html │ │ │ │ │ │ ├── BR_State.html │ │ │ │ │ │ ├── BR_StateDescriptor.html │ │ │ │ │ │ ├── BR_StateSpace.html │ │ │ │ │ │ ├── BR_TransitionProbability.html │ │ │ │ │ │ ├── BowserRouting.html │ │ │ │ │ │ └── BowserRoutingBatch.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ └── package-use.html │ │ │ │ ├── stochastic │ │ │ │ │ ├── fuel │ │ │ │ │ │ ├── BRF_Action.html │ │ │ │ │ │ ├── BRF_ForwardRecursion.html │ │ │ │ │ │ ├── BRF_State.html │ │ │ │ │ │ ├── BRF_StateDescriptor.html │ │ │ │ │ │ ├── BRF_StateSpace.html │ │ │ │ │ │ ├── BRF_TransitionProbability.html │ │ │ │ │ │ ├── BowserRoutingFuel.html │ │ │ │ │ │ ├── BowserRoutingFuelBatch.html │ │ │ │ │ │ ├── class-use │ │ │ │ │ │ │ ├── BRF_Action.html │ │ │ │ │ │ │ ├── BRF_ForwardRecursion.html │ │ │ │ │ │ │ ├── BRF_State.html │ │ │ │ │ │ │ ├── BRF_StateDescriptor.html │ │ │ │ │ │ │ ├── BRF_StateSpace.html │ │ │ │ │ │ │ ├── BRF_TransitionProbability.html │ │ │ │ │ │ │ ├── BowserRoutingFuel.html │ │ │ │ │ │ │ └── BowserRoutingFuelBatch.html │ │ │ │ │ │ ├── package-frame.html │ │ │ │ │ │ ├── package-summary.html │ │ │ │ │ │ ├── package-tree.html │ │ │ │ │ │ └── package-use.html │ │ │ │ │ └── location │ │ │ │ │ │ ├── BRL_Action.html │ │ │ │ │ │ ├── BRL_ForwardRecursion.html │ │ │ │ │ │ ├── BRL_State.html │ │ │ │ │ │ ├── BRL_StateDescriptor.html │ │ │ │ │ │ ├── BRL_StateSpace.html │ │ │ │ │ │ ├── BRL_TransitionProbability.html │ │ │ │ │ │ ├── BowserRoutingLocation.html │ │ │ │ │ │ ├── BowserRoutingLocationBatch.html │ │ │ │ │ │ ├── class-use │ │ │ │ │ │ ├── BRL_Action.html │ │ │ │ │ │ ├── BRL_ForwardRecursion.html │ │ │ │ │ │ ├── BRL_State.html │ │ │ │ │ │ ├── BRL_StateDescriptor.html │ │ │ │ │ │ ├── BRL_StateSpace.html │ │ │ │ │ │ ├── BRL_TransitionProbability.html │ │ │ │ │ │ ├── BowserRoutingLocation.html │ │ │ │ │ │ └── BowserRoutingLocationBatch.html │ │ │ │ │ │ ├── package-frame.html │ │ │ │ │ │ ├── package-summary.html │ │ │ │ │ │ ├── package-tree.html │ │ │ │ │ │ └── package-use.html │ │ │ │ └── topologies │ │ │ │ │ ├── Location.html │ │ │ │ │ ├── Topology.html │ │ │ │ │ ├── class-use │ │ │ │ │ ├── Location.html │ │ │ │ │ └── Topology.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ └── package-use.html │ │ │ ├── skeleton │ │ │ │ ├── StochasticDynamicProgram.html │ │ │ │ ├── class-use │ │ │ │ │ └── StochasticDynamicProgram.html │ │ │ │ ├── package-frame.html │ │ │ │ ├── package-summary.html │ │ │ │ ├── package-tree.html │ │ │ │ └── package-use.html │ │ │ └── standalone │ │ │ │ ├── deterministic │ │ │ │ ├── Knapsack.html │ │ │ │ ├── class-use │ │ │ │ │ └── Knapsack.html │ │ │ │ ├── package-frame.html │ │ │ │ ├── package-summary.html │ │ │ │ ├── package-tree.html │ │ │ │ └── package-use.html │ │ │ │ └── stochastic │ │ │ │ ├── GamblersRuin.html │ │ │ │ ├── InventoryControl.html │ │ │ │ ├── class-use │ │ │ │ ├── GamblersRuin.html │ │ │ │ └── InventoryControl.html │ │ │ │ ├── package-frame.html │ │ │ │ ├── package-summary.html │ │ │ │ ├── package-tree.html │ │ │ │ └── package-use.html │ │ ├── sdp │ │ │ ├── Action.html │ │ │ ├── ActionIterator.html │ │ │ ├── BackwardRecursion.html │ │ │ ├── BestActionRepository.html │ │ │ ├── ForwardRecursion.html │ │ │ ├── HashType.html │ │ │ ├── ImmediateValueFunction.html │ │ │ ├── RandomOutcomeFunction.html │ │ │ ├── Recursion.OptimisationDirection.html │ │ │ ├── Recursion.html │ │ │ ├── State.html │ │ │ ├── StateAction.html │ │ │ ├── StateDescriptor.html │ │ │ ├── StateSpace.html │ │ │ ├── StateSpaceIterator.html │ │ │ ├── StateTransitionFunction.html │ │ │ ├── TransitionProbability.html │ │ │ ├── ValueRepository.html │ │ │ ├── class-use │ │ │ │ ├── Action.html │ │ │ │ ├── ActionIterator.html │ │ │ │ ├── BackwardRecursion.html │ │ │ │ ├── BestActionRepository.html │ │ │ │ ├── ForwardRecursion.html │ │ │ │ ├── HashType.html │ │ │ │ ├── ImmediateValueFunction.html │ │ │ │ ├── RandomOutcomeFunction.html │ │ │ │ ├── Recursion.OptimisationDirection.html │ │ │ │ ├── Recursion.html │ │ │ │ ├── State.html │ │ │ │ ├── StateAction.html │ │ │ │ ├── StateDescriptor.html │ │ │ │ ├── StateSpace.html │ │ │ │ ├── StateSpaceIterator.html │ │ │ │ ├── StateTransitionFunction.html │ │ │ │ ├── TransitionProbability.html │ │ │ │ └── ValueRepository.html │ │ │ ├── impl │ │ │ │ ├── multivariate │ │ │ │ │ ├── ActionImpl.html │ │ │ │ │ ├── BackwardRecursionImpl.html │ │ │ │ │ ├── SamplingScheme.html │ │ │ │ │ ├── StateDescriptorImpl.html │ │ │ │ │ ├── StateImpl.html │ │ │ │ │ ├── StateSpaceImpl.html │ │ │ │ │ ├── StateSpaceIteratorImpl.html │ │ │ │ │ ├── StateSpaceSampleIteratorImpl.html │ │ │ │ │ ├── TransitionProbabilityImpl.html │ │ │ │ │ ├── class-use │ │ │ │ │ │ ├── ActionImpl.html │ │ │ │ │ │ ├── BackwardRecursionImpl.html │ │ │ │ │ │ ├── SamplingScheme.html │ │ │ │ │ │ ├── StateDescriptorImpl.html │ │ │ │ │ │ ├── StateImpl.html │ │ │ │ │ │ ├── StateSpaceImpl.html │ │ │ │ │ │ ├── StateSpaceIteratorImpl.html │ │ │ │ │ │ ├── StateSpaceSampleIteratorImpl.html │ │ │ │ │ │ └── TransitionProbabilityImpl.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ └── package-use.html │ │ │ │ └── univariate │ │ │ │ │ ├── ActionImpl.html │ │ │ │ │ ├── BackwardRecursionImpl.html │ │ │ │ │ ├── SamplingScheme.html │ │ │ │ │ ├── StateDescriptorImpl.html │ │ │ │ │ ├── StateImpl.html │ │ │ │ │ ├── StateSpaceImpl.html │ │ │ │ │ ├── StateSpaceIteratorImpl.html │ │ │ │ │ ├── StateSpaceSampleIteratorImpl.html │ │ │ │ │ ├── TransitionProbabilityImpl.html │ │ │ │ │ ├── class-use │ │ │ │ │ ├── ActionImpl.html │ │ │ │ │ ├── BackwardRecursionImpl.html │ │ │ │ │ ├── SamplingScheme.html │ │ │ │ │ ├── StateDescriptorImpl.html │ │ │ │ │ ├── StateImpl.html │ │ │ │ │ ├── StateSpaceImpl.html │ │ │ │ │ ├── StateSpaceIteratorImpl.html │ │ │ │ │ ├── StateSpaceSampleIteratorImpl.html │ │ │ │ │ └── TransitionProbabilityImpl.html │ │ │ │ │ ├── package-frame.html │ │ │ │ │ ├── package-summary.html │ │ │ │ │ ├── package-tree.html │ │ │ │ │ └── package-use.html │ │ │ ├── package-frame.html │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ └── package-use.html │ │ └── utilities │ │ │ ├── hash │ │ │ ├── MapDBHashTable.Storage.html │ │ │ ├── MapDBHashTable.html │ │ │ ├── class-use │ │ │ │ ├── MapDBHashTable.Storage.html │ │ │ │ └── MapDBHashTable.html │ │ │ ├── package-frame.html │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ └── package-use.html │ │ │ ├── monitoring │ │ │ ├── MonitoringInterface.html │ │ │ ├── MonitoringInterfaceBackward.html │ │ │ ├── MonitoringInterfaceForward.html │ │ │ ├── class-use │ │ │ │ ├── MonitoringInterface.html │ │ │ │ ├── MonitoringInterfaceBackward.html │ │ │ │ └── MonitoringInterfaceForward.html │ │ │ ├── package-frame.html │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ └── package-use.html │ │ │ ├── probdist │ │ │ ├── DiscreteDistributionFactory.html │ │ │ ├── MultiINIDistribution.html │ │ │ ├── SafeMultinomialDist.html │ │ │ ├── class-use │ │ │ │ ├── DiscreteDistributionFactory.html │ │ │ │ ├── MultiINIDistribution.html │ │ │ │ └── SafeMultinomialDist.html │ │ │ ├── package-frame.html │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ └── package-use.html │ │ │ └── sampling │ │ │ ├── SampleFactory.html │ │ │ ├── class-use │ │ │ └── SampleFactory.html │ │ │ ├── package-frame.html │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ └── package-use.html │ ├── overview-frame.html │ ├── overview-summary.html │ ├── overview-tree.html │ ├── package-list │ ├── script.js │ ├── serialized-form.html │ └── stylesheet.css ├── javascripts │ └── scale.fix.js ├── jsdp │ ├── .settings │ │ └── org.eclipse.core.resources.prefs │ └── target │ │ ├── classes │ │ ├── META-INF │ │ │ ├── MANIFEST.MF │ │ │ └── maven │ │ │ │ └── org.jsdp │ │ │ │ └── jsdp │ │ │ │ ├── pom.properties │ │ │ │ └── pom.xml │ │ └── log4j2.xml │ │ └── test-classes │ │ └── log4j2.xml ├── params.json ├── robots.txt └── stylesheets │ ├── github-dark.css │ ├── github-light.css │ ├── normalize.css │ ├── styles.css │ └── stylesheet.css └── jsdp ├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── pom.xml └── src ├── main ├── java │ └── jsdp │ │ ├── app │ │ ├── control │ │ │ └── clqg │ │ │ │ └── univariate │ │ │ │ └── CLQG.java │ │ ├── inventory │ │ │ ├── capital │ │ │ │ ├── CF_Action.java │ │ │ │ ├── CF_ForwardRecursion.java │ │ │ │ ├── CF_State.java │ │ │ │ ├── CF_StateDescriptor.java │ │ │ │ ├── CF_StateSpace.java │ │ │ │ ├── CF_TransitionProbability.java │ │ │ │ ├── CapitalFlow.java │ │ │ │ ├── CapitalFlowBatch.java │ │ │ │ └── ImmediateValueFunction.java │ │ │ ├── multivariate │ │ │ │ ├── ActionIteratorImpl.java │ │ │ │ ├── ActionSampleIteratorImpl.java │ │ │ │ └── MultiItemStochasticLotSizing.java │ │ │ └── univariate │ │ │ │ ├── CapacitatedStochasticLotSizing.java │ │ │ │ ├── CapacitatedStochasticLotSizingBatch.java │ │ │ │ ├── StochasticLotSizing.java │ │ │ │ └── simulation │ │ │ │ ├── SimulatePolicies.java │ │ │ │ ├── sS_Policy.java │ │ │ │ └── skSk_Policy.java │ │ ├── lotsizing │ │ │ ├── sS_Action.java │ │ │ ├── sS_BackwardRecursion.java │ │ │ ├── sS_CostRepository.java │ │ │ ├── sS_ForwardRecursion.java │ │ │ ├── sS_SequentialBackwardRecursion.java │ │ │ ├── sS_State.java │ │ │ ├── sS_StateDescriptor.java │ │ │ ├── sS_StateSpace.java │ │ │ ├── sS_StateSpaceIterator.java │ │ │ ├── sS_StateSpaceSampleIterator.java │ │ │ ├── sS_TransitionProbability.java │ │ │ ├── sS_jsdp.java │ │ │ └── simulation │ │ │ │ └── SimulatePolicies.java │ │ ├── maintenance │ │ │ └── MaintenanceScheduling.java │ │ ├── routing │ │ │ ├── deterministic │ │ │ │ ├── BR_Action.java │ │ │ │ ├── BR_ForwardRecursion.java │ │ │ │ ├── BR_State.java │ │ │ │ ├── BR_StateDescriptor.java │ │ │ │ ├── BR_StateSpace.java │ │ │ │ ├── BR_TransitionProbability.java │ │ │ │ ├── BowserRouting.java │ │ │ │ └── BowserRoutingBatch.java │ │ │ ├── stochastic │ │ │ │ ├── fuel │ │ │ │ │ ├── BRF_Action.java │ │ │ │ │ ├── BRF_ForwardRecursion.java │ │ │ │ │ ├── BRF_State.java │ │ │ │ │ ├── BRF_StateDescriptor.java │ │ │ │ │ ├── BRF_StateSpace.java │ │ │ │ │ ├── BRF_TransitionProbability.java │ │ │ │ │ ├── BowserRoutingFuel.java │ │ │ │ │ └── BowserRoutingFuelBatch.java │ │ │ │ └── location │ │ │ │ │ ├── BRL_Action.java │ │ │ │ │ ├── BRL_ForwardRecursion.java │ │ │ │ │ ├── BRL_State.java │ │ │ │ │ ├── BRL_StateDescriptor.java │ │ │ │ │ ├── BRL_StateSpace.java │ │ │ │ │ ├── BRL_TransitionProbability.java │ │ │ │ │ ├── BowserRoutingLocation.java │ │ │ │ │ └── BowserRoutingLocationBatch.java │ │ │ └── topologies │ │ │ │ ├── Location.java │ │ │ │ └── Topology.java │ │ ├── skeleton │ │ │ └── StochasticDynamicProgram.java │ │ └── standalone │ │ │ ├── deterministic │ │ │ └── Knapsack.java │ │ │ └── stochastic │ │ │ ├── GamblersRuin.java │ │ │ ├── InventoryControl.java │ │ │ ├── capacitated │ │ │ └── CapacitatedStochasticLotSizingFast.java │ │ │ ├── lateraltransshipment │ │ │ └── LateralTransshipment.java │ │ │ ├── servicelevel │ │ │ └── StochasticLotSizingFast.java │ │ │ └── ss │ │ │ └── StochasticLotSizingFast.java │ │ ├── sdp │ │ ├── Action.java │ │ ├── ActionIterator.java │ │ ├── BackwardRecursion.java │ │ ├── BestActionRepository.java │ │ ├── ForwardRecursion.java │ │ ├── HashType.java │ │ ├── ImmediateValueFunction.java │ │ ├── RandomOutcomeFunction.java │ │ ├── Recursion.java │ │ ├── State.java │ │ ├── StateAction.java │ │ ├── StateDescriptor.java │ │ ├── StateSpace.java │ │ ├── StateSpaceIterator.java │ │ ├── StateTransitionFunction.java │ │ ├── TransitionProbability.java │ │ ├── ValueRepository.java │ │ └── impl │ │ │ ├── multivariate │ │ │ ├── ActionImpl.java │ │ │ ├── BackwardRecursionImpl.java │ │ │ ├── SamplingScheme.java │ │ │ ├── StateDescriptorImpl.java │ │ │ ├── StateImpl.java │ │ │ ├── StateSpaceImpl.java │ │ │ ├── StateSpaceIteratorImpl.java │ │ │ ├── StateSpaceSampleIteratorImpl.java │ │ │ └── TransitionProbabilityImpl.java │ │ │ └── univariate │ │ │ ├── ActionImpl.java │ │ │ ├── BackwardRecursionImpl.java │ │ │ ├── SamplingScheme.java │ │ │ ├── StateDescriptorImpl.java │ │ │ ├── StateImpl.java │ │ │ ├── StateSpaceImpl.java │ │ │ ├── StateSpaceIteratorImpl.java │ │ │ ├── StateSpaceSampleIteratorImpl.java │ │ │ └── TransitionProbabilityImpl.java │ │ └── utilities │ │ ├── hash │ │ └── MapDBHashTable.java │ │ ├── monitoring │ │ ├── MonitoringInterface.java │ │ ├── MonitoringInterfaceBackward.java │ │ └── MonitoringInterfaceForward.java │ │ ├── probdist │ │ ├── DiscreteDistributionFactory.java │ │ ├── MultiINIDistribution.java │ │ └── SafeMultinomialDist.java │ │ └── sampling │ │ └── SampleFactory.java └── resources │ └── log4j2.xml └── test ├── java └── jsdp │ └── utilities │ └── sampling │ ├── AllTests.java │ ├── NormalSampleTest.java │ └── PoissonSampleTest.java └── resources └── log4j2.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Roberto Rossi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | ====================== 2 | THIRD PARTY COMPONENTS 3 | ====================== 4 | 5 | JSDP relies upon the following components, released under the licenses listed below. 6 | 7 | 8 | === Apache License Version 2.0 - http://www.apache.org/licenses/ === 9 | 10 | SSJ: Stochastic Simulation in Java http://simul.iro.umontreal.ca/ssj/indexe.html 11 | Apache Log4j http://logging.apache.org/log4j/ 12 | Apache Commons Lang http://commons.apache.org/proper/commons-lang/ 13 | Apache Maven Shade Plugin http://maven.apache.org/plugins/maven-shade-plugin/ 14 | MapDB http://www.mapdb.org 15 | 16 | 17 | === Eclipse Public License 1.0 - http://www.eclipse.org/legal/epl-v10.html === 18 | 19 | JUnit http://junit.org/junit4/ 20 | 21 | 22 | === Lesser GNU Public License (LGPL) - http://www.gnu.org/licenses/ === 23 | 24 | Trove4j http://trove4j.sourceforge.net/ 25 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | =================================================== 2 | jsdp: a Java Stochastic Dynamic Programming Library 3 | =================================================== 4 | 5 | http://gwr3n.github.io/jsdp/ 6 | 7 | jsdp provides a general purpose Java library for 8 | modeling and solving Stochastic Dynamic Programs. 9 | 10 | The library features a number of applications in 11 | maintenance, stochastic optimal control, and 12 | stochastic lot sizing; including the computation 13 | of optimal nonstationary (s,S) policy parameters, 14 | as discussed by Herbert Scarf in his seminal work 15 | the optimality of (s,S) policies in the dynamic 16 | inventory problem. 17 | 18 | To learn more please take a tour of our Wiki. 19 | 20 | https://github.com/gwr3n/jsdp/wiki 21 | 22 | jsdp is maintained by Roberto Rossi, 23 | Full Professsor and Chair in Uncertainty Modelling 24 | at the University of Edinburgh. 25 | 26 | To cite jsdp: 27 | 28 | @software{jsdp_github, 29 | author = {Roberto Rossi}, 30 | title = {jsdp: a Java Stochastic Dynamic Programming library}, 31 | url = {http://gwr3n.github.io/jsdp/}, 32 | version = {1.0.1}, 33 | year = {2018} 34 | } 35 | 36 | @misc{2209.09979, 37 | Author = {Roberto Rossi}, 38 | Title = {jsdp: a Java Stochastic Dynamic Programming Library}, 39 | Year = {2022}, 40 | Eprint = {arXiv:2209.09979} 41 | } 42 | -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | gems: 2 | - jekyll-sitemap -------------------------------------------------------------------------------- /docs/images/checker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gwr3n/jsdp/dfbdba78bcef6b4949bf99131fb565b1be306183/docs/images/checker.png -------------------------------------------------------------------------------- /docs/images/jsdp_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gwr3n/jsdp/dfbdba78bcef6b4949bf99131fb565b1be306183/docs/images/jsdp_logo.png -------------------------------------------------------------------------------- /docs/jar/jsdp-1.0.1-javadoc.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gwr3n/jsdp/dfbdba78bcef6b4949bf99131fb565b1be306183/docs/jar/jsdp-1.0.1-javadoc.jar -------------------------------------------------------------------------------- /docs/jar/jsdp-1.0.1-shaded.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gwr3n/jsdp/dfbdba78bcef6b4949bf99131fb565b1be306183/docs/jar/jsdp-1.0.1-shaded.jar -------------------------------------------------------------------------------- /docs/jar/jsdp-1.0.1-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gwr3n/jsdp/dfbdba78bcef6b4949bf99131fb565b1be306183/docs/jar/jsdp-1.0.1-sources.jar -------------------------------------------------------------------------------- /docs/jar/jsdp-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gwr3n/jsdp/dfbdba78bcef6b4949bf99131fb565b1be306183/docs/jar/jsdp-1.0.1.jar -------------------------------------------------------------------------------- /docs/javadoc/constant-values.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Constant Field Values (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 | 23 | 26 | 27 |
28 | 29 | 30 |
Skip navigation links
31 | 32 | 33 | 34 | 44 |
45 | 72 | 73 |
74 |

Constant Field Values

75 |

Contents

76 |
77 | 78 |
79 | 80 | 81 |
Skip navigation links
82 | 83 | 84 | 85 | 95 |
96 | 123 | 124 |

Copyright © 2017–2018. All rights reserved.

125 | 126 | 127 | -------------------------------------------------------------------------------- /docs/javadoc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp 1.0.0 API 8 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | <noscript> 69 | <div>JavaScript is disabled on your browser.</div> 70 | </noscript> 71 | <h2>Frame Alert</h2> 72 | <p>This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to <a href="overview-summary.html">Non-frame version</a>.</p> 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/control/clqg/univariate/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.control.clqg.univariate (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.control.clqg.univariate

14 |
15 |

Classes

16 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/inventory/capital/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.inventory.capital (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.inventory.capital

14 |
15 |

Interfaces

16 | 19 |

Classes

20 | 30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/inventory/multivariate/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.inventory.multivariate (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.inventory.multivariate

14 |
15 |

Classes

16 | 21 |
22 | 23 | 24 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/inventory/univariate/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.inventory.univariate (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.inventory.univariate

14 |
15 |

Classes

16 | 20 |
21 | 22 | 23 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/inventory/univariate/simulation/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.inventory.univariate.simulation (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.inventory.univariate.simulation

14 |
15 |

Classes

16 | 20 |
21 | 22 | 23 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/lotsizing/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.lotsizing (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.lotsizing

14 |
15 |

Classes

16 | 30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/lotsizing/simulation/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.lotsizing.simulation (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.lotsizing.simulation

14 |
15 |

Classes

16 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/maintenance/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.maintenance (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.maintenance

14 |
15 |

Classes

16 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/routing/deterministic/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.routing.deterministic (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.routing.deterministic

14 |
15 |

Classes

16 | 26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/routing/stochastic/fuel/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.routing.stochastic.fuel (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.routing.stochastic.fuel

14 |
15 |

Classes

16 | 26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/routing/stochastic/location/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.routing.stochastic.location (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.routing.stochastic.location

14 |
15 |

Classes

16 | 26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/routing/topologies/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.routing.topologies (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.routing.topologies

14 |
15 |

Classes

16 | 20 |
21 | 22 | 23 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/skeleton/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.skeleton (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.skeleton

14 |
15 |

Classes

16 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/standalone/deterministic/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.standalone.deterministic (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.standalone.deterministic

14 |
15 |

Classes

16 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/app/standalone/stochastic/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.app.standalone.stochastic (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.app.standalone.stochastic

14 |
15 |

Classes

16 | 20 |
21 | 22 | 23 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/sdp/impl/multivariate/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.sdp.impl.multivariate (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.sdp.impl.multivariate

14 |
15 |

Classes

16 | 26 |

Enums

27 | 30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/sdp/impl/univariate/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.sdp.impl.univariate (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.sdp.impl.univariate

14 |
15 |

Classes

16 | 26 |

Enums

27 | 30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/sdp/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.sdp (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.sdp

14 |
15 |

Interfaces

16 | 21 |

Classes

22 | 37 |

Enums

38 | 42 |
43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/utilities/hash/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.utilities.hash (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.utilities.hash

14 |
15 |

Classes

16 | 19 |

Enums

20 | 23 |
24 | 25 | 26 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/utilities/monitoring/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.utilities.monitoring (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.utilities.monitoring

14 |
15 |

Classes

16 | 21 |
22 | 23 | 24 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/utilities/probdist/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.utilities.probdist (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.utilities.probdist

14 |
15 |

Classes

16 | 21 |
22 | 23 | 24 | -------------------------------------------------------------------------------- /docs/javadoc/jsdp/utilities/sampling/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jsdp.utilities.sampling (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |

jsdp.utilities.sampling

14 |
15 |

Classes

16 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /docs/javadoc/overview-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Overview List (jsdp 1.0.0 API) 8 | 9 | 10 | 11 | 12 | 13 |
All Classes
14 |
15 |

Packages

16 | 40 |
41 |

 

42 | 43 | 44 | -------------------------------------------------------------------------------- /docs/javadoc/package-list: -------------------------------------------------------------------------------- 1 | jsdp.app.control.clqg.univariate 2 | jsdp.app.inventory.capital 3 | jsdp.app.inventory.multivariate 4 | jsdp.app.inventory.univariate 5 | jsdp.app.inventory.univariate.simulation 6 | jsdp.app.lotsizing 7 | jsdp.app.lotsizing.simulation 8 | jsdp.app.maintenance 9 | jsdp.app.routing.deterministic 10 | jsdp.app.routing.stochastic.fuel 11 | jsdp.app.routing.stochastic.location 12 | jsdp.app.routing.topologies 13 | jsdp.app.skeleton 14 | jsdp.app.standalone.deterministic 15 | jsdp.app.standalone.stochastic 16 | jsdp.sdp 17 | jsdp.sdp.impl.multivariate 18 | jsdp.sdp.impl.univariate 19 | jsdp.utilities.hash 20 | jsdp.utilities.monitoring 21 | jsdp.utilities.probdist 22 | jsdp.utilities.sampling 23 | -------------------------------------------------------------------------------- /docs/javadoc/script.js: -------------------------------------------------------------------------------- 1 | function show(type) 2 | { 3 | count = 0; 4 | for (var key in methods) { 5 | var row = document.getElementById(key); 6 | if ((methods[key] & type) != 0) { 7 | row.style.display = ''; 8 | row.className = (count++ % 2) ? rowColor : altColor; 9 | } 10 | else 11 | row.style.display = 'none'; 12 | } 13 | updateTabs(type); 14 | } 15 | 16 | function updateTabs(type) 17 | { 18 | for (var value in tabs) { 19 | var sNode = document.getElementById(tabs[value][0]); 20 | var spanNode = sNode.firstChild; 21 | if (value == type) { 22 | sNode.className = activeTableTab; 23 | spanNode.innerHTML = tabs[value][1]; 24 | } 25 | else { 26 | sNode.className = tableTab; 27 | spanNode.innerHTML = "" + tabs[value][1] + ""; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /docs/javascripts/scale.fix.js: -------------------------------------------------------------------------------- 1 | fixScale = function(doc) { 2 | 3 | var addEvent = 'addEventListener', 4 | type = 'gesturestart', 5 | qsa = 'querySelectorAll', 6 | scales = [1, 1], 7 | meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : []; 8 | 9 | function fix() { 10 | meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1]; 11 | doc.removeEventListener(type, fix, true); 12 | } 13 | 14 | if ((meta = meta[meta.length - 1]) && addEvent in doc) { 15 | fix(); 16 | scales = [.25, 1.6]; 17 | doc[addEvent](type, fix, true); 18 | } 19 | 20 | }; -------------------------------------------------------------------------------- /docs/jsdp/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding//src/test/resources=UTF-8 6 | encoding/=UTF-8 7 | -------------------------------------------------------------------------------- /docs/jsdp/target/classes/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Built-By: gwren 3 | Build-Jdk: 1.8.0_60 4 | Created-By: Maven Integration for Eclipse 5 | 6 | -------------------------------------------------------------------------------- /docs/jsdp/target/classes/META-INF/maven/org.jsdp/jsdp/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Sat Dec 30 18:21:53 CET 2017 3 | version=1.0.0-SNAPSHOT 4 | groupId=org.jsdp 5 | m2e.projectName=jsdp 6 | m2e.projectLocation=/Users/gwren/GitHub/jsdp/jsdp 7 | artifactId=jsdp 8 | -------------------------------------------------------------------------------- /docs/jsdp/target/classes/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /docs/jsdp/target/test-classes/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /docs/params.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jsdp", 3 | "tagline": "A Java Stochastic Dynamic Programming Library", 4 | "body": "![](https://github.com/gwr3n/jsdp/blob/master/jsdp/src/main/resources/jsdp_logo.png?raw=true)\r\n\r\n### Welcome to jsdp: a Java Stochastic Dynamic Programming Library.\r\n[Stochastic Programming](https://en.wikipedia.org/wiki/Stochastic_programming) is a framework for modeling and solving problems of decision making under uncertainty. Stochastic Dynamic Programming, originally introduced by [Richard Bellman](https://en.wikipedia.org/wiki/Richard_E._Bellman) in his seminal book [Dynamic Programming](https://www.amazon.co.uk/Dynamic-Programming-Dover-Computer-Science/dp/0486428095), is a branch of Stochastic Programming that deals with multistage decision processes and takes a \"functional equation\" approach to the discovery of optimum policies. \r\n\r\n### The Library\r\njsdp provides a general purpose Java library for modeling and solving Stochastic Dynamic Programs. The library features a number of applications in maintenance, stochastic optimal control, and stochastic lot sizing; including the computation of optimal nonstationary (s,S) policy parameters, as discussed by [Herbert Scarf](https://en.wikipedia.org/wiki/Herbert_Scarf) in his seminal work [the optimality of (s s) policies in the dynamic inventory problem](https://statistics.stanford.edu/sites/default/files/KAR%20ONR%2011.pdf).\r\n\r\nTo learn more please take a tour of our [Wiki](https://github.com/gwr3n/jsdp/wiki).\r\n\r\n### About the author\r\njsdp is maintained by [Roberto Rossi](https://sites.google.com/site/robros/Home?pli=1) (@gwr3n), Reader at the University of Edinburgh.", 5 | "note": "Don't delete this file! It's used internally to help with page regeneration." 6 | } -------------------------------------------------------------------------------- /docs/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Disallow: -------------------------------------------------------------------------------- /docs/stylesheets/github-dark.css: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2016 GitHub, Inc. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | .pl-c /* comment */ { 27 | color: #969896; 28 | } 29 | 30 | .pl-c1 /* constant, variable.other.constant, support, meta.property-name, support.constant, support.variable, meta.module-reference, markup.raw, meta.diff.header */, 31 | .pl-s .pl-v /* string variable */ { 32 | color: #0099cd; 33 | } 34 | 35 | .pl-e /* entity */, 36 | .pl-en /* entity.name */ { 37 | color: #9774cb; 38 | } 39 | 40 | .pl-smi /* variable.parameter.function, storage.modifier.package, storage.modifier.import, storage.type.java, variable.other */, 41 | .pl-s .pl-s1 /* string source */ { 42 | color: #ddd; 43 | } 44 | 45 | .pl-ent /* entity.name.tag */ { 46 | color: #7bcc72; 47 | } 48 | 49 | .pl-k /* keyword, storage, storage.type */ { 50 | color: #cc2372; 51 | } 52 | 53 | .pl-s /* string */, 54 | .pl-pds /* punctuation.definition.string, string.regexp.character-class */, 55 | .pl-s .pl-pse .pl-s1 /* string punctuation.section.embedded source */, 56 | .pl-sr /* string.regexp */, 57 | .pl-sr .pl-cce /* string.regexp constant.character.escape */, 58 | .pl-sr .pl-sre /* string.regexp source.ruby.embedded */, 59 | .pl-sr .pl-sra /* string.regexp string.regexp.arbitrary-repitition */ { 60 | color: #3c66e2; 61 | } 62 | 63 | .pl-v /* variable */ { 64 | color: #fb8764; 65 | } 66 | 67 | .pl-id /* invalid.deprecated */ { 68 | color: #e63525; 69 | } 70 | 71 | .pl-ii /* invalid.illegal */ { 72 | color: #f8f8f8; 73 | background-color: #e63525; 74 | } 75 | 76 | .pl-sr .pl-cce /* string.regexp constant.character.escape */ { 77 | font-weight: bold; 78 | color: #7bcc72; 79 | } 80 | 81 | .pl-ml /* markup.list */ { 82 | color: #c26b2b; 83 | } 84 | 85 | .pl-mh /* markup.heading */, 86 | .pl-mh .pl-en /* markup.heading entity.name */, 87 | .pl-ms /* meta.separator */ { 88 | font-weight: bold; 89 | color: #264ec5; 90 | } 91 | 92 | .pl-mq /* markup.quote */ { 93 | color: #00acac; 94 | } 95 | 96 | .pl-mi /* markup.italic */ { 97 | font-style: italic; 98 | color: #ddd; 99 | } 100 | 101 | .pl-mb /* markup.bold */ { 102 | font-weight: bold; 103 | color: #ddd; 104 | } 105 | 106 | .pl-md /* markup.deleted, meta.diff.header.from-file */ { 107 | color: #bd2c00; 108 | background-color: #ffecec; 109 | } 110 | 111 | .pl-mi1 /* markup.inserted, meta.diff.header.to-file */ { 112 | color: #55a532; 113 | background-color: #eaffea; 114 | } 115 | 116 | .pl-mdr /* meta.diff.range */ { 117 | font-weight: bold; 118 | color: #9774cb; 119 | } 120 | 121 | .pl-mo /* meta.output */ { 122 | color: #264ec5; 123 | } 124 | 125 | -------------------------------------------------------------------------------- /docs/stylesheets/github-light.css: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2016 GitHub, Inc. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | .pl-c /* comment */ { 27 | color: #969896; 28 | } 29 | 30 | .pl-c1 /* constant, variable.other.constant, support, meta.property-name, support.constant, support.variable, meta.module-reference, markup.raw, meta.diff.header */, 31 | .pl-s .pl-v /* string variable */ { 32 | color: #0086b3; 33 | } 34 | 35 | .pl-e /* entity */, 36 | .pl-en /* entity.name */ { 37 | color: #795da3; 38 | } 39 | 40 | .pl-smi /* variable.parameter.function, storage.modifier.package, storage.modifier.import, storage.type.java, variable.other */, 41 | .pl-s .pl-s1 /* string source */ { 42 | color: #333; 43 | } 44 | 45 | .pl-ent /* entity.name.tag */ { 46 | color: #63a35c; 47 | } 48 | 49 | .pl-k /* keyword, storage, storage.type */ { 50 | color: #a71d5d; 51 | } 52 | 53 | .pl-s /* string */, 54 | .pl-pds /* punctuation.definition.string, string.regexp.character-class */, 55 | .pl-s .pl-pse .pl-s1 /* string punctuation.section.embedded source */, 56 | .pl-sr /* string.regexp */, 57 | .pl-sr .pl-cce /* string.regexp constant.character.escape */, 58 | .pl-sr .pl-sre /* string.regexp source.ruby.embedded */, 59 | .pl-sr .pl-sra /* string.regexp string.regexp.arbitrary-repitition */ { 60 | color: #183691; 61 | } 62 | 63 | .pl-v /* variable */ { 64 | color: #ed6a43; 65 | } 66 | 67 | .pl-id /* invalid.deprecated */ { 68 | color: #b52a1d; 69 | } 70 | 71 | .pl-ii /* invalid.illegal */ { 72 | color: #f8f8f8; 73 | background-color: #b52a1d; 74 | } 75 | 76 | .pl-sr .pl-cce /* string.regexp constant.character.escape */ { 77 | font-weight: bold; 78 | color: #63a35c; 79 | } 80 | 81 | .pl-ml /* markup.list */ { 82 | color: #693a17; 83 | } 84 | 85 | .pl-mh /* markup.heading */, 86 | .pl-mh .pl-en /* markup.heading entity.name */, 87 | .pl-ms /* meta.separator */ { 88 | font-weight: bold; 89 | color: #1d3e81; 90 | } 91 | 92 | .pl-mq /* markup.quote */ { 93 | color: #008080; 94 | } 95 | 96 | .pl-mi /* markup.italic */ { 97 | font-style: italic; 98 | color: #333; 99 | } 100 | 101 | .pl-mb /* markup.bold */ { 102 | font-weight: bold; 103 | color: #333; 104 | } 105 | 106 | .pl-md /* markup.deleted, meta.diff.header.from-file */ { 107 | color: #bd2c00; 108 | background-color: #ffecec; 109 | } 110 | 111 | .pl-mi1 /* markup.inserted, meta.diff.header.to-file */ { 112 | color: #55a532; 113 | background-color: #eaffea; 114 | } 115 | 116 | .pl-mdr /* meta.diff.range */ { 117 | font-weight: bold; 118 | color: #795da3; 119 | } 120 | 121 | .pl-mo /* meta.output */ { 122 | color: #1d3e81; 123 | } 124 | 125 | -------------------------------------------------------------------------------- /jsdp/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /jsdp/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /bin/ 3 | 4 | .DS_Store 5 | .settings/ 6 | -------------------------------------------------------------------------------- /jsdp/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | jsdp 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /jsdp/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.8 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 12 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 13 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 14 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning 15 | org.eclipse.jdt.core.compiler.release=disabled 16 | org.eclipse.jdt.core.compiler.source=1.8 17 | -------------------------------------------------------------------------------- /jsdp/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/capital/CF_Action.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.inventory.capital; 2 | 3 | import java.util.stream.IntStream; 4 | 5 | import jsdp.sdp.Action; 6 | import jsdp.sdp.State; 7 | 8 | public class CF_Action extends Action{ 9 | 10 | private static final long serialVersionUID = 1L; 11 | 12 | private static int maxOrderQuantity; 13 | 14 | public static void setMaxOrderQuantity(int maxOrderQuantity){ 15 | CF_Action.maxOrderQuantity = maxOrderQuantity; 16 | } 17 | 18 | int orderQuantity; 19 | 20 | public CF_Action(State state, 21 | int orderQuantity){ 22 | super(state); 23 | this.orderQuantity = orderQuantity; 24 | } 25 | 26 | public int getOrderQuantity(){ 27 | return this.orderQuantity; 28 | } 29 | 30 | @Override 31 | public boolean equals(Object action) { 32 | if(action instanceof CF_Action) 33 | return this.orderQuantity == ((CF_Action) action).orderQuantity; 34 | else 35 | return false; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | String hash = "A"; 41 | hash = (hash + state.getPeriod()) + "_" + 42 | this.orderQuantity; 43 | return hash.hashCode(); 44 | } 45 | 46 | @Override 47 | public String toString(){ 48 | String out = ""; 49 | out += "Period: " + state.getPeriod() + "\t"; 50 | out += "Order quantity: " + this.orderQuantity; 51 | return out; 52 | } 53 | 54 | public static IntStream generateOrderQuantities(CF_State state){ 55 | return IntStream.iterate(0, i -> i + 1).limit(CF_Action.maxOrderQuantity + 1); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/capital/CF_State.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.inventory.capital; 2 | 3 | import jsdp.sdp.State; 4 | 5 | public class CF_State extends State { 6 | 7 | private static final long serialVersionUID = 1L; 8 | 9 | private int inventory; 10 | private int capital; 11 | 12 | public CF_State(CF_StateDescriptor descriptor) { 13 | super(descriptor.getPeriod()); 14 | this.inventory = descriptor.getInventory(); 15 | this.capital = descriptor.getCapital(); 16 | } 17 | 18 | public int getInventory(){ 19 | return this.inventory; 20 | } 21 | 22 | public int getCapital(){ 23 | return this.capital; 24 | } 25 | 26 | @Override 27 | public boolean equals(Object state) { 28 | if(state instanceof CF_State) 29 | return this.period == ((CF_State) state).period && 30 | this.inventory == ((CF_State) state).inventory && 31 | this.capital == ((CF_State) state).capital; 32 | else 33 | return false; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | String hash = "S"; 39 | hash = (hash + period) + "_I" + 40 | this.inventory + "_C" + 41 | this.capital; 42 | return hash.hashCode(); 43 | } 44 | 45 | @Override 46 | public String toString(){ 47 | String out = ""; 48 | out = "Period: " + period + "\t" + 49 | "Inventory: " + this.inventory + "\t" + 50 | "Capital: " + this.capital; 51 | return out; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/capital/CF_StateDescriptor.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.inventory.capital; 2 | 3 | import jsdp.sdp.State; 4 | 5 | public class CF_StateDescriptor extends State{ 6 | 7 | private static final long serialVersionUID = 1L; 8 | 9 | private int inventory; 10 | private int capital; 11 | 12 | public CF_StateDescriptor(int period, 13 | int inventory, 14 | int capital) { 15 | super(period); 16 | this.inventory = inventory; 17 | this.capital = capital; 18 | } 19 | 20 | @Override 21 | public boolean equals(Object state) { 22 | if(state instanceof CF_StateDescriptor){ 23 | return this.period == ((CF_StateDescriptor) state).period && 24 | this.inventory == ((CF_StateDescriptor) state).inventory && 25 | this.capital == ((CF_StateDescriptor) state).capital; 26 | }else 27 | return false; 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | String hash = "SD"; 33 | hash = (hash + period) + "_I" + 34 | this.inventory + "C_" + 35 | this.capital; 36 | return hash.hashCode(); 37 | } 38 | 39 | @Override 40 | public String toString(){ 41 | String out = ""; 42 | out = "Period: " + period + "\t" + 43 | "Inventory: " + this.inventory + "\t" + 44 | "Capital: " + this.capital; 45 | return out; 46 | } 47 | 48 | public int getCapital(){ 49 | return this.capital; 50 | } 51 | 52 | public int getInventory(){ 53 | return this.inventory; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/capital/CF_StateSpace.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.inventory.capital; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.function.Function; 6 | 7 | import jsdp.sdp.Action; 8 | import jsdp.sdp.HashType; 9 | import jsdp.sdp.State; 10 | import jsdp.sdp.StateSpace; 11 | 12 | public class CF_StateSpace extends StateSpace { 13 | 14 | public CF_StateSpace(int period, 15 | Function> buildActionList, 16 | HashType hashType, 17 | int stateSpaceSizeLowerBound, 18 | float loadFactor){ 19 | super(period, hashType, stateSpaceSizeLowerBound, loadFactor); 20 | CF_StateSpace.buildActionList = buildActionList; 21 | } 22 | 23 | public CF_StateSpace(int period, 24 | Function> buildActionList, 25 | HashType hashType){ 26 | super(period, hashType); 27 | CF_StateSpace.buildActionList = buildActionList; 28 | } 29 | 30 | public boolean exists(CF_StateDescriptor descriptor){ 31 | return states.get(descriptor) != null; 32 | } 33 | 34 | public State getState(CF_StateDescriptor descriptor){ 35 | State value = states.get(descriptor); 36 | if(value == null){ 37 | State state = new CF_State(descriptor); 38 | this.states.put(descriptor, state); 39 | return state; 40 | }else 41 | return (CF_State) value; 42 | } 43 | 44 | public Iterator iterator() { 45 | throw new NullPointerException("Method not implemented"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/capital/CF_TransitionProbability.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.inventory.capital; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Random; 6 | import java.util.stream.IntStream; 7 | 8 | import jsdp.sdp.Action; 9 | import jsdp.sdp.State; 10 | import jsdp.sdp.TransitionProbability; 11 | import jsdp.sdp.impl.univariate.SamplingScheme; 12 | import umontreal.ssj.probdist.Distribution; 13 | 14 | public class CF_TransitionProbability extends TransitionProbability { 15 | 16 | Distribution[] distributions; 17 | double[] supportLB; 18 | double[] supportUB; 19 | double truncationQuantile = 0.99; 20 | 21 | SamplingScheme samplingScheme; 22 | int sampleSize; 23 | double reductionFactorPerStage; 24 | 25 | CF_StateSpace[] stateSpace; 26 | ImmediateValueFunction immediateValueFunction; 27 | 28 | public CF_TransitionProbability(Distribution[] distributions, 29 | ImmediateValueFunction immediateValueFunction, 30 | CF_StateSpace[] stateSpace, 31 | SamplingScheme samplingScheme, 32 | int sampleSize, 33 | double reductionFactorPerStage){ 34 | this.distributions = distributions; 35 | this.supportLB = IntStream.iterate(0, i -> i + 1) 36 | .limit(distributions.length) 37 | .mapToDouble(i -> distributions[i].inverseF(1-truncationQuantile)) 38 | .toArray(); 39 | this.supportUB = IntStream.iterate(0, i -> i + 1) 40 | .limit(distributions.length) 41 | .mapToDouble(i -> distributions[i].inverseF(truncationQuantile)) 42 | .toArray(); 43 | this.immediateValueFunction = immediateValueFunction; 44 | this.stateSpace = stateSpace; 45 | this.samplingScheme = samplingScheme; 46 | this.sampleSize = sampleSize; 47 | this.reductionFactorPerStage = reductionFactorPerStage; 48 | } 49 | 50 | @Override 51 | public double getTransitionProbability(State initialState, Action action, State finalState) { 52 | int demand = ((CF_State) initialState).getInventory() + ((CF_Action) action).orderQuantity - ((CF_State) finalState).getInventory(); 53 | double probability = this.distributions[initialState.getPeriod()].cdf(demand)-this.distributions[initialState.getPeriod()].cdf(demand-1); 54 | return probability; 55 | } 56 | 57 | @Override 58 | public ArrayList generateFinalStates(State initialState, Action action) { 59 | ArrayList finalStates = new ArrayList(); 60 | for(int demand = 0; demand < this.supportUB[initialState.getPeriod()]; demand++){ 61 | CF_StateDescriptor descriptor = new CF_StateDescriptor(initialState.getPeriod() + 1, 62 | ((CF_State) initialState).getInventory() + ((CF_Action) action).orderQuantity - demand, 63 | ((CF_State) initialState).getCapital() + (int) Math.round(this.immediateValueFunction.apply(initialState, action, Integer.valueOf(demand)))); 64 | finalStates.add(this.stateSpace[initialState.getPeriod() + 1].getState(descriptor)); 65 | } 66 | 67 | if(this.samplingScheme == SamplingScheme.NONE) 68 | return finalStates; 69 | else if(this.samplingScheme == SamplingScheme.SIMPLE_RANDOM_SAMPLING){ 70 | Random rnd = new Random(12345); 71 | Collections.shuffle(finalStates, rnd); 72 | //int reductionFactor = reductionFactorPerStage*initialState.getPeriod() == 0 ? 1 : reductionFactorPerStage*initialState.getPeriod(); 73 | int reductionFactor = (int) Math.pow(reductionFactorPerStage, initialState.getPeriod()); 74 | return new ArrayList(finalStates.subList(0, this.sampleSize/reductionFactor < 1 ? 1 : Math.min(this.sampleSize/reductionFactor, finalStates.size()))); 75 | }else{ 76 | throw new NullPointerException("Method not implemented"); 77 | } 78 | } 79 | 80 | @Override 81 | public ArrayList getFinalStates(State initialState, Action action) { 82 | throw new NullPointerException("Method not implemented"); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/capital/ImmediateValueFunction.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.inventory.capital; 2 | 3 | /** 4 | * A functional interface that captures immediate value of a transition from an initial state to 5 | * a final state under a chosen action. 6 | * 7 | * @author Roberto Rossi 8 | * 9 | * @param the generic type for a state 10 | * @param the generic type for an action 11 | * @param the generic type for a random outcome 12 | * @param the generic type of the value returned 13 | */ 14 | 15 | @FunctionalInterface 16 | public interface ImmediateValueFunction { 17 | /** 18 | * The immediate value function 19 | * 20 | * @param initialState the initial state of the stochastic process. 21 | * @param action the chosen action. 22 | * @param randomOutcome the final state of the stochastic process. 23 | * @return the immediate value of a transition from {@code initialState} to {@code finalState} under a chosen {@code action}. 24 | */ 25 | public D apply (S initialState, A action, I randomOutcome); 26 | } 27 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/multivariate/ActionIteratorImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.inventory.multivariate; 28 | 29 | import jsdp.sdp.Action; 30 | import jsdp.sdp.ActionIterator; 31 | import jsdp.sdp.impl.multivariate.ActionImpl; 32 | import jsdp.sdp.impl.multivariate.StateImpl; 33 | 34 | /** 35 | * A concrete implementation of {@code StateSpaceIterator}. 36 | * 37 | * @author Roberto Rossi 38 | * 39 | */ 40 | public class ActionIteratorImpl extends ActionIterator{ 41 | 42 | StateImpl state; 43 | ActionImpl currentAction; 44 | int actionPointer; 45 | 46 | public ActionIteratorImpl(StateImpl state){ 47 | this.state = state; 48 | actionPointer = getActionSpaceCardinality(this.state); 49 | currentAction = new ActionImpl(this.state, getIntAction(actionPointer, this.state)); 50 | } 51 | 52 | private static int getActionSpaceCardinality(StateImpl state){ 53 | int cardinality = 1; 54 | for(int i = 0; i < StateImpl.getStateDimension(); i++){ 55 | cardinality *= StateImpl.getMaxIntState()[i] - state.getInitialIntState()[i] + 1; 56 | } 57 | return cardinality; 58 | } 59 | 60 | private static int getResidualStateSpaceCardinality(int j, StateImpl state){ 61 | int cardinality = 1; 62 | for(int i = j; i < StateImpl.getStateDimension(); i++){ 63 | cardinality *= StateImpl.getMaxIntState()[i] - state.getInitialIntState()[i] + 1; 64 | } 65 | return cardinality; 66 | } 67 | 68 | private static int[] getIntAction(int actionPointer, StateImpl state){ 69 | int[] intState = new int[StateImpl.getStateDimension()]; 70 | for(int i = 0; i < StateImpl.getStateDimension(); i++){ 71 | intState[i] = Math.floorDiv(Math.floorMod(actionPointer, getResidualStateSpaceCardinality(i, state)), i < StateImpl.getStateDimension() - 1 ? getResidualStateSpaceCardinality(i+1, state) : 1); 72 | } 73 | return intState; 74 | } 75 | 76 | public boolean hasNext() { 77 | if(actionPointer > 0) 78 | return true; 79 | else 80 | return false; 81 | } 82 | 83 | public Action next() { 84 | if(actionPointer > 0){ 85 | Action action = currentAction; 86 | actionPointer -= 1; 87 | if(actionPointer > 0 ) 88 | currentAction = new ActionImpl(this.state, getIntAction(actionPointer, this.state)); 89 | else 90 | currentAction = null; 91 | return action; 92 | }else{ 93 | return null; 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/univariate/simulation/sS_Policy.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.inventory.univariate.simulation; 2 | 3 | import jsdp.sdp.Action; 4 | import jsdp.sdp.State; 5 | import jsdp.sdp.impl.univariate.ActionImpl; 6 | import jsdp.sdp.impl.univariate.BackwardRecursionImpl; 7 | import jsdp.sdp.impl.univariate.StateDescriptorImpl; 8 | import jsdp.sdp.impl.univariate.StateImpl; 9 | import jsdp.sdp.impl.univariate.StateSpaceImpl; 10 | 11 | /** 12 | * This class extracts an (s,S) policy from a {@code BackwardRecursionImpl} object 13 | * 14 | * @author Roberto Rossi 15 | * 16 | */ 17 | public class sS_Policy { 18 | 19 | BackwardRecursionImpl recursion; 20 | int horizonLength; 21 | 22 | public sS_Policy(BackwardRecursionImpl recursion, int horizonLength){ 23 | this.recursion = recursion; 24 | this.horizonLength = horizonLength; 25 | } 26 | 27 | /** 28 | * Extracts the optimal policy for a given initial inventory level 29 | * 30 | * @param initialInventory the initial inventory level 31 | * @return the optimal policy array 32 | */ 33 | public double[][] getOptimalPolicy(double initialInventory){ 34 | double[][] optimalPolicy = new double[2][]; 35 | double[] S = new double[this.horizonLength]; 36 | double[] s = new double[this.horizonLength]; 37 | for(int i = 0; i < this.horizonLength; i++){ 38 | if(i == 0) { 39 | StateDescriptorImpl stateDescriptor = new StateDescriptorImpl(0, initialInventory); 40 | s[i] = ((StateImpl) this.find_s(i)).getInitialState(); 41 | S[i] = recursion.getOptimalAction(stateDescriptor).getAction()+initialInventory; 42 | } 43 | else{ 44 | s[i] = ((StateImpl) this.find_s(i)).getInitialState(); 45 | S[i] = ((StateImpl) this.find_S(i)).getInitialState(); 46 | } 47 | } 48 | optimalPolicy[0] = s; 49 | optimalPolicy[1] = S; 50 | return optimalPolicy; 51 | } 52 | 53 | private State find_S(int period){ 54 | StateImpl s = (StateImpl) this.find_s(period); 55 | double i = ((ActionImpl)recursion.getValueRepository().getOptimalAction(s)).getAction()+s.getInitialState(); 56 | StateDescriptorImpl stateDescriptor = new StateDescriptorImpl(period, i); 57 | return ((StateSpaceImpl)recursion.getStateSpace()[period]).getState(stateDescriptor); 58 | } 59 | 60 | private State find_s(int period){ 61 | for(double i = StateImpl.getMaxState(); i >= StateImpl.getMinState(); i -= StateImpl.getStepSize()){ 62 | StateDescriptorImpl stateDescriptor = new StateDescriptorImpl(period, i); 63 | Action action = recursion.getOptimalAction(stateDescriptor); 64 | if(action == null) 65 | continue; 66 | if(((ActionImpl)action).getAction() > 0){ 67 | return ((StateSpaceImpl)recursion.getStateSpace(period)).getState(stateDescriptor); 68 | } 69 | } 70 | StateDescriptorImpl stateDescriptor = new StateDescriptorImpl(period, StateImpl.getMinState()); 71 | return ((StateSpaceImpl)recursion.getStateSpace(period)).getState(stateDescriptor); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/inventory/univariate/simulation/skSk_Policy.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.inventory.univariate.simulation; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | import jsdp.sdp.Action; 7 | import jsdp.sdp.State; 8 | import jsdp.sdp.impl.univariate.ActionImpl; 9 | import jsdp.sdp.impl.univariate.BackwardRecursionImpl; 10 | import jsdp.sdp.impl.univariate.StateDescriptorImpl; 11 | import jsdp.sdp.impl.univariate.StateImpl; 12 | import jsdp.sdp.impl.univariate.StateSpaceImpl; 13 | 14 | /** 15 | * This class extracts an (sk,Sk) policy from a {@code BackwardRecursionImpl} object 16 | * 17 | * @author Roberto Rossi 18 | * 19 | */ 20 | public class skSk_Policy { 21 | 22 | BackwardRecursionImpl recursion; 23 | int horizonLength; 24 | 25 | public skSk_Policy(BackwardRecursionImpl recursion, int horizonLength){ 26 | this.recursion = recursion; 27 | this.horizonLength = horizonLength; 28 | } 29 | 30 | /** 31 | * Extracts the optimal policy for a given initial inventory level 32 | * 33 | * @param initialInventory the initial inventory level 34 | * @return the optimal policy array 35 | */ 36 | public double[][][] getOptimalPolicy(double initialInventory, int thresholdNumberLimit, double maxOrderQuantity){ 37 | double[][][] optimalPolicy = new double[2][][]; 38 | double[][] S = new double[this.horizonLength][]; 39 | double[][] s = new double[this.horizonLength][]; 40 | for(int i = 0; i < this.horizonLength; i++){ 41 | State[][] policy = find_skSk(i); 42 | s[i] = Arrays.stream(policy[0], Math.max(0, policy[0].length-thresholdNumberLimit), policy[0].length).mapToDouble(a -> ((StateImpl)a).getInitialState()).toArray(); 43 | S[i] = Arrays.stream(policy[1], Math.max(0, policy[1].length-thresholdNumberLimit), policy[1].length).mapToDouble(a -> ((StateImpl)a).getInitialState()).toArray(); 44 | if(s[i][s[i].length-1]-s[i][0] > maxOrderQuantity) 45 | System.err.println("Period "+(i+1)+". Gap between s thresholds greater than capacity: "+(s[i][s[i].length-1]-s[i][0])); 46 | } 47 | optimalPolicy[0] = s; 48 | optimalPolicy[1] = S; 49 | return optimalPolicy; 50 | } 51 | 52 | private State[][] find_skSk(int period){ 53 | ArrayList s = new ArrayList(); 54 | ArrayList S = new ArrayList(); 55 | double nextAction = 0; 56 | boolean startedOrdering = false; 57 | for(double i = StateImpl.getMaxState(); i >= StateImpl.getMinState(); i -= StateImpl.getStepSize()){ 58 | StateDescriptorImpl stateDescriptor = new StateDescriptorImpl(period, i); 59 | Action action = recursion.getOptimalAction(stateDescriptor); 60 | if(action == null) 61 | continue; 62 | else if( 63 | (((ActionImpl)action).getAction() > 0 && nextAction == 0) || 64 | ((ActionImpl)action).getAction() > 0 && nextAction > ((ActionImpl)action).getAction() 65 | ){ 66 | if(!startedOrdering) 67 | startedOrdering = true; 68 | State reorder = ((StateSpaceImpl)recursion.getStateSpace(period)).getState(stateDescriptor); 69 | s.add(0,reorder); 70 | double inv = ((ActionImpl)recursion.getValueRepository().getOptimalAction(reorder)).getAction()+((StateImpl)reorder).getInitialState(); 71 | StateDescriptorImpl descriptor = new StateDescriptorImpl(period, inv); 72 | S.add(0, ((StateSpaceImpl)recursion.getStateSpace()[period]).getState(descriptor)); 73 | }else if(startedOrdering && ((ActionImpl)action).getAction() == 0) { 74 | //System.err.println("Zero order after started ordering. Period: "+period+". State: "+i+". Next action: "+nextAction); 75 | } 76 | nextAction = ((ActionImpl)action).getAction(); 77 | } 78 | if(s.size() == 0 || S.size() == 0) { 79 | System.err.println("State space boundaries probably too narrow."); 80 | StateDescriptorImpl stateDescriptor = new StateDescriptorImpl(period, StateImpl.getMinState()); 81 | State[][] policy = new State[2][]; 82 | policy[0] = new State[] {((StateSpaceImpl)recursion.getStateSpace(period)).getState(stateDescriptor)}; 83 | policy[1] = new State[] {((StateSpaceImpl)recursion.getStateSpace(period)).getState(stateDescriptor)}; 84 | return policy; 85 | }else { 86 | State[][] policy = new State[2][]; 87 | policy[0] = s.stream().toArray(State[]::new); 88 | policy[1] = S.stream().toArray(State[]::new); 89 | return policy; 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/lotsizing/sS_Action.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.lotsizing; 28 | 29 | import jsdp.sdp.Action; 30 | import jsdp.sdp.State; 31 | 32 | public class sS_Action extends Action { 33 | 34 | private static final long serialVersionUID = 1L; 35 | 36 | int intAction; 37 | 38 | public static double actionToOrderQuantity(int action){ 39 | return action*sS_State.getStepSize(); 40 | } 41 | 42 | public static int orderQuantityToAction(double orderQty){ 43 | return (int) Math.round(orderQty/sS_State.getStepSize()); 44 | } 45 | 46 | public sS_Action(State state, int intAction){ 47 | super(state); 48 | this.intAction = intAction; 49 | } 50 | 51 | public int getIntAction(){ 52 | return this.intAction; 53 | } 54 | 55 | @Override 56 | public boolean equals(Object action){ 57 | if(action instanceof sS_Action) 58 | return this.intAction == ((sS_Action)action).intAction; 59 | else 60 | return false; 61 | } 62 | 63 | @Override 64 | public int hashCode(){ 65 | String hash = ""; 66 | hash = (hash + intAction); 67 | return hash.hashCode(); 68 | } 69 | 70 | @Override 71 | public String toString(){ 72 | return state+"\tAction: "+ actionToOrderQuantity(this.intAction); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/lotsizing/sS_CostRepository.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.lotsizing; 28 | 29 | import jsdp.sdp.HashType; 30 | import jsdp.sdp.ValueRepository; 31 | 32 | public class sS_CostRepository extends ValueRepository { 33 | public sS_CostRepository(double fixedOrderingCost, double proportionalOrderingCost, double holdingCost, double penaltyCost){ 34 | super( (initialState, action, finalState) -> { 35 | sS_Action a = (sS_Action)action; 36 | sS_State fs = (sS_State)finalState; 37 | double totalCost = a.getIntAction() > 0 ? (fixedOrderingCost + sS_Action.actionToOrderQuantity(a.getIntAction())*proportionalOrderingCost) : 0; 38 | totalCost += Math.max(sS_State.stateToInventory(fs.getInitialIntState()),0)*holdingCost+ 39 | Math.max(-sS_State.stateToInventory(fs.getInitialIntState()),0)*penaltyCost; 40 | return totalCost; 41 | }, 1.0, HashType.HASHTABLE); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/lotsizing/sS_StateDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.lotsizing; 28 | 29 | import jsdp.sdp.StateDescriptor; 30 | 31 | public class sS_StateDescriptor extends StateDescriptor{ 32 | 33 | private static final long serialVersionUID = 1L; 34 | 35 | int initialIntState; 36 | 37 | public sS_StateDescriptor(int period, int initialIntState){ 38 | super(period); 39 | this.initialIntState = initialIntState; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object descriptor){ 44 | if(descriptor instanceof sS_StateDescriptor) 45 | return this.period == ((sS_StateDescriptor)descriptor).period && 46 | this.initialIntState == ((sS_StateDescriptor)descriptor).initialIntState; 47 | else 48 | return false; 49 | } 50 | 51 | @Override 52 | public int hashCode(){ 53 | String hash = ""; 54 | hash = (hash + period) + "_" + initialIntState; 55 | return hash.hashCode(); 56 | } 57 | 58 | public int getInitialIntState(){ 59 | return initialIntState; 60 | } 61 | } -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/lotsizing/sS_StateSpace.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.lotsizing; 28 | 29 | import java.util.Iterator; 30 | 31 | import jsdp.sdp.HashType; 32 | import jsdp.sdp.State; 33 | import jsdp.sdp.StateSpace; 34 | 35 | public class sS_StateSpace extends StateSpace{ 36 | 37 | sS_StateSpaceSampleIterator.SamplingScheme samplingScheme = sS_StateSpaceSampleIterator.SamplingScheme.NONE; 38 | int maxSampleSize = Integer.MAX_VALUE; 39 | 40 | public sS_StateSpace(int period){ 41 | super(period, HashType.HASHTABLE); 42 | } 43 | 44 | public sS_StateSpace(int period, 45 | sS_StateSpaceSampleIterator.SamplingScheme samplingScheme, 46 | int maxSampleSize){ 47 | super(period, HashType.HASHTABLE); 48 | this.setSamplingScheme(samplingScheme, maxSampleSize); 49 | } 50 | 51 | public void setSamplingScheme(sS_StateSpaceSampleIterator.SamplingScheme samplingScheme, int maxSampleSize){ 52 | switch(samplingScheme){ 53 | case NONE: 54 | this.samplingScheme = sS_StateSpaceSampleIterator.SamplingScheme.NONE; 55 | this.maxSampleSize = Integer.MAX_VALUE; 56 | break; 57 | default: 58 | this.samplingScheme = samplingScheme; 59 | this.maxSampleSize = maxSampleSize; 60 | } 61 | } 62 | 63 | public boolean exists (sS_StateDescriptor descriptor){ 64 | return states.get(descriptor) != null; 65 | } 66 | 67 | public State getState(sS_StateDescriptor descriptor){ 68 | State value = states.get(descriptor); 69 | if(value == null){ 70 | State state = new sS_State(descriptor); 71 | this.states.put(descriptor, state); 72 | return state; 73 | }else 74 | return (sS_State) value; 75 | } 76 | 77 | public sS_StateSpaceSampleIterator.SamplingScheme getSamplingScheme(){ 78 | return this.samplingScheme; 79 | } 80 | 81 | public Iterator iterator() { 82 | if(period == 0 || this.samplingScheme == sS_StateSpaceSampleIterator.SamplingScheme.NONE) 83 | return new sS_StateSpaceIterator(this); 84 | else 85 | return new sS_StateSpaceSampleIterator(this, this.maxSampleSize); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/lotsizing/sS_StateSpaceIterator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.lotsizing; 28 | 29 | import jsdp.sdp.State; 30 | import jsdp.sdp.StateSpaceIterator; 31 | 32 | public class sS_StateSpaceIterator extends StateSpaceIterator { 33 | 34 | sS_StateSpace stateSpace; 35 | sS_StateDescriptor currentStateDescriptor; 36 | 37 | public sS_StateSpaceIterator(sS_StateSpace stateSpace){ 38 | this.stateSpace = stateSpace; 39 | currentStateDescriptor = new sS_StateDescriptor(this.stateSpace.getPeriod(), sS_State.getMaxIntState()); 40 | } 41 | 42 | public boolean hasNext() { 43 | if(currentStateDescriptor.getInitialIntState() <= sS_State.getMaxIntState() && 44 | currentStateDescriptor.getInitialIntState() >= sS_State.getMinIntState()) 45 | return true; 46 | else 47 | return false; 48 | } 49 | 50 | public State next() { 51 | if(currentStateDescriptor.getInitialIntState() <= sS_State.getMaxIntState() && 52 | currentStateDescriptor.getInitialIntState() >= sS_State.getMinIntState()){ 53 | State state = stateSpace.getState(currentStateDescriptor); 54 | currentStateDescriptor = new sS_StateDescriptor(currentStateDescriptor.getPeriod(), 55 | currentStateDescriptor.getInitialIntState() - 1); 56 | return state; 57 | }else{ 58 | return null; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/deterministic/BR_StateDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.routing.deterministic; 28 | 29 | import java.util.Arrays; 30 | 31 | import jsdp.sdp.StateDescriptor; 32 | 33 | public class BR_StateDescriptor extends StateDescriptor { 34 | 35 | private static final long serialVersionUID = 1L; 36 | 37 | private int bowserTankLevel; 38 | private int bowserLocation; 39 | 40 | private int machineTankLevel[]; 41 | private int machineLocation[]; 42 | 43 | public BR_StateDescriptor(int period, 44 | int bowserTankLevel, 45 | int bowserLocation, 46 | int machineTankLevel[], 47 | int machineLocation[]){ 48 | super(period); 49 | this.bowserTankLevel = bowserTankLevel; 50 | this.bowserLocation = bowserLocation; 51 | this.machineTankLevel = Arrays.copyOf(machineTankLevel, machineTankLevel.length); 52 | this.machineLocation = Arrays.copyOf(machineLocation, machineLocation.length); 53 | } 54 | 55 | public int getBowserTankLevel(){ 56 | return this.bowserTankLevel; 57 | } 58 | 59 | public int getBowserLocation(){ 60 | return this.bowserLocation; 61 | } 62 | 63 | public int[] getMachineTankLevel(){ 64 | return this.machineTankLevel; 65 | } 66 | 67 | public int[] getMachineLocation(){ 68 | return this.machineLocation; 69 | } 70 | 71 | @Override 72 | public boolean equals(Object state) { 73 | if(state instanceof BR_StateDescriptor) 74 | return this.period == ((BR_StateDescriptor)state).period && 75 | this.bowserTankLevel == ((BR_StateDescriptor)state).bowserTankLevel && 76 | this.bowserLocation == ((BR_StateDescriptor)state).bowserLocation && 77 | Arrays.equals(this.machineTankLevel, ((BR_StateDescriptor)state).machineTankLevel) && 78 | Arrays.equals(this.machineLocation, ((BR_StateDescriptor)state).machineLocation); 79 | else 80 | return false; 81 | } 82 | 83 | @Override 84 | public int hashCode() { 85 | String hash = "SD"; 86 | hash = (hash + period) + "_" + 87 | this.bowserTankLevel + "_" + 88 | this.bowserLocation + 89 | Arrays.toString(this.machineTankLevel); 90 | Arrays.toString(this.machineLocation); 91 | return hash.hashCode(); 92 | } 93 | 94 | @Override 95 | public String toString(){ 96 | String out = ""; 97 | out = "Period: " + period + "\t" + 98 | "Bowser fuel: " + this.bowserTankLevel + "\t" + 99 | "Bowser location: " + this.bowserLocation + "\t" + 100 | "Machine tank: " +Arrays.toString(this.machineTankLevel) + "\t" + 101 | "Machine location: " + Arrays.toString(this.machineLocation); 102 | return out; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/deterministic/BR_StateSpace.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.routing.deterministic; 28 | 29 | import java.util.ArrayList; 30 | import java.util.Iterator; 31 | import java.util.function.Function; 32 | 33 | import jsdp.sdp.Action; 34 | import jsdp.sdp.HashType; 35 | import jsdp.sdp.State; 36 | import jsdp.sdp.StateSpace; 37 | 38 | public class BR_StateSpace extends StateSpace { 39 | 40 | public BR_StateSpace(int period, 41 | Function> buildActionList, 42 | HashType hashType, 43 | int stateSpaceSizeLowerBound, 44 | float loadFactor){ 45 | super(period, hashType, stateSpaceSizeLowerBound, loadFactor); 46 | BR_StateSpace.buildActionList = buildActionList; 47 | } 48 | 49 | public BR_StateSpace(int period, 50 | Function> buildActionList, 51 | HashType hashType){ 52 | super(period, hashType); 53 | BR_StateSpace.buildActionList = buildActionList; 54 | } 55 | 56 | public boolean exists (BR_StateDescriptor descriptor){ 57 | return states.get(descriptor) != null; 58 | } 59 | 60 | public State getState(BR_StateDescriptor descriptor){ 61 | State value = states.get(descriptor); 62 | if(value == null){ 63 | State state = new BR_State(descriptor); 64 | this.states.put(descriptor, state); 65 | return state; 66 | }else 67 | return (BR_State) value; 68 | } 69 | 70 | public Iterator iterator() { 71 | throw new NullPointerException("Method not implemented"); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/deterministic/BR_TransitionProbability.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.routing.deterministic; 28 | 29 | import java.util.ArrayList; 30 | import java.util.Arrays; 31 | 32 | import jsdp.sdp.Action; 33 | import jsdp.sdp.State; 34 | import jsdp.sdp.TransitionProbability; 35 | 36 | public class BR_TransitionProbability extends TransitionProbability { 37 | 38 | private double[][][] machineLocation; 39 | private int[][] fuelConsumption; 40 | BR_StateSpace[] stateSpace; 41 | 42 | public BR_TransitionProbability(double[][][] machineLocation, int[][] fuelConsumption, BR_StateSpace[] stateSpace){ 43 | this.machineLocation = machineLocation; 44 | this.fuelConsumption = fuelConsumption; 45 | this.stateSpace = stateSpace; 46 | } 47 | 48 | @Override 49 | public double getTransitionProbability(State initialState, Action action, State finalState) { 50 | double probability = 1; 51 | return probability; 52 | } 53 | 54 | @Override 55 | public ArrayList generateFinalStates(State initialState, Action action) { 56 | int bowserTankLevel = ((BR_State) initialState).getBowserTankLevel() + 57 | ((BR_Action) action).getBowserRefuelQty() - 58 | Arrays.stream(((BR_Action)action).getMachineRefuelQty()).sum(); 59 | int bowserLocation = ((BR_Action) action).getBowserNewLocation(); 60 | 61 | int machineTankLevel[] = Arrays.stream(((BR_State) initialState).getMachineTankLevel()).map(i -> Math.max(i, 0)).toArray(); 62 | for(int i = 0; i < machineTankLevel.length; i++){ 63 | machineTankLevel[i] += ((BR_Action) action).getMachineRefuelQty()[i] - this.fuelConsumption[i][initialState.getPeriod()]; 64 | } 65 | 66 | int[] machineLocations = new int[((BR_State) initialState).getMachineLocation().length]; 67 | double[][] locationProbabilityMatrix = this.machineLocation[Math.min(initialState.getPeriod()+1, this.machineLocation.length-1)]; 68 | for(int machine = 0; machine < machineLocations.length; machine++){ 69 | for(int i = 0; i < locationProbabilityMatrix[machine].length; i++){ 70 | if(locationProbabilityMatrix[machine][i] == 1){ 71 | machineLocations[machine] = i; 72 | } 73 | } 74 | } 75 | 76 | BR_StateDescriptor descriptor = new BR_StateDescriptor(initialState.getPeriod() + 1, 77 | bowserTankLevel, 78 | bowserLocation, 79 | machineTankLevel, 80 | machineLocations); 81 | 82 | ArrayList finalStates = new ArrayList(); 83 | finalStates.add(this.stateSpace[initialState.getPeriod() + 1].getState(descriptor)); 84 | 85 | return finalStates; 86 | } 87 | 88 | @Override 89 | public ArrayList getFinalStates(State initialState, Action action) { 90 | throw new NullPointerException("Method not implemented"); 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/deterministic/BowserRoutingBatch.java: -------------------------------------------------------------------------------- 1 | package jsdp.app.routing.deterministic; 2 | 3 | import java.io.File; 4 | import java.io.FileNotFoundException; 5 | import java.io.FileOutputStream; 6 | import java.io.IOException; 7 | import java.io.OutputStreamWriter; 8 | 9 | import jsdp.app.routing.topologies.Location; 10 | import jsdp.app.routing.topologies.Topology; 11 | 12 | public class BowserRoutingBatch { 13 | static int T, M, N; 14 | static int bowserInitialTankLevel; 15 | static int maxBowserTankLevel; 16 | static int minRefuelingQty; 17 | static int[] tankCapacity; 18 | static int[] initialTankLevel; 19 | static int[][] fuelConsumption; 20 | static int[][] connectivity; 21 | static double[][] distance; 22 | static double[][][] machineLocation; 23 | static int fuelStockOutPenaltyCost; 24 | 25 | public static void writeToFile(String fileName, String str){ 26 | File results = new File(fileName); 27 | try { 28 | FileOutputStream fos = new FileOutputStream(results, true); 29 | OutputStreamWriter osw = new OutputStreamWriter(fos); 30 | osw.write(str+"\n"); 31 | osw.close(); 32 | } catch (FileNotFoundException e) { 33 | // TODO Auto-generated catch block 34 | e.printStackTrace(); 35 | } catch (IOException e) { 36 | // TODO Auto-generated catch block 37 | e.printStackTrace(); 38 | } 39 | } 40 | 41 | public static void main(String args[]){ 42 | /** 43 | * Fixed parameters 44 | */ 45 | T = 5; //time horizon 46 | M = 3; //machines 47 | N = 5; //nodes 48 | bowserInitialTankLevel = 0; 49 | maxBowserTankLevel = 20; 50 | minRefuelingQty = 1; 51 | tankCapacity = new int[]{6,6,6}; 52 | 53 | /** 54 | * Variable parameters 55 | */ 56 | int topologies = 6; 57 | int[][] initialTankLevelArray = {{0,0,0},{3,0,5},{5,5,5}}; 58 | int[][][] fuelConsumptionArray = new int[][][]{ 59 | {{3,3,3,3,3}, 60 | {3,3,3,3,3}, 61 | {3,3,3,3,3} 62 | }, 63 | {{2,2,2,2,2}, 64 | {1,1,1,1,1}, 65 | {3,3,3,3,3} 66 | }, 67 | {{1,2,3,4,5}, 68 | {5,4,3,2,1}, 69 | {3,3,1,1,2} 70 | } 71 | }; 72 | int[] fuelStockOutPenaltyCosts = {50,100}; 73 | 74 | writeToFile("./"+BowserRoutingBatch.class.getSimpleName() + "_results.csv", BowserRouting.getHeadersString()); 75 | 76 | for(int topology = 0; topology < topologies; topology++){ 77 | for(int initialTankLevelIndex = 0; initialTankLevelIndex < initialTankLevelArray.length; initialTankLevelIndex++){ 78 | for(int fuelConsumptionIndex = 0; fuelConsumptionIndex < fuelConsumptionArray.length; fuelConsumptionIndex++){ 79 | for(int fuelStockOutPenaltyCostIndex = 0; fuelStockOutPenaltyCostIndex < fuelStockOutPenaltyCosts.length; fuelStockOutPenaltyCostIndex++){ 80 | /** 81 | * Instance 82 | */ 83 | initialTankLevel = initialTankLevelArray[initialTankLevelIndex]; 84 | fuelConsumption = fuelConsumptionArray[fuelConsumptionIndex]; 85 | N = Topology.getTopology(topology).getN(); 86 | connectivity = Topology.getTopology(topology).getConnectivity().clone(); 87 | distance = Topology.getTopology(topology).getDistance().clone(); 88 | machineLocation = Location.getMachineLocation(topology).getMachineLocation().clone(); 89 | fuelStockOutPenaltyCost = fuelStockOutPenaltyCosts[fuelStockOutPenaltyCostIndex]; 90 | 91 | BowserRouting bowserRouting = new BowserRouting(T, M, N, 92 | bowserInitialTankLevel, 93 | maxBowserTankLevel, 94 | minRefuelingQty, 95 | tankCapacity, 96 | initialTankLevel, 97 | fuelConsumption, 98 | connectivity, 99 | distance, 100 | machineLocation, 101 | fuelStockOutPenaltyCost); 102 | bowserRouting.runInstance(); 103 | writeToFile("./"+BowserRoutingBatch.class.getSimpleName() + "_results.csv", bowserRouting.toString()); 104 | } 105 | } 106 | } 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/stochastic/fuel/BRF_StateDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.routing.stochastic.fuel; 28 | 29 | import java.util.Arrays; 30 | 31 | import jsdp.sdp.StateDescriptor; 32 | 33 | public class BRF_StateDescriptor extends StateDescriptor { 34 | 35 | private static final long serialVersionUID = 1L; 36 | 37 | private int bowserTankLevel; 38 | private int bowserLocation; 39 | 40 | private int machineTankLevel[]; 41 | private int machineLocation[]; 42 | 43 | public BRF_StateDescriptor(int period, 44 | int bowserTankLevel, 45 | int bowserLocation, 46 | int machineTankLevel[], 47 | int machineLocation[]){ 48 | super(period); 49 | this.bowserTankLevel = bowserTankLevel; 50 | this.bowserLocation = bowserLocation; 51 | this.machineTankLevel = Arrays.copyOf(machineTankLevel, machineTankLevel.length); 52 | this.machineLocation = Arrays.copyOf(machineLocation, machineLocation.length); 53 | } 54 | 55 | public int getBowserTankLevel(){ 56 | return this.bowserTankLevel; 57 | } 58 | 59 | public int getBowserLocation(){ 60 | return this.bowserLocation; 61 | } 62 | 63 | public int[] getMachineTankLevel(){ 64 | return this.machineTankLevel; 65 | } 66 | 67 | public int[] getMachineLocation(){ 68 | return this.machineLocation; 69 | } 70 | 71 | @Override 72 | public boolean equals(Object state) { 73 | if(state instanceof BRF_StateDescriptor) 74 | return this.period == ((BRF_StateDescriptor)state).period && 75 | this.bowserTankLevel == ((BRF_StateDescriptor)state).bowserTankLevel && 76 | this.bowserLocation == ((BRF_StateDescriptor)state).bowserLocation && 77 | Arrays.equals(this.machineTankLevel, ((BRF_StateDescriptor)state).machineTankLevel) && 78 | Arrays.equals(this.machineLocation, ((BRF_StateDescriptor)state).machineLocation); 79 | else 80 | return false; 81 | } 82 | 83 | @Override 84 | public int hashCode() { 85 | String hash = "SD"; 86 | hash = (hash + period) + "_" + 87 | this.bowserTankLevel + "_" + 88 | this.bowserLocation + 89 | Arrays.toString(this.machineTankLevel); 90 | Arrays.toString(this.machineLocation); 91 | return hash.hashCode(); 92 | } 93 | 94 | @Override 95 | public String toString(){ 96 | String out = ""; 97 | out = "Period: " + period + "\t" + 98 | "Bowser fuel: " + this.bowserTankLevel + "\t" + 99 | "Bowser location: " + this.bowserLocation + "\t" + 100 | "Machine tank: " +Arrays.toString(this.machineTankLevel) + "\t" + 101 | "Machine location: " + Arrays.toString(this.machineLocation); 102 | return out; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/stochastic/fuel/BRF_StateSpace.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.routing.stochastic.fuel; 28 | 29 | import java.util.ArrayList; 30 | import java.util.Iterator; 31 | import java.util.function.Function; 32 | 33 | import jsdp.sdp.Action; 34 | import jsdp.sdp.HashType; 35 | import jsdp.sdp.State; 36 | import jsdp.sdp.StateSpace; 37 | 38 | public class BRF_StateSpace extends StateSpace { 39 | 40 | public BRF_StateSpace(int period, 41 | Function> buildActionList, 42 | HashType hashType, 43 | int stateSpaceSizeLowerBound, 44 | float loadFactor){ 45 | super(period, hashType, stateSpaceSizeLowerBound, loadFactor); 46 | BRF_StateSpace.buildActionList = buildActionList; 47 | } 48 | 49 | public BRF_StateSpace(int period, 50 | Function> buildActionList, 51 | HashType hashType){ 52 | super(period, hashType); 53 | BRF_StateSpace.buildActionList = buildActionList; 54 | } 55 | 56 | public boolean exists (BRF_StateDescriptor descriptor){ 57 | return states.get(descriptor) != null; 58 | } 59 | 60 | public State getState(BRF_StateDescriptor descriptor){ 61 | State value = states.get(descriptor); 62 | if(value == null){ 63 | State state = new BRF_State(descriptor); 64 | this.states.put(descriptor, state); 65 | return state; 66 | }else 67 | return (BRF_State) value; 68 | } 69 | 70 | public Iterator iterator() { 71 | throw new NullPointerException("Method not implemented"); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/stochastic/location/BRL_ForwardRecursion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.routing.stochastic.location; 28 | 29 | import java.util.ArrayList; 30 | import java.util.function.Function; 31 | 32 | import jsdp.sdp.Action; 33 | import jsdp.sdp.ForwardRecursion; 34 | import jsdp.sdp.HashType; 35 | import jsdp.sdp.ImmediateValueFunction; 36 | import jsdp.sdp.State; 37 | import jsdp.sdp.ValueRepository; 38 | import jsdp.sdp.impl.univariate.SamplingScheme; 39 | 40 | public class BRL_ForwardRecursion extends ForwardRecursion { 41 | 42 | double[][][] machineLocation; 43 | int[][] fuelConsumption; 44 | 45 | public BRL_ForwardRecursion(int horizonLength, 46 | double[][][] machineLocation, 47 | int[][] fuelConsumption, 48 | ImmediateValueFunction immediateValueFunction, 49 | Function> buildActionList, 50 | double discountFactor, 51 | HashType hashType, 52 | int stateSpaceSizeLowerBound, 53 | float loadFactor, 54 | SamplingScheme samplingScheme, 55 | int sampleSize, 56 | double reductionFactorPerStage){ 57 | super(OptimisationDirection.MIN); 58 | this.horizonLength = horizonLength; 59 | this.machineLocation = machineLocation; 60 | this.fuelConsumption = fuelConsumption; 61 | 62 | this.stateSpace = new BRL_StateSpace[this.horizonLength+1]; 63 | for(int i = 0; i < this.horizonLength + 1; i++) 64 | this.stateSpace[i] = new BRL_StateSpace(i, buildActionList, hashType, stateSpaceSizeLowerBound, loadFactor); 65 | this.transitionProbability = new BRL_TransitionProbability(machineLocation, 66 | fuelConsumption, 67 | (BRL_StateSpace[])this.getStateSpace(), 68 | samplingScheme, 69 | sampleSize, 70 | reductionFactorPerStage); 71 | this.valueRepository = new ValueRepository(immediateValueFunction, 72 | discountFactor, 73 | stateSpaceSizeLowerBound, 74 | loadFactor, 75 | hashType); 76 | } 77 | 78 | public double getExpectedCost(BRL_StateDescriptor stateDescriptor){ 79 | State state = ((BRL_StateSpace)this.getStateSpace(stateDescriptor.getPeriod())).getState(stateDescriptor); 80 | return getExpectedValue(state); 81 | } 82 | 83 | public BRL_Action getOptimalAction(BRL_StateDescriptor stateDescriptor){ 84 | State state = ((BRL_StateSpace)this.getStateSpace(stateDescriptor.getPeriod())).getState(stateDescriptor); 85 | return (BRL_Action) this.getValueRepository().getOptimalAction(state); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/stochastic/location/BRL_StateDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.routing.stochastic.location; 28 | 29 | import java.util.Arrays; 30 | 31 | import jsdp.sdp.StateDescriptor; 32 | 33 | public class BRL_StateDescriptor extends StateDescriptor { 34 | 35 | private static final long serialVersionUID = 1L; 36 | 37 | private int bowserTankLevel; 38 | private int bowserLocation; 39 | 40 | private int machineTankLevel[]; 41 | private int machineLocation[]; 42 | 43 | public BRL_StateDescriptor(int period, 44 | int bowserTankLevel, 45 | int bowserLocation, 46 | int machineTankLevel[], 47 | int machineLocation[]){ 48 | super(period); 49 | this.bowserTankLevel = bowserTankLevel; 50 | this.bowserLocation = bowserLocation; 51 | this.machineTankLevel = Arrays.copyOf(machineTankLevel, machineTankLevel.length); 52 | this.machineLocation = Arrays.copyOf(machineLocation, machineLocation.length); 53 | } 54 | 55 | public int getBowserTankLevel(){ 56 | return this.bowserTankLevel; 57 | } 58 | 59 | public int getBowserLocation(){ 60 | return this.bowserLocation; 61 | } 62 | 63 | public int[] getMachineTankLevel(){ 64 | return this.machineTankLevel; 65 | } 66 | 67 | public int[] getMachineLocation(){ 68 | return this.machineLocation; 69 | } 70 | 71 | @Override 72 | public boolean equals(Object state) { 73 | if(state instanceof BRL_StateDescriptor) 74 | return this.period == ((BRL_StateDescriptor)state).period && 75 | this.bowserTankLevel == ((BRL_StateDescriptor)state).bowserTankLevel && 76 | this.bowserLocation == ((BRL_StateDescriptor)state).bowserLocation && 77 | Arrays.equals(this.machineTankLevel, ((BRL_StateDescriptor)state).machineTankLevel) && 78 | Arrays.equals(this.machineLocation, ((BRL_StateDescriptor)state).machineLocation); 79 | else 80 | return false; 81 | } 82 | 83 | @Override 84 | public int hashCode() { 85 | String hash = "SD"; 86 | hash = (hash + period) + "_" + 87 | this.bowserTankLevel + "_" + 88 | this.bowserLocation + 89 | Arrays.toString(this.machineTankLevel); 90 | Arrays.toString(this.machineLocation); 91 | return hash.hashCode(); 92 | } 93 | 94 | @Override 95 | public String toString(){ 96 | String out = ""; 97 | out = "Period: " + period + "\t" + 98 | "Bowser fuel: " + this.bowserTankLevel + "\t" + 99 | "Bowser location: " + this.bowserLocation + "\t" + 100 | "Machine tank: " +Arrays.toString(this.machineTankLevel) + "\t" + 101 | "Machine location: " + Arrays.toString(this.machineLocation); 102 | return out; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/app/routing/stochastic/location/BRL_StateSpace.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.app.routing.stochastic.location; 28 | 29 | import java.util.ArrayList; 30 | import java.util.Iterator; 31 | import java.util.function.Function; 32 | 33 | import jsdp.sdp.Action; 34 | import jsdp.sdp.HashType; 35 | import jsdp.sdp.State; 36 | import jsdp.sdp.StateSpace; 37 | 38 | public class BRL_StateSpace extends StateSpace { 39 | 40 | public BRL_StateSpace(int period, 41 | Function> buildActionList, 42 | HashType hashType, 43 | int stateSpaceSizeLowerBound, 44 | float loadFactor){ 45 | super(period, hashType, stateSpaceSizeLowerBound, loadFactor); 46 | BRL_StateSpace.buildActionList = buildActionList; 47 | } 48 | 49 | public BRL_StateSpace(int period, 50 | Function> buildActionList, 51 | HashType hashType){ 52 | super(period, hashType); 53 | BRL_StateSpace.buildActionList = buildActionList; 54 | } 55 | 56 | public boolean exists (BRL_StateDescriptor descriptor){ 57 | return states.get(descriptor) != null; 58 | } 59 | 60 | public State getState(BRL_StateDescriptor descriptor){ 61 | State value = states.get(descriptor); 62 | if(value == null){ 63 | State state = new BRL_State(descriptor); 64 | this.states.put(descriptor, state); 65 | return state; 66 | }else 67 | return (BRL_State) value; 68 | } 69 | 70 | public Iterator iterator() { 71 | throw new NullPointerException("Method not implemented"); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/Action.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import java.io.Serializable; 30 | 31 | /** 32 | * An abstraction for an action that can be selected for a given {@code State}. 33 | * 34 | * @author Roberto Rossi 35 | * 36 | */ 37 | public abstract class Action implements Serializable{ 38 | 39 | private static final long serialVersionUID = 1L; 40 | 41 | protected State state; 42 | 43 | public Action(State state){ 44 | this.state = state; 45 | } 46 | 47 | /** 48 | * Returns the {@code State} associated with this {@code Action}. 49 | * 50 | * @return the {@code State} associated with this {@code Action} 51 | */ 52 | public State getState() { 53 | return this.state; 54 | } 55 | 56 | public abstract boolean equals(Object action); 57 | 58 | public abstract int hashCode(); 59 | } 60 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/ActionIterator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import java.util.Iterator; 30 | 31 | /** 32 | * An abstraction representing an {@code Iterator} for a given action space. 33 | * 34 | * @author Roberto Rossi 35 | * 36 | */ 37 | public abstract class ActionIterator implements Iterator { 38 | 39 | public abstract boolean hasNext(); 40 | public abstract Action next(); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/BestActionRepository.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import org.apache.logging.log4j.LogManager; 30 | import org.apache.logging.log4j.Logger; 31 | 32 | import jsdp.sdp.Recursion.OptimisationDirection; 33 | 34 | /** 35 | * Stores the best action and its value. 36 | * 37 | * @author Roberto Rossi 38 | * 39 | */ 40 | public class BestActionRepository { 41 | 42 | static final Logger logger = LogManager.getLogger(BestActionRepository.class.getName()); 43 | 44 | Action bestAction = null; 45 | double bestValue; 46 | OptimisationDirection direction; 47 | 48 | public BestActionRepository(OptimisationDirection direction) { 49 | this.direction = direction; 50 | bestValue = direction == OptimisationDirection.MIN ? Double.MAX_VALUE : Double.MIN_VALUE; 51 | } 52 | 53 | /** 54 | * Compares {@code currentAction} and {@code currentValue} to the best action currently stored and updates 55 | * values stored accordingly. 56 | * 57 | * @param currentAction the action. 58 | * @param currentValue the action expected value. 59 | */ 60 | public synchronized void update(Action currentAction, double currentValue){ 61 | if(currentAction == null) 62 | throw new NullPointerException("Current action cannot be null"); 63 | switch(direction){ 64 | case MIN: 65 | if(bestAction == null || Double.isNaN(this.bestValue) || currentValue < bestValue){ 66 | bestValue = currentValue; 67 | bestAction = currentAction; 68 | } 69 | break; 70 | case MAX: 71 | if(bestAction == null || Double.isNaN(this.bestValue) || currentValue > bestValue){ 72 | bestValue = currentValue; 73 | bestAction = currentAction; 74 | } 75 | break; 76 | } 77 | } 78 | 79 | /** 80 | * Returns the best action stored. 81 | * 82 | * @return the best action stored. 83 | */ 84 | public Action getBestAction(){ 85 | if(Double.isNaN(this.bestValue)) 86 | logger.error("Number of samples probably too low"); 87 | return this.bestAction; 88 | } 89 | 90 | /** 91 | * Returns the value associated with the best action stored. 92 | * 93 | * @return the value associated with the best action stored. 94 | */ 95 | public double getBestValue(){ 96 | if(Double.isNaN(this.bestValue)) 97 | logger.error("Number of samples probably too low"); 98 | return this.bestValue; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/HashType.java: -------------------------------------------------------------------------------- 1 | package jsdp.sdp; 2 | 3 | /** 4 | * Available hash mappings. Note that ConcurrentHashMap should not be used with our 5 | * forward recursion code, as this leads to deadlocks. It is instead fine to 6 | * use ConcurrentHashMap in the context of our backward recursion code. 7 | * 8 | * http://blog.jooq.org/2015/03/04/avoid-recursion-in-concurrenthashmap-computeifabsent 9 | * https://bugs.openjdk.java.net/browse/JDK-8074374 10 | * http://stackoverflow.com/q/28840047/521799 11 | * 12 | * @author Roberto Rossi 13 | * 14 | */ 15 | 16 | public enum HashType { 17 | HASHTABLE, 18 | CONCURRENT_HASHMAP, 19 | THASHMAP, 20 | MAPDB_HEAP, 21 | MAPDB_HEAP_SHARDED, 22 | MAPDB_MEMORY, 23 | MAPDB_MEMORY_SHARDED, 24 | MAPDB_DISK 25 | } 26 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/ImmediateValueFunction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | /** 30 | * A functional interface that captures immediate value of a transition from an initial state to 31 | * a final state under a chosen action. 32 | * 33 | * @author Roberto Rossi 34 | * 35 | * @param the generic type for a state 36 | * @param the generic type for an action 37 | * @param the generic type of the value returned 38 | */ 39 | @FunctionalInterface 40 | public interface ImmediateValueFunction { 41 | /** 42 | * The immediate value function 43 | * 44 | * @param initialState the initial state of the stochastic process. 45 | * @param action the chosen action. 46 | * @param finalState the final state of the stochastic process. 47 | * @return the immediate value of a transition from {@code initialState} to {@code finalState} under a chosen {@code action}. 48 | */ 49 | public D apply (S initialState, A action, S finalState); 50 | } -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/RandomOutcomeFunction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | /** 30 | * A functional interface that captures random outcomes. 31 | * 32 | * @author Roberto Rossi 33 | * 34 | * @param the generic type for a state 35 | * @param the generic type for an action 36 | * @param the generic type of the value returned 37 | */ 38 | @FunctionalInterface 39 | public interface RandomOutcomeFunction { 40 | /** 41 | * The random outcome function 42 | * 43 | * @param initialState the initial state of the stochastic process. 44 | * @param action the chosen action. 45 | * @param finalState the final state of the stochastic process. 46 | * @return the random outcome associated with a transition from {@code initialState} to {@code finalState} under a chosen {@code action}. 47 | */ 48 | public D apply (S initialState, A action, S finalState); 49 | } -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/Recursion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import org.apache.logging.log4j.LogManager; 30 | import org.apache.logging.log4j.Logger; 31 | 32 | /** 33 | * An abstraction for a recursive solution method for the stochastic dynamic program. 34 | * 35 | * @author Roberto Rossi 36 | * 37 | */ 38 | public abstract class Recursion { 39 | 40 | static final Logger logger = LogManager.getLogger(Recursion.class.getName()); 41 | 42 | public enum OptimisationDirection { 43 | MIN, 44 | MAX 45 | }; 46 | 47 | protected int horizonLength; 48 | protected StateSpace[] stateSpace; 49 | protected TransitionProbability transitionProbability; 50 | protected ValueRepository valueRepository; 51 | final protected OptimisationDirection direction; 52 | 53 | protected boolean stateMonitoring = false; 54 | 55 | /** 56 | * Creates an instance of {@code Recursion} with the given optimisatio direction. 57 | * 58 | * @param direction the direction of optimisation. 59 | */ 60 | protected Recursion(OptimisationDirection direction){ 61 | this.direction = direction; 62 | } 63 | 64 | /** 65 | * Returns the expected value associated with {@code state}. 66 | * 67 | * @param state the target state. 68 | * @return the expected value associated with {@code state}. 69 | */ 70 | public double getExpectedValue(State state){ 71 | return this.getValueRepository().getOptimalExpectedValue(state); 72 | } 73 | 74 | /** 75 | * Returns the {@code StateSpace} for period {@code period}. 76 | * 77 | * @param period the target period. 78 | * @return the {@code StateSpace} for period {@code period}. 79 | */ 80 | public StateSpace getStateSpace(int period){ 81 | return this.stateSpace[period]; 82 | } 83 | 84 | /** 85 | * Returns the {@code StateSpace} array for the stochastic process planning horizon. 86 | * 87 | * @return the {@code StateSpace} array for the stochastic process planning horizon. 88 | */ 89 | public StateSpace[] getStateSpace(){ 90 | return this.stateSpace; 91 | } 92 | 93 | /** 94 | * Returns the {@code TransitionProbability} of the stochastic process. 95 | * 96 | * @return the {@code TransitionProbability} of the stochastic process. 97 | */ 98 | public TransitionProbability getTransitionProbability(){ 99 | return this.transitionProbability; 100 | } 101 | 102 | /** 103 | * Returns the {@code ValueRepository} of the stochastic process. 104 | * 105 | * @return the {@code ValueRepository} of the stochastic process. 106 | */ 107 | public ValueRepository getValueRepository(){ 108 | return this.valueRepository; 109 | } 110 | 111 | /** 112 | * If {@code stateMonitoring} is {@code true} state generation and reuse 113 | * are monitored during the recursion. 114 | * 115 | * @param stateMonitoring status of state monitoring 116 | */ 117 | public void setStateMonitoring(boolean stateMonitoring){ 118 | this.stateMonitoring = stateMonitoring; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/State.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import java.io.Serializable; 30 | import java.util.ArrayList; 31 | 32 | /** 33 | * An abstraction for a state in which the system may be found in period {@code period}. 34 | * 35 | * @author Roberto Rossi 36 | * 37 | */ 38 | public abstract class State implements Serializable{ 39 | 40 | private static final long serialVersionUID = 1L; 41 | 42 | protected int period; 43 | 44 | /** 45 | * Creates a {@code State} associated with a given {@code period}. 46 | * 47 | * @param period planning horizon period associated with the state. 48 | */ 49 | public State(int period){ 50 | this.period = period; 51 | } 52 | 53 | /** 54 | * Returns an {@code ArrayList} of feasible actions for this 55 | * {@code State}. 56 | * 57 | * @return the {@code ArrayList} of feasible actions. 58 | */ 59 | public ArrayList getFeasibleActions() { 60 | return StateSpace.getBuildActionList().apply(this); 61 | } 62 | 63 | /** 64 | * Returns the idempotent {@code Action} for this {@code State}. 65 | * 66 | * @return the idempotent {@code Action} for this {@code State}. 67 | */ 68 | public Action getNoAction(){ 69 | return StateSpace.getIdempotentAction().apply(this); 70 | } 71 | 72 | /** 73 | * Returns the planning horizon period associated with the state. 74 | * 75 | * @return the planning horizon period associated with the state. 76 | */ 77 | public int getPeriod(){ 78 | return this.period; 79 | } 80 | 81 | public abstract boolean equals(Object state); 82 | public abstract int hashCode(); 83 | } 84 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/StateAction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import java.io.Serializable; 30 | 31 | /** 32 | * An association abstraction for a pair state-action. 33 | * 34 | * @author Roberto Rossi 35 | * 36 | */ 37 | public class StateAction implements Serializable{ 38 | 39 | private static final long serialVersionUID = 1L; 40 | 41 | State initialState; 42 | Action action; 43 | 44 | /** 45 | * Creates an instance of {@code StateAction} from state {@code initialState} and action {@code action}. 46 | * 47 | * @param initialState the target state. 48 | * @param action the target action. 49 | */ 50 | public StateAction(State initialState, Action action){ 51 | this.initialState = initialState; 52 | this.action = action; 53 | } 54 | 55 | public boolean equals(StateAction stateAction){ 56 | return this.initialState.equals(stateAction.initialState) && this.action.equals(stateAction.action); 57 | } 58 | 59 | public int hashCode(){ 60 | String hash = ""; 61 | hash = hash + initialState.hashCode() + action.hashCode(); 62 | return hash.hashCode(); 63 | } 64 | } -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/StateDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import java.io.Serializable; 30 | 31 | /** 32 | * An abstract lightweight descriptor to uniquely identify a {@code State}. {@code StateDescriptor} 33 | * must implement method {@code hashCode()}, which will be used by the {@code Hashtable} used 34 | * to store states. 35 | * 36 | * @author Roberto Rossi 37 | * 38 | */ 39 | public abstract class StateDescriptor implements Serializable{ 40 | 41 | private static final long serialVersionUID = 1L; 42 | 43 | protected int period; 44 | 45 | @Override 46 | public abstract boolean equals(Object descriptor); 47 | @Override 48 | public abstract int hashCode(); 49 | 50 | public StateDescriptor(int period){ 51 | this.period = period; 52 | } 53 | 54 | /** 55 | * Returns the period associated with this {@code StateDescriptor}. 56 | * @return the period associated with this {@code StateDescriptor}. 57 | */ 58 | public int getPeriod(){ 59 | return period; 60 | } 61 | } -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/StateSpaceIterator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import java.util.Iterator; 30 | 31 | /** 32 | * An abstraction representing an {@code Iterator} for a given {@code StateSpace}. 33 | * 34 | * @author Roberto Rossi 35 | * 36 | */ 37 | public abstract class StateSpaceIterator implements Iterator { 38 | 39 | public abstract boolean hasNext(); 40 | public abstract State next(); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/StateTransitionFunction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | /** 30 | * A functional interface that captures the state transition function. 31 | * 32 | * @author Roberto Rossi 33 | * 34 | * @param the generic type for a state 35 | * @param the generic type for an action 36 | * @param the generic type of a random outcome 37 | */ 38 | @FunctionalInterface 39 | public interface StateTransitionFunction { 40 | 41 | /** 42 | * The state transition function 43 | * 44 | * @param state the initial state 45 | * @param action the chosen action 46 | * @param randomOutcome the observed random outcome 47 | * @return the final state 48 | */ 49 | public S apply (S state, A action, R randomOutcome); 50 | } 51 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/TransitionProbability.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp; 28 | 29 | import java.util.ArrayList; 30 | 31 | /** 32 | * An abstraction to capture the stochastic decision process transition probabilities. 33 | * 34 | * @author Roberto Rossi 35 | * 36 | */ 37 | public abstract class TransitionProbability { 38 | 39 | /** 40 | * This method returns the transition probability from {@code initialState} to {@code finalState} when 41 | * {@code action} is selected. 42 | * 43 | * @param initialState the initial state. 44 | * @param action the action chosen. 45 | * @param finalState the final state. 46 | * @return the transition probability from {@code initialState} to {@code finalState} when 47 | * {@code action} is selected. 48 | */ 49 | public abstract double getTransitionProbability(State initialState, Action action, State finalState); 50 | 51 | /** 52 | * This method constructs an {@code ArrayList} of states towards which the stochastic process may 53 | * transition in period {@code t+1} if {@code action} is selected in {@code initialState} at period 54 | * {@code t}; note that these states may not yet exist in the state space. This method is used by 55 | * forward recursion procedures. 56 | * 57 | * @param initialState the initial state of the stochastic process; note that we assume that 58 | * {@code initialState} is associated with period {@code t}. 59 | * @param action the action selected at period {@code t}. 60 | * @return an {@code ArrayList} of states towards which the stochastic process may 61 | * transition in period {@code t+1}. 62 | */ 63 | public abstract ArrayList generateFinalStates(State initialState, Action action); 64 | 65 | /** 66 | * This method retrieves an {@code ArrayList} of existing states towards which the stochastic process may 67 | * transition in period {@code t+1} if {@code action} is selected in {@code initialState} at period 68 | * {@code t}. This method is used by backward recursion procedures. 69 | * 70 | * @param initialState the initial state of the stochastic process; note that we assume that 71 | * {@code initialState} is associated with period {@code t}. 72 | * @param action the action selected at period {@code t}. 73 | * @return an {@code ArrayList} of states towards which the stochastic process may 74 | * transition in period {@code t+1}. 75 | */ 76 | public abstract ArrayList getFinalStates(State initialState, Action action); 77 | } 78 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/multivariate/ActionImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.multivariate; 28 | 29 | import java.util.Arrays; 30 | 31 | import jsdp.sdp.Action; 32 | import jsdp.sdp.State; 33 | 34 | /** 35 | * A concrete implementation of {@code Action}. 36 | * 37 | * @author Roberto Rossi 38 | * 39 | */ 40 | public class ActionImpl extends Action { 41 | 42 | private static final long serialVersionUID = 1L; 43 | 44 | int[] intAction; 45 | 46 | private static double[] arrayProduct(int[] integerArray, double[] doubleArray){ 47 | if(integerArray.length != doubleArray.length) 48 | throw new NullPointerException("Array sizes do not agree"); 49 | double[] result = new double[integerArray.length]; 50 | for(int i = 0; i < integerArray.length; i++){ 51 | result[i] = integerArray[i]*doubleArray[i]; 52 | } 53 | return result; 54 | } 55 | 56 | public static double[] intActionToAction(int[] action){ 57 | return arrayProduct(action, StateImpl.getStepSize()); 58 | } 59 | 60 | private static double[] arrayDivision(double[] doubleArray1, double[] doubleArray2){ 61 | if(doubleArray1.length != doubleArray2.length) 62 | throw new NullPointerException("Array sizes do not agree"); 63 | double[] result = new double[doubleArray1.length]; 64 | for(int i = 0; i < doubleArray1.length; i++){ 65 | result[i] = doubleArray1[i]/doubleArray2[i]; 66 | } 67 | return result; 68 | } 69 | 70 | public static int[] actionToIntAction(double[] action){ 71 | double[] result = arrayDivision(action, StateImpl.getStepSize()); 72 | int[] intResult = new int[result.length]; 73 | for(int i = 0; i < result.length; i++){ 74 | intResult[i] = (int) Math.round(result[i]); 75 | } 76 | return intResult; 77 | } 78 | 79 | public ActionImpl(State state, int[] intAction){ 80 | super(state); 81 | this.intAction = Arrays.copyOf(intAction, intAction.length); 82 | } 83 | 84 | public ActionImpl(State state, double[] action){ 85 | super(state); 86 | this.intAction = actionToIntAction(action); 87 | } 88 | 89 | public int[] getIntAction(){ 90 | return this.intAction; 91 | } 92 | 93 | public double[] getAction(){ 94 | return intActionToAction(this.intAction); 95 | } 96 | 97 | @Override 98 | public boolean equals(Object action){ 99 | if(action instanceof ActionImpl) 100 | return Arrays.equals(this.intAction, ((ActionImpl)action).intAction); 101 | else 102 | return false; 103 | } 104 | 105 | @Override 106 | public int hashCode(){ 107 | String hash = ""; 108 | hash = (hash + Arrays.toString(intAction)); 109 | return hash.hashCode(); 110 | } 111 | 112 | @Override 113 | public String toString(){ 114 | return state+"\tAction: "+ Arrays.toString(intActionToAction(this.intAction)); 115 | } 116 | } 117 | 118 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/multivariate/SamplingScheme.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.multivariate; 28 | 29 | /** 30 | * An enumeration of possible sampling strategies that may be used by the backward recursion algorithm. 31 | * 32 | * @author Roberto Rossi 33 | * 34 | */ 35 | public enum SamplingScheme { 36 | NONE, 37 | SIMPLE_RANDOM_SAMPLING, 38 | STRATIFIED_SAMPLING, 39 | JENSENS_PARTITIONING 40 | } 41 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/multivariate/StateDescriptorImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.multivariate; 28 | 29 | import java.util.Arrays; 30 | 31 | import jsdp.sdp.StateDescriptor; 32 | 33 | /** 34 | * A concrete implementation of {@code StateDescriptor}. 35 | * 36 | * @author Roberto Rossi 37 | * 38 | */ 39 | public class StateDescriptorImpl extends StateDescriptor{ 40 | 41 | private static final long serialVersionUID = 1L; 42 | 43 | int[] initialIntState; 44 | 45 | public StateDescriptorImpl(int period, int[] initialIntState){ 46 | super(period); 47 | this.initialIntState = Arrays.copyOf(initialIntState, initialIntState.length); 48 | } 49 | 50 | public StateDescriptorImpl(int period, double[] initialState){ 51 | super(period); 52 | this.initialIntState = StateImpl.stateToIntState(initialState); 53 | } 54 | 55 | @Override 56 | public boolean equals(Object descriptor){ 57 | if(descriptor instanceof StateDescriptorImpl) 58 | return this.period == ((StateDescriptorImpl)descriptor).period && 59 | Arrays.equals(this.initialIntState, ((StateDescriptorImpl)descriptor).initialIntState); 60 | else 61 | return false; 62 | } 63 | 64 | @Override 65 | public int hashCode(){ 66 | String hash = ""; 67 | hash = (hash + period) + "_" + Arrays.toString(initialIntState); 68 | return hash.hashCode(); 69 | } 70 | 71 | public int[] getInitialIntState(){ 72 | return this.initialIntState; 73 | } 74 | } -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/multivariate/StateSpaceIteratorImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.multivariate; 28 | 29 | import jsdp.sdp.State; 30 | import jsdp.sdp.StateSpaceIterator; 31 | 32 | /** 33 | * A concrete implementation of {@code StateSpaceIterator}. 34 | * 35 | * @author Roberto Rossi 36 | * 37 | */ 38 | public class StateSpaceIteratorImpl extends StateSpaceIterator { 39 | 40 | StateSpaceImpl stateSpace; 41 | StateDescriptorImpl currentStateDescriptor; 42 | int stateSpacePointer; 43 | 44 | public StateSpaceIteratorImpl(StateSpaceImpl stateSpace){ 45 | this.stateSpace = stateSpace; 46 | currentStateDescriptor = new StateDescriptorImpl(this.stateSpace.getPeriod(), StateImpl.getMaxIntState()); 47 | stateSpacePointer = getStateSpaceCardinality() - 1; 48 | } 49 | 50 | private static int getStateSpaceCardinality(){ 51 | int cardinality = 1; 52 | for(int i = 0; i < StateImpl.getStateDimension(); i++){ 53 | cardinality *= StateImpl.getMaxIntState()[i] - StateImpl.getMinIntState()[i] + 1; 54 | } 55 | return cardinality; 56 | } 57 | 58 | private static int getResidualStateSpaceCardinality(int j){ 59 | int cardinality = 1; 60 | for(int i = j; i < StateImpl.getStateDimension(); i++){ 61 | cardinality *= StateImpl.getMaxIntState()[i] - StateImpl.getMinIntState()[i] + 1; 62 | } 63 | return cardinality; 64 | } 65 | 66 | private static int[] getIntState(int stateSpacePointer){ 67 | int[] intState = new int[StateImpl.getStateDimension()]; 68 | for(int i = 0; i < StateImpl.getStateDimension(); i++){ 69 | intState[i] = Math.floorDiv(Math.floorMod(stateSpacePointer, getResidualStateSpaceCardinality(i)), i < StateImpl.getStateDimension() - 1 ? getResidualStateSpaceCardinality(i+1) : 1) + StateImpl.getMinIntState()[i]; 70 | } 71 | return intState; 72 | } 73 | 74 | public boolean hasNext() { 75 | if(stateSpacePointer >= 0) 76 | return true; 77 | else 78 | return false; 79 | } 80 | 81 | public State next() { 82 | if(stateSpacePointer >= 0){ 83 | State state = stateSpace.getState(currentStateDescriptor); 84 | stateSpacePointer -= 1; 85 | if(stateSpacePointer >= 0 ) 86 | currentStateDescriptor = new StateDescriptorImpl(currentStateDescriptor.getPeriod(), getIntState(stateSpacePointer)); 87 | else 88 | currentStateDescriptor = null; 89 | return state; 90 | }else{ 91 | return null; 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/univariate/ActionImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.univariate; 28 | 29 | import jsdp.sdp.Action; 30 | import jsdp.sdp.State; 31 | 32 | /** 33 | * A concrete implementation of {@code Action}. 34 | * 35 | * @author Roberto Rossi 36 | * 37 | */ 38 | public class ActionImpl extends Action { 39 | 40 | private static final long serialVersionUID = 1L; 41 | 42 | int intAction; 43 | 44 | public static double intActionToAction(int action){ 45 | return action*StateImpl.getStepSize(); 46 | } 47 | 48 | public static int actionToIntAction(double action){ 49 | return (int) Math.round(action/StateImpl.getStepSize()); 50 | } 51 | 52 | public ActionImpl(State state, int intAction){ 53 | super(state); 54 | this.intAction = intAction; 55 | } 56 | 57 | public ActionImpl(State state, double action){ 58 | super(state); 59 | this.intAction = actionToIntAction(action); 60 | } 61 | 62 | protected int getIntAction(){ 63 | return this.intAction; 64 | } 65 | 66 | public double getAction(){ 67 | return intActionToAction(this.intAction); 68 | } 69 | 70 | @Override 71 | public boolean equals(Object action){ 72 | if(action instanceof ActionImpl) 73 | return this.state.equals(((ActionImpl)action).state) && (this.intAction == ((ActionImpl)action).intAction); 74 | else 75 | return false; 76 | } 77 | 78 | @Override 79 | public int hashCode(){ 80 | String hash = ""; 81 | hash = (hash + this.state.hashCode() + "_" + intAction); 82 | return hash.hashCode(); 83 | } 84 | 85 | @Override 86 | public String toString(){ 87 | return state+"\tAction: "+ intActionToAction(this.intAction); 88 | } 89 | } 90 | 91 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/univariate/SamplingScheme.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.univariate; 28 | 29 | /** 30 | * An enumeration of possible sampling strategies that may be used by the backward recursion algorithm. 31 | * 32 | * @author Roberto Rossi 33 | * 34 | */ 35 | public enum SamplingScheme { 36 | NONE, 37 | SIMPLE_RANDOM_SAMPLING, 38 | STRATIFIED_SAMPLING, 39 | JENSENS_PARTITIONING 40 | } 41 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/univariate/StateDescriptorImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.univariate; 28 | 29 | import jsdp.sdp.StateDescriptor; 30 | 31 | /** 32 | * A concrete implementation of {@code StateDescriptor}. 33 | * 34 | * @author Roberto Rossi 35 | * 36 | */ 37 | public class StateDescriptorImpl extends StateDescriptor{ 38 | 39 | private static final long serialVersionUID = 1L; 40 | 41 | int initialIntState; 42 | 43 | public StateDescriptorImpl(int period, int initialIntState){ 44 | super(period); 45 | this.initialIntState = initialIntState; 46 | } 47 | 48 | public StateDescriptorImpl(int period, double initialState){ 49 | super(period); 50 | this.initialIntState = StateImpl.stateToIntState(initialState); 51 | } 52 | 53 | @Override 54 | public boolean equals(Object descriptor){ 55 | if(descriptor instanceof StateDescriptorImpl) 56 | return this.period == ((StateDescriptorImpl)descriptor).period && 57 | this.initialIntState == ((StateDescriptorImpl)descriptor).initialIntState; 58 | else 59 | return false; 60 | } 61 | 62 | @Override 63 | public int hashCode(){ 64 | String hash = ""; 65 | hash = (hash + period) + "_" + initialIntState; 66 | return hash.hashCode(); 67 | } 68 | 69 | public int getInitialIntState(){ 70 | return initialIntState; 71 | } 72 | } -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/univariate/StateImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.univariate; 28 | 29 | import jsdp.sdp.State; 30 | 31 | /** 32 | * A concrete implementation of {@code State}. 33 | * 34 | * @author Roberto Rossi 35 | * 36 | */ 37 | public class StateImpl extends State { 38 | 39 | private static final long serialVersionUID = 1L; 40 | 41 | private int initialIntState; 42 | 43 | private static double stepSize; 44 | private static int minIntState; 45 | private static int maxIntState; 46 | 47 | /** 48 | * Initializes the state space boundaries. Note that {@code stepSize} must be one if 49 | * {@code DiscreteDistributionInt} are used. 50 | * 51 | * @param stepSize the discretization step used to encode the state space 52 | * @param minState the minimum value used to encode a state 53 | * @param maxState the maximum value used to encode a state 54 | */ 55 | public static void setStateBoundaries(double stepSize, double minState, double maxState){ 56 | StateImpl.stepSize = stepSize; 57 | StateImpl.minIntState = (int)Math.round(minState/stepSize); 58 | StateImpl.maxIntState = (int)Math.round(maxState/stepSize); 59 | } 60 | 61 | public static double getStepSize(){ 62 | return StateImpl.stepSize; 63 | } 64 | 65 | public static double intStateToState(int intState){ 66 | return intState*stepSize; 67 | } 68 | 69 | public static int stateToIntState(double state){ 70 | return (int) Math.max(Math.min(Math.round(state/stepSize), StateImpl.maxIntState), StateImpl.minIntState); 71 | } 72 | 73 | public static int getMinIntState(){ 74 | return StateImpl.minIntState; 75 | } 76 | 77 | public static int getMaxIntState(){ 78 | return StateImpl.maxIntState; 79 | } 80 | 81 | public static double getMinState(){ 82 | return intStateToState(minIntState); 83 | } 84 | 85 | public static double getMaxState(){ 86 | return intStateToState(maxIntState); 87 | } 88 | 89 | public StateImpl(StateDescriptorImpl descriptor){ 90 | super(descriptor.getPeriod()); 91 | this.initialIntState = descriptor.getInitialIntState(); 92 | } 93 | 94 | public int getInitialIntState(){ 95 | return this.initialIntState; 96 | } 97 | 98 | public double getInitialState(){ 99 | return intStateToState(this.initialIntState); 100 | } 101 | 102 | @Override 103 | public boolean equals(Object state){ 104 | if(state instanceof StateImpl) 105 | return this.period == ((StateImpl)state).period && this.initialIntState == ((StateImpl)state).initialIntState; 106 | else return false; 107 | } 108 | 109 | @Override 110 | public int hashCode(){ 111 | String hash = ""; 112 | hash = hash + period + "_" + initialIntState; 113 | return hash.hashCode(); 114 | } 115 | 116 | @Override 117 | public String toString(){ 118 | return "Period: "+this.period+"\tState:"+intStateToState(this.initialIntState); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/sdp/impl/univariate/StateSpaceIteratorImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.sdp.impl.univariate; 28 | 29 | import jsdp.sdp.State; 30 | import jsdp.sdp.StateSpaceIterator; 31 | 32 | /** 33 | * A concrete implementation of {@code StateSpaceIterator}. 34 | * 35 | * @author Roberto Rossi 36 | * 37 | */ 38 | public class StateSpaceIteratorImpl extends StateSpaceIterator { 39 | 40 | StateSpaceImpl stateSpace; 41 | StateDescriptorImpl currentStateDescriptor; 42 | 43 | public StateSpaceIteratorImpl(StateSpaceImpl stateSpace){ 44 | this.stateSpace = stateSpace; 45 | currentStateDescriptor = new StateDescriptorImpl(this.stateSpace.getPeriod(), StateImpl.getMaxIntState()); 46 | } 47 | 48 | public boolean hasNext() { 49 | if(currentStateDescriptor.getInitialIntState() <= StateImpl.getMaxIntState() && 50 | currentStateDescriptor.getInitialIntState() >= StateImpl.getMinIntState()) 51 | return true; 52 | else 53 | return false; 54 | } 55 | 56 | public State next() { 57 | if(currentStateDescriptor.getInitialIntState() <= StateImpl.getMaxIntState() && 58 | currentStateDescriptor.getInitialIntState() >= StateImpl.getMinIntState()){ 59 | State state = stateSpace.getState(currentStateDescriptor); 60 | currentStateDescriptor = new StateDescriptorImpl(currentStateDescriptor.getPeriod(), 61 | currentStateDescriptor.getInitialIntState() - 1); 62 | return state; 63 | }else{ 64 | return null; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/utilities/monitoring/MonitoringInterface.java: -------------------------------------------------------------------------------- 1 | package jsdp.utilities.monitoring; 2 | 3 | import java.awt.HeadlessException; 4 | import java.io.IOException; 5 | import java.lang.management.ManagementFactory; 6 | 7 | import javax.swing.JFrame; 8 | import javax.swing.JOptionPane; 9 | import javax.swing.JTextArea; 10 | 11 | import com.sun.management.OperatingSystemMXBean; 12 | 13 | /** 14 | * Abstract class capturing a graphical interface to monitor computation 15 | * 16 | * @author Roberto Rossi 17 | * 18 | */ 19 | 20 | @SuppressWarnings("restriction") 21 | public abstract class MonitoringInterface extends JFrame implements Runnable{ 22 | 23 | private static final long serialVersionUID = 1L; 24 | 25 | protected JTextArea text = new JTextArea(); 26 | 27 | protected OperatingSystemMXBean osMBean; 28 | protected long nanoBefore; 29 | protected long nanoAfter; 30 | protected long cpuBefore; 31 | protected long cpuAfter; 32 | protected boolean terminate = false; 33 | 34 | protected long generatedStates; 35 | 36 | public MonitoringInterface() throws HeadlessException { 37 | super(); 38 | JFrame frame = this; 39 | frame.addWindowListener(new java.awt.event.WindowAdapter() { 40 | @Override 41 | public void windowClosing(java.awt.event.WindowEvent windowEvent) { 42 | if (JOptionPane.showConfirmDialog(frame, 43 | "Are you sure to close this window?", "Really Closing?", 44 | JOptionPane.YES_NO_OPTION, 45 | JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){ 46 | System.exit(0); 47 | } 48 | } 49 | }); 50 | } 51 | 52 | /** 53 | * Set monitoring window text 54 | * 55 | * @param text the text to be displayed 56 | */ 57 | protected void setText(String text) { 58 | this.text.setText(text); 59 | } 60 | 61 | /** 62 | * Starts monitoring the resolution process 63 | */ 64 | public void startMonitoring() { 65 | try { 66 | osMBean = ManagementFactory.newPlatformMXBeanProxy( 67 | ManagementFactory.getPlatformMBeanServer(), ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class); 68 | nanoBefore = System.nanoTime(); 69 | cpuBefore = osMBean.getProcessCpuTime(); 70 | } catch (IOException e) { 71 | // TODO Auto-generated catch block 72 | e.printStackTrace(); 73 | } 74 | 75 | Thread runner = new Thread(this); 76 | runner.start(); 77 | } 78 | 79 | /** 80 | * Terminates monitoring 81 | */ 82 | public void terminate() { 83 | this.terminate = true; 84 | } 85 | 86 | /** 87 | * Get the current resolution time (in seconds) 88 | * 89 | * @return the current resolution time (in seconds) 90 | */ 91 | public int getTime(){ 92 | return (int) Math.ceil(((this.nanoAfter-this.nanoBefore)*Math.pow(10, -9))); 93 | } 94 | 95 | /** 96 | * Get the % usage for the available cores 97 | * 98 | * @return the % usage for the available cores 99 | */ 100 | public long getPercentCPU(){ 101 | long percent; 102 | if (this.nanoAfter > this.nanoBefore) 103 | percent = ((this.cpuAfter-this.cpuBefore)*100L)/(this.nanoAfter-this.nanoBefore); 104 | else percent = 0; 105 | return percent; 106 | } 107 | 108 | /** 109 | * Get the number of generated states 110 | * 111 | * @return the number of generated states 112 | */ 113 | public long getGeneratedStates(){ 114 | return this.generatedStates; 115 | } 116 | 117 | /** 118 | * Get the number of processed states per second 119 | * 120 | * @return the number of processed states per second 121 | */ 122 | public abstract double getProcessedStatesPerSecond(); 123 | } -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/utilities/monitoring/MonitoringInterfaceBackward.java: -------------------------------------------------------------------------------- 1 | package jsdp.utilities.monitoring; 2 | 3 | import jsdp.sdp.Recursion; 4 | 5 | /** 6 | * Monitoring interface for backward recursion 7 | * 8 | * @author Roberto Rossi 9 | * 10 | */ 11 | 12 | public class MonitoringInterfaceBackward extends MonitoringInterface{ 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | protected long processedStates; 17 | protected int currentStage; 18 | 19 | public MonitoringInterfaceBackward(Recursion recursion){ 20 | recursion.setStateMonitoring(true); 21 | this.setTitle("jsdp statistics"); 22 | this.text.setEditable(false); 23 | this.getContentPane().add(text); 24 | this.setSize(300, 150); 25 | this.setVisible(true); 26 | } 27 | 28 | /** 29 | * Set states status 30 | * 31 | * @param generatedStates number of generated states 32 | * @param processedStates number of processed states 33 | * @param currentStage current stage 34 | */ 35 | public void setStates(long generatedStates, long processedStates, int currentStage) { 36 | this.generatedStates = generatedStates; 37 | this.processedStates = processedStates; 38 | this.currentStage = currentStage; 39 | } 40 | 41 | @SuppressWarnings("restriction") 42 | @Override 43 | public void run() { 44 | this.cpuAfter = this.osMBean.getProcessCpuTime(); 45 | this.nanoAfter = System.nanoTime(); 46 | while(!terminate){ 47 | try { 48 | Thread.sleep(1000); 49 | } catch (InterruptedException e) { 50 | // TODO Auto-generated catch block 51 | e.printStackTrace(); 52 | } 53 | this.cpuAfter = this.osMBean.getProcessCpuTime(); 54 | this.nanoAfter = System.nanoTime(); 55 | 56 | setText("Time: " + getTime() +"\n" 57 | + "CPU: " +this.getPercentCPU()+ "%" +" ("+Runtime.getRuntime().availableProcessors()+" cores)\n" 58 | + "Generated states: " + this.getGeneratedStates() +"\n" 59 | + "States processed: "+ this.getProcessedStates() +"\n" 60 | + "States processed per second: "+ this.getProcessedStatesPerSecond() +"\n" 61 | + "Percent completed: "+ (int) Math.floor(processedStates*100.0/generatedStates) +"%\n" 62 | + "Current stage: " + currentStage); 63 | } 64 | } 65 | 66 | /** 67 | * Get the number of processed states 68 | * 69 | * @return the number of processed states 70 | */ 71 | public long getProcessedStates(){ 72 | return this.processedStates; 73 | } 74 | 75 | @Override 76 | public double getProcessedStatesPerSecond(){ 77 | return (int) Math.ceil((this.processedStates)/((this.nanoAfter-this.nanoBefore)*Math.pow(10, -9))); 78 | } 79 | 80 | @Override 81 | public String toString(){ 82 | return "Time: " + getTime() +"\n" 83 | + "CPU: " +this.getPercentCPU()+ "%" +" ("+Runtime.getRuntime().availableProcessors()+" cores)\n" 84 | + "Generated states: " + this.getGeneratedStates() +"\n" 85 | + "States processed: "+ this.getProcessedStates() +"\n" 86 | + "States processed per second: "+ this.getProcessedStatesPerSecond() +"\n" 87 | + "Percent completed: "+ (int) Math.floor(processedStates*100.0/generatedStates) +"%\n" 88 | + "Current stage: " + currentStage; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/utilities/monitoring/MonitoringInterfaceForward.java: -------------------------------------------------------------------------------- 1 | package jsdp.utilities.monitoring; 2 | 3 | import java.util.Arrays; 4 | 5 | import jsdp.sdp.Recursion; 6 | 7 | /** 8 | * Monitoring interface for forward recursion 9 | * 10 | * @author Roberto Rossi 11 | * 12 | */ 13 | 14 | public class MonitoringInterfaceForward extends MonitoringInterface{ 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | protected long reusedStates; 19 | protected long actionCounter; 20 | protected long totalActions; 21 | protected long[] actionFrequencies; 22 | 23 | public MonitoringInterfaceForward(Recursion recursion){ 24 | recursion.setStateMonitoring(true); 25 | this.setTitle("jsdp statistics"); 26 | this.text.setEditable(false); 27 | this.getContentPane().add(text); 28 | this.setSize(300, 150); 29 | this.setVisible(true); 30 | } 31 | 32 | /** 33 | * Set states status 34 | * 35 | * @param generatedStates number of generated states 36 | * @param reusedStates number of reused 37 | */ 38 | public void setStates(long generatedStates, long reusedStates, long actionCounter, long totalActions, long[] actionFrequencies) { 39 | this.generatedStates = generatedStates; 40 | this.reusedStates = reusedStates; 41 | this.actionCounter = actionCounter; 42 | this.totalActions = totalActions; 43 | this.actionFrequencies = actionFrequencies; 44 | } 45 | 46 | @SuppressWarnings("restriction") 47 | @Override 48 | public void run() { 49 | this.cpuAfter = this.osMBean.getProcessCpuTime(); 50 | this.nanoAfter = System.nanoTime(); 51 | while(!terminate){ 52 | try { 53 | Thread.sleep(1000); 54 | } catch (InterruptedException e) { 55 | // TODO Auto-generated catch block 56 | e.printStackTrace(); 57 | } 58 | 59 | this.cpuAfter = this.osMBean.getProcessCpuTime(); 60 | this.nanoAfter = System.nanoTime(); 61 | 62 | setText("Time: " + this.getTime() +"\n" 63 | + "CPU: " + this.getPercentCPU() +"%" +" ("+Runtime.getRuntime().availableProcessors()+" cores)\n" 64 | + "States processed per second: "+ getProcessedStatesPerSecond() +"\n" 65 | + "Generated states: " + this.getGeneratedStates() +"\n" 66 | + "Reused states: " + this.getReusedStates() +"\n" 67 | + "Percent completed: "+ (int) Math.floor(actionCounter*100.0/totalActions) +"%\n" 68 | + Arrays.toString(actionFrequencies)); 69 | } 70 | } 71 | 72 | /** 73 | * Get number of states that have been reused in the forward recursion process. 74 | * In forward recursion if a state has been already visited, the optimal cost/action 75 | * do not need to be recomputed. 76 | * 77 | * @return number of states that have been reused 78 | * @see Memoization 79 | */ 80 | public long getReusedStates(){ 81 | return this.reusedStates; 82 | } 83 | 84 | @Override 85 | public double getProcessedStatesPerSecond(){ 86 | return (int) Math.ceil((generatedStates+reusedStates)/((this.nanoAfter-this.nanoBefore)*Math.pow(10, -9))); 87 | } 88 | 89 | @Override 90 | public String toString(){ 91 | return "Time: " + this.getTime() +"\n" 92 | + "CPU: " + this.getPercentCPU() +"%" +" ("+Runtime.getRuntime().availableProcessors()+" cores)\n" 93 | + "States processed per second: "+ getProcessedStatesPerSecond() +"\n" 94 | + "Generated states: " + this.getGeneratedStates() +"\n" 95 | + "Reused states: " + this.getReusedStates(); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/utilities/probdist/MultiINIDistribution.java: -------------------------------------------------------------------------------- 1 | package jsdp.utilities.probdist; 2 | 3 | import java.util.Arrays; 4 | import java.util.stream.IntStream; 5 | 6 | import jsdp.sdp.impl.multivariate.StateImpl; 7 | 8 | import umontreal.ssj.probdist.DiscreteDistribution; 9 | import umontreal.ssj.probdist.Distribution; 10 | import umontreal.ssj.probdistmulti.DiscreteDistributionIntMulti; 11 | 12 | /** 13 | * This class implements a multivariate distribution built from independently 14 | * non-identically distributed random variables 15 | * 16 | * @author Roberto Rossi 17 | * 18 | */ 19 | public class MultiINIDistribution extends DiscreteDistributionIntMulti{ 20 | Distribution[] distributions; 21 | double[] supportLowerBounds; 22 | double[] supportUpperBounds; 23 | DiscreteDistribution[] discreteDistributions; 24 | 25 | /** 26 | * Creates a new instance of a multivariate distribution built from independently 27 | * non-identically distributed random variables. 28 | * 29 | * @param distributions the constituting independent discrete distributions. 30 | * @param supportLowerBounds support lower bound of each distribution 31 | * @param supportUpperBounds support upper bound of each distribution 32 | */ 33 | public MultiINIDistribution(Distribution[] distributions, double[] supportLowerBounds, double[] supportUpperBounds){ 34 | this.dimension = distributions.length; 35 | this.distributions = Arrays.copyOf(distributions, distributions.length); 36 | this.supportLowerBounds = Arrays.copyOf(supportLowerBounds, supportLowerBounds.length); 37 | this.supportUpperBounds = Arrays.copyOf(supportUpperBounds, supportUpperBounds.length); 38 | } 39 | 40 | public void discretizeDistributions(){ 41 | this.discreteDistributions = IntStream.iterate(0, i -> i + 1) 42 | .limit(this.dimension) 43 | .mapToObj(i -> DiscreteDistributionFactory.getTruncatedDiscreteDistribution(distributions[i], supportLowerBounds[i], supportUpperBounds[i], StateImpl.getStepSize()[i])) 44 | .toArray(DiscreteDistribution[]::new); 45 | } 46 | 47 | @Override 48 | public double prob(int[] x) { 49 | double prob = 1; 50 | for(int i = 0; i < this.dimension; i++){ 51 | prob *= this.discreteDistributions[i].prob(x[i]); 52 | } 53 | return prob; 54 | } 55 | 56 | @Override 57 | public double[] getMean() { 58 | return Arrays.stream(this.discreteDistributions).mapToDouble(d -> d.getMean()).toArray(); 59 | } 60 | 61 | @Override 62 | public double[][] getCovariance() { 63 | double[][] covMatrix = new double[this.dimension][this.dimension]; 64 | for(int i = 0; i < this.dimension; i++){ 65 | covMatrix[i][i] = this.discreteDistributions[i].getVariance(); 66 | } 67 | return covMatrix; 68 | } 69 | 70 | @Override 71 | public double[][] getCorrelation() { 72 | double[][] corrMatrix = new double[this.dimension][this.dimension]; 73 | for(int i = 0; i < this.dimension; i++){ 74 | corrMatrix[i][i] = 1.0; 75 | } 76 | return corrMatrix; 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/utilities/probdist/SafeMultinomialDist.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.utilities.probdist; 28 | 29 | import java.util.Arrays; 30 | 31 | import umontreal.ssj.probdistmulti.MultinomialDist; 32 | 33 | /** 34 | * A "safe" implementation of a MultinomialDist; this implementation 35 | * rather than throwing an exception, returns a probability 0.0 for states 36 | * that are outside the multinomial support (i.e. states with negative values). 37 | * 38 | * @author Roberto Rossi 39 | * 40 | */ 41 | public class SafeMultinomialDist extends MultinomialDist{ 42 | 43 | /** 44 | * Creates a `MultinomialDist` object with parameters @f$n@f$ and 45 | * (@f$p_1@f$,…,@f$p_d@f$) such that @f$\sum_{i=1}^d p_i = 1@f$. We 46 | * have @f$p_i = @f$ `p[i-1]`. 47 | * 48 | * @param n number of trials 49 | * @param p multinomial probabilities 50 | */ 51 | public SafeMultinomialDist (int n, double p[]) { 52 | super(n, p); 53 | } 54 | 55 | @Override 56 | public double prob (int x[]) { 57 | if(Arrays.stream(x).filter(element -> element < 0).findAny().isPresent()) 58 | return 0; 59 | else 60 | return super.prob(x); 61 | } 62 | 63 | @Override 64 | public double cdf (int x[]) { 65 | if(Arrays.stream(x).filter(element -> element < 0).findAny().isPresent()) 66 | return 0; 67 | else 68 | return super.cdf(x); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /jsdp/src/main/java/jsdp/utilities/sampling/SampleFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.utilities.sampling; 28 | 29 | import java.util.stream.DoubleStream; 30 | import java.util.stream.IntStream; 31 | 32 | import umontreal.ssj.probdist.Distribution; 33 | import umontreal.ssj.randvar.UniformGen; 34 | import umontreal.ssj.randvar.UniformIntGen; 35 | import umontreal.ssj.rng.MRG32k3aL; 36 | import umontreal.ssj.rng.RandomStream; 37 | 38 | public class SampleFactory { 39 | 40 | private static RandomStream stream = new MRG32k3aL(); 41 | 42 | /** 43 | * Reinitializes the stream to the beginning of its next substream. 44 | */ 45 | public static void resetNextSubstream(){ 46 | stream.resetNextSubstream(); 47 | } 48 | 49 | /** 50 | * Reinitializes the stream to its initial state. 51 | */ 52 | public static void resetStartStream(){ 53 | stream.resetStartStream(); 54 | } 55 | 56 | /** 57 | * Implements Simple Random Sampling 58 | * @param distributions array of distributions to be sampled 59 | * @return a Simple Random Sample for the distributions in {@code distributions} 60 | */ 61 | public static double[] getNextSample(Distribution[] distributions){ 62 | UniformGen uniform = new UniformGen(stream); 63 | return IntStream.iterate(0, i -> i + 1) 64 | .limit(distributions.length).mapToDouble( 65 | i -> distributions[i].inverseF(uniform.nextDouble())) 66 | .toArray(); 67 | } 68 | 69 | /** 70 | * Implements Latin Hypercube Sampling as originally introduced in 71 | * 72 | * McKay, M.D.; Beckman, R.J.; Conover, W.J. (May 1979). 73 | * "A Comparison of Three Methods for Selecting Values of Input Variables 74 | * in the Analysis of Output from a Computer Code". 75 | * Technometrics 21 (2): 239–245. 76 | * 77 | * @param distributions array of distributions to be sampled 78 | * @param samples number of samples 79 | * @return a Latin Hypercube Sample for the distributions in {@code distributions} 80 | */ 81 | public static double[][] getNextLHSample(Distribution[] distributions, int samples){ 82 | double x[][] = new double[distributions.length][samples]; 83 | x = IntStream.iterate(0, d -> d + 1) 84 | .limit(distributions.length) 85 | .mapToObj( 86 | d -> DoubleStream.iterate(0, i -> i + 1.0/samples) 87 | .limit(samples) 88 | .map(i -> distributions[d].inverseF(i + UniformGen.nextDouble(stream, 0, 1.0/samples))) 89 | .toArray()) 90 | .toArray(double[][]::new); 91 | for(int i = 0; i < x.length; i++){ 92 | shuffle(x[i]); 93 | } 94 | return x; 95 | } 96 | 97 | /** 98 | * Returns a random shuffle of {@code sample}. 99 | * 100 | * @param sample the original sample. 101 | * @return a random shuffle of {@code sample}. 102 | */ 103 | private static double[] shuffle(double[] sample){ 104 | for(int i = 0; i < sample.length; i++){ 105 | int j = UniformIntGen.nextInt(stream, 0, sample.length - 1); 106 | double temp = sample[i]; 107 | sample[i] = sample[j]; 108 | sample[j] = temp; 109 | } 110 | return sample; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /jsdp/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /jsdp/src/test/java/jsdp/utilities/sampling/AllTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * jsdp: A Java Stochastic Dynamic Programming Library 3 | * 4 | * MIT License 5 | * 6 | * Copyright (c) 2016 Roberto Rossi 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in all 16 | * copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | * SOFTWARE. 25 | */ 26 | 27 | package jsdp.utilities.sampling; 28 | 29 | import org.junit.runner.RunWith; 30 | import org.junit.runners.Suite; 31 | import org.junit.runners.Suite.SuiteClasses; 32 | 33 | @RunWith(Suite.class) 34 | @SuiteClasses({ 35 | NormalSampleTest.class, 36 | PoissonSampleTest.class 37 | }) 38 | 39 | public class AllTests { 40 | 41 | } 42 | -------------------------------------------------------------------------------- /jsdp/src/test/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | --------------------------------------------------------------------------------