...

Source file src/scheduler/api/learning.go

Documentation: scheduler/api

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019 - 2022. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  package api
    20  
    21  import (
    22  	"net/http"
    23  	"scheduler/log"
    24  	"scheduler/service_learning"
    25  	"time"
    26  )
    27  
    28  var learningDevTestEid = 0
    29  
    30  func LearningDevTestAct(w http.ResponseWriter, r *http.Request) {
    31  	entry := service_learning.EntryAct{State: []float64{1.0, 2.3, 1.8}}
    32  	action, err := service_learning.Act(&entry)
    33  
    34  	if err != nil {
    35  		log.Log.Errorf("Cannot take decision: %s", err)
    36  	} else {
    37  		log.Log.Debugf("Taken decision: %f", action)
    38  	}
    39  
    40  	w.WriteHeader(200)
    41  }
    42  
    43  func LearningDevTestActWs(w http.ResponseWriter, r *http.Request) {
    44  	timeStart := time.Now()
    45  
    46  	entry := service_learning.EntryAct{State: []float64{1.0, 2.3, 1.8}}
    47  	action, err := service_learning.SocketAct(&entry)
    48  
    49  	timeEnd := time.Now()
    50  
    51  	if err != nil {
    52  		log.Log.Errorf("Cannot take decision: %s", err)
    53  	} else {
    54  		log.Log.Debugf("Taken decision: %f", action)
    55  	}
    56  
    57  	log.Log.Debugf("Elapsed time: %fms", (float64(timeEnd.UnixNano())-float64(timeStart.UnixNano()))/1000000)
    58  
    59  	w.WriteHeader(200)
    60  }
    61  
    62  func LearningDevTestTrain(w http.ResponseWriter, r *http.Request) {
    63  	entry := service_learning.EntryLearning{
    64  		Eid:    learningDevTestEid,
    65  		State:  []float64{1.2, 1.0, 1.6},
    66  		Action: 2,
    67  		Reward: 1.3,
    68  	}
    69  	err := service_learning.Train(&entry)
    70  
    71  	learningDevTestEid += 1
    72  
    73  	if err != nil {
    74  		log.Log.Errorf("Cannot take decision: %s", err)
    75  	} else {
    76  		log.Log.Debugf("Ok")
    77  	}
    78  
    79  	w.WriteHeader(200)
    80  }
    81  

View as plain text