Skip to content

Instantly share code, notes, and snippets.

@MohamedGouaouri
Created September 29, 2025 19:27
Show Gist options
  • Save MohamedGouaouri/d848b09bac7da778ad04482e1504fd48 to your computer and use it in GitHub Desktop.
Save MohamedGouaouri/d848b09bac7da778ad04482e1504fd48 to your computer and use it in GitHub Desktop.
package poweraware
import (
"bytes"
"encoding/json"
"fmt"
"os"
"gonum.org/v1/gonum/mat"
"k8s.io/klog/v2"
"io/ioutil"
"net/http"
)
var BaseUrl = os.Getenv("OPTIMIZER_URL")
type SchedulerRequest struct {
Nodes map[string][]float64 `json:"nodes"`
NObj int `json:"n_obj"`
}
type OptimizerResponse struct {
BestNode int `json:"best_node"`
}
func makeRequest(url string, data *SchedulerRequest) (*OptimizerResponse, error) {
// Marshal the data into JSON
jsonData, err := json.Marshal(data)
if err != nil {
return nil, err
}
// Create a new HTTP POST request
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
// Set the content type to application/json
req.Header.Set("Content-Type", "application/json")
// Use the default HTTP client to send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Check the HTTP status code
if resp.StatusCode != http.StatusOK {
return nil, err
}
// Print the response body
klog.Infof("Response: %s\n", string(body))
var optimizerResp = &OptimizerResponse{}
err = json.Unmarshal(body, optimizerResp)
if err != nil {
return nil, err
}
return optimizerResp, nil
}
func Nsga2Optimize(decisionMatrix *mat.Dense, numNodes, numCriterias int, algorithm string) (*OptimizerResponse, error) {
var request = &SchedulerRequest{
Nodes: make(map[string][]float64),
}
request.NObj = numCriterias
for i := 0; i < numNodes; i++ {
row := make([]float64, numCriterias)
for j := 0; j < numCriterias; j++ {
row[j] = decisionMatrix.At(i, j)
}
request.Nodes[fmt.Sprintf("%d", i)] = row
}
response, err := makeRequest(fmt.Sprintf("%s/%s", BaseUrl, algorithm), request)
if err != nil {
klog.Errorf("Error occured %v\n", err)
return nil, err
}
return response, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment