<commit_before>"""Bryson--Denham optimal control problem."""


import numpy as np
import sympy
import sym2num.model
import sym2num.var

from ceacoest import oc, symb_oc


@symb_oc.collocate(order=3)
class BrysonDenham:
    """Symbolic Bryson--Denham optimal control model."""
        
    @sym2num.model.make_variables_dict
    def variables():
        """Model variables definition."""
        return [
            sym2num.var.SymbolArray('x', ['x1', 'x2', 'x3']),
            sym2num.var.SymbolArray('u', ['u1']),
            sym2num.var.SymbolArray('p', ['T']),
        ]
    
    @sym2num.model.symbols_from('x, u, p')
    def f(self, s):
        """ODE function."""
        return sympy.Array([s.x2, s.u1, 0.5*s.u1**2]) * s.T
    
    @sym2num.model.symbols_from('x, u, p')
    def g(self, s):
        """Path constraints."""
        return sympy.Array([], 0)
    
    @sym2num.model.symbols_from('xe, p')
    def h(self, s):
        """Endpoint constraints."""
        return sympy.Array([s.x1_start, s.x1_end, s.x2_start - 1, s.x2_end + 1])
    
    @sym2num.model.symbols_from('xe, p')
    def M(self, s):
        """Mayer (endpoint) cost."""
        return sympy.Array(s.x3_end)


if __name__ == '__main__':
    symb_mdl = BrysonDenham()
    GeneratedBrysonDenham = sym2num.model.compile_class(
        'GeneratedBrysonDenham', symb_mdl
    )
    mdl = GeneratedBrysonDenham()
    
    t = np.linspace(0, 1, 30)
    problem = oc.Problem(mdl, t)
    
    dec_bounds = np.repeat([[-np.inf], [np.inf]], problem.ndec, axis=-1)
    problem.set_decision('p', 0, dec_bounds[0])
    problem.set_decision('p', np.inf, dec_bounds[1])
    problem.set_decision('x', [-np.inf, -np.inf, 0], dec_bounds[0])
    problem.set_decision('x', [1/9, np.inf, np.inf], dec_bounds[1])
    constr_bounds = np.zeros((2, problem.ncons))
    <commit_msg>Tweaking Bryson--Denham example (MUMPS is not so good).<commit_after>"""Bryson--Denham optimal control problem."""


import numpy as np
import sympy
import sym2num.model
import sym2num.var

from ceacoest import oc, symb_oc


@symb_oc.collocate(order=3)
class BrysonDenham:
    """Symbolic Bryson--Denham optimal control model."""
        
    @sym2num.model.make_variables_dict
    def variables():
        """Model variables definition."""
        return [
            sym2num.var.SymbolArray('x', ['x1', 'x2', 'x3']),
            sym2num.var.SymbolArray('u', ['u1']),
            sym2num.var.SymbolArray('p', ['T']),
        ]
    
    @sym2num.model.symbols_from('x, u, p')
    def f(self, s):
        """ODE function."""
        return sympy.Array([s.x2, s.u1, 0.5*s.u1**2]) * s.T
    
    @sym2num.model.symbols_from('x, u, p')
    def g(self, s):
        """Path constraints."""
        return sympy.Array([], 0)
    
    @sym2num.model.symbols_from('xe, p')
    def h(self, s):
        """Endpoint constraints."""
        return sympy.Array([s.x1_start, s.x1_end, s.x2_start - 1, s.x2_end + 1])
    
    @sym2num.model.symbols_from('xe, p')
    def M(self, s):
        """Mayer (endpoint) cost."""
        return sympy.Array(s.x3_end)


if __name__ == '__main__':
    symb_mdl = BrysonDenham()
    GeneratedBrysonDenham = sym2num.model.compile_class(
        'GeneratedBrysonDenham', symb_mdl
    )
    mdl = GeneratedBrysonDenham()
    
    t = np.linspace(0, 1, 20)
    problem = oc.Problem(mdl, t)
    
    dec_bounds = np.repeat([[-np.inf], [np.inf]], problem.ndec, axis=-1)
    problem.set_decision('p', 0, dec_bounds[0])
    problem.set_decision('p', 4, dec_bounds[1])
    problem.set_decision('x', [0, -np.inf, 0], dec_bounds[0])
    problem.set_decision('x', [1/9, np.inf, np.inf], dec_bounds[1])
    constr_bounds = np.zeros((2, problem.ncons))
    
{'fraction_non_alphanumeric': 0.17139767504731007, 'fraction_numerical': 0.024330900243309004, 'mean_word_length': 2.7525354969574036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5253955', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1389, 'n_words': 271}