“无法获得未返回的生成器的返回值”php回溯

I am trying to do backtracking to solved a problem but I get this error:

Error: genator hasn´t returned

Fisrt I will introduce the main problem that the software must resolve:

I have been trying to create the software for a hospital, this software must create a schedule with the doctors for each day. Each day must have 5 assigned doctors, one for a specialty called "trauma", another for "ucr" and another for "box13".

Before assigning doctors, the user must enter the number of holes of each type that doctor can do that month. So I know how many "trauma", "ucr" or "box13" have any doctor that month.

Aaah! one more thing, you can not assign a doctor two days in a row.

So I decided to create a backtracking algorithm, the problem is that I am getting a Generator object at the end of the execution but I don´t get a valid return.

function bt($ps){
    if($ps->is_solution()){
        return $ps->get_solution();
    }
    else{   
        yield from $ps->successors();
    }
}


class Briker_vc_PS{
        public function __construct( $decisiones, $diasTotalFinal, $fechaInicio, $box13, $ucr, $trauma, $numGuardiasAsig){            
            $this->decisiones= $decisiones;
            $this->diasTotalFinal = $diasTotalFinal;
            $this->fechaInicio = $fechaInicio;
            $this->box13 = $box13;
            $this->ucr = $ucr;
            $this->trauma = $trauma;
            $this->numGuardiasAsig = $numGuardiasAsig;
        }   

        public function is_solution(){

            return $this->numGuardiasAsig == $this->diasTotalFinal*5;                
        }

        public function get_solution(){
            return $this->decisiones;
        }

        public function state(){ 
            return $this->decisiones[$this->fechaInicio];
        }

        public function  successors(){
            if( array_key_exists( $this->fechaInicio, $this->decisiones) == false ){
                $this->decisiones[$this->fechaInicio] = [];
            }

            if( array_key_exists( 'ids', $this->decisiones[$this->fechaInicio]) == false ){
                $this->decisiones[$this->fechaInicio]['ids'] = [];
            }


            if( array_key_exists( $this->fechaInicio ,$this->decisiones) and array_key_exists( 'box13' ,$this->decisiones) and array_key_exists( 'trauma' ,$this->decisiones) and array_key_exists( 'ucr' ,$this->decisiones)){
                if( (count($this->decisiones['trauma'])==1) and (count($this->decisiones['ucr'])==2) and (count($this->decisiones['box13'])==2) ){
                    $this->fechaInicio = date("Y-m-d",strtotime($this->fechaInicio." +1 day"));
                    $this->decisiones[$this->fechaInicio]['date'] = $this->fechaInicio;
                }               
            }

            $anterior = date("Y-m-d",strtotime($this->fechaInicio." -1 day"));

            $Lista=[];

            if( array_key_exists( 'trauma' ,$this->decisiones) == false ){
                foreach($this->trauma as $key => $val){
                    if( (in_array($key, $this->decisiones[$this->fechaInicio]['ids']) == false) and (in_array($key, $this->decisiones[$anterior]['ids']) == false) ){
                        $decisions= $this->decisiones;

                        $decisions[$this->fechaInicio]['trauma'] = [$key];
                        $auxtra= $this->trauma;

                        if($auxtra[$key] -1 == 0){
                            unset($auxtra[$key]);
                        }else{
                            $auxtra[$key] = $auxtra[$key] -1;
                        }

                        $num = $this->numGuardiasAsig +1 ;

                        $decisions[$this->fechaInicio]['ids'][] = $key;

                        yield from bt(new Briker_vc_PS( $decisions, $this->diasTotalFinal, $this->fechaInicio, $this->box13, $this->ucr, $auxtra, $num));

                    }
                }
            }

            if( (array_key_exists( 'box13' ,$this->decisiones) == false) or (count($this->decisiones['box13'])<2) ){
                foreach($this->box13 as $key => $val){
                    if( (in_array($key, $this->decisiones[$this->fechaInicio]['ids']) == false) and (in_array($key, $this->decisiones[$anterior]['ids']) == false) ){
                        $decisions= $this->decisiones;

                        if(array_key_exists( 'box13' ,$this->decisiones) == false){
                            $decisions[$this->fechaInicio]['box13'] = array();
                        }

                        $decisions[$this->fechaInicio]['box13'][] = $key;
                        $auxbox13= $this->box13;

                        if($auxbox13[$key] -1 == 0){
                            unset($auxbox13[$key]);
                        }else{
                            $auxbox13[$key] = $auxbox13[$key] -1;
                        }

                        $num = $this->numGuardiasAsig +1 ;

                        $decisions[$this->fechaInicio]['ids'][] = $key;

                        yield from bt( new Briker_vc_PS( $decisions, $this->diasTotalFinal, $this->fechaInicio, $auxbox13, $this->ucr, $this->trauma, $num));

                    }
                }
            }

            if( (array_key_exists( 'ucr' ,$this->decisiones) == false) or (count($this->decisiones['ucr'])<2) ){
                foreach($this->ucr as $key => $val){
                    if( (in_array($key, $this->decisiones[$this->fechaInicio]['ids']) == false) and (in_array($key, $this->decisiones[$anterior]['ids']) == false) ){
                        $decisions= $this->decisiones;

                        if(array_key_exists( 'ucr' ,$this->decisiones) == false){
                            $decisions[$this->fechaInicio]['ucr'] = array();
                        }   

                        $decisions[$this->fechaInicio]['ucr'][] = $key;
                        $auxucr= $this->ucr;

                        if($auxucr[$key] -1 == 0){
                            unset($auxucr[$key]);
                        }else{
                            $auxucr[$key] = $auxucr[$key] -1;
                        }

                        $decisions[$this->fechaInicio]['ids'][] = $key;

                        $num = $this->numGuardiasAsig +1 ;

                        yield from bt(new Briker_vc_PS( $decisions, $this->diasTotalFinal, $this->fechaInicio, $this->box13, $auxucr, $this->trauma, $num));

                    }
                }
            }

        }

        protected $GuardiasMedico;
        protected $decisiones;

 }

And this is the main program

      $month = $_REQUEST['mes'];
        $year = $_REQUEST['any'];

        $fecha_inicio = "01-".$month."-".$year;


 // fisrt i get the number of "trauma", "ucr" and "box13" for each doctor
// And i create a array with it with key the doctor_id

        $db = new SQLite3($dbname);

        $result = $db->query('SELECT m.id, m.nombre, m.apellido, mgm.ucr, mgm.trauma, mgm.box13 FROM medico AS m INNER JOIN MedicoGuardiaMes AS mgm ON m.id = mgm.doctor_id WHERE m.borrado = 0 AND mgm.mes = '.$month.' AND mgm.any = '.$year);


        $box13 = [];
        $ucr = [];
        $trauma = [];

        while($res = $result->fetchArray()){

            $box13[$res['id']] =  $res['box13'];
            $ucr[$res['id']] =  $res['ucr'];
            $trauma[$res['id']] =  $res['trauma'];

        }

        // I create the solution array and add the last day from the previous month

        $dataBaseDecisiones = [];

        $anterior = date("Y-m-d",strtotime($fecha_inicio." -1 day"));

        $dataBaseDecisiones[$anterior] = [];
        $dataBaseDecisiones[$anterior]['ids'] = [];

        $diasTotalFinal=cal_days_in_month(CAL_GREGORIAN, $month, $year);


       // Finally I create the initial object and starts the backtracking


        $initial_ps = new Briker_vc_PS($dataBaseDecisiones, $diasTotalFinal, $fecha_inicio, $box13, $ucr, $trauma, $numGuardiasAsig);

        var_dump(  bt($initial_ps)->getReturn() );

I don´t know why this code is not working or if I am using yield the right way.

dongyou5068
dongyou5068 toolong
接近 2 年之前 回复

1个回答

The problem that is reported happens because of the last line in your code:

bt($initial_ps)->getReturn();

When bt executes the else part it performs a recursive search until there is a return, and then it yields that value. But that does not finish the iterator's results and this yielded value is not considered the iterator's return value. So getReturn() triggers the exception you got.

You don't explain what you intend to get as output in that var_dump. If you want to output all the yielded results, then collect all those results first, for instance with iterator_to_array:

$iter = bt($initial_ps);
print_r(iterator_to_array($iter));

And only then you can execute:

print_r($iter->getReturn());
dongtang7347
dongtang7347 我只想要$ ps这是解决方案,数组调用决策。 var_dump是因为我想知道代码是否返回了一些内容
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问