2023,江端さんの技術メモ

https://development.relaxes.jp/windows11にwsl2+ubuntu20-04をインストールする/

これまで、VMWareやら、VirtualBoxやら、(Dockerも、)仮想環境というのは基本的に面倒くさい思い出ばかりなの(特に通信I/F回り)で、Windows10/11をインストールする時も、意図してWSLは弾いてきたのですが ―― そのツケが回ってきました。

Golangは、Windowsで実行するより、WSL on Windows10/11の方が実行速度が速い 

という驚愕の情報をデータ付きで教えて頂き、現在、WSLの環境構築と、Golangのインストールを実行しています。

以外に簡単でしたし、通信I/F回りも今のところ、あまり問題になっていないようです。


さて、wslのインストールがコマンドプロンプトから、

>wsl -d Ubuntu-20.04

の環境を立ち上げて、

>sudo apt update
>sudo apt install golang-go

をしました。これでgolangは動くようになりました。

で、実際のプロウラムを動かしてみると、

# m/AgentSimulation
./db_util.go:280:24: undefined: sql.NullInt16
note: module requires Go 1.17

というエラーが出て止まります。

Go 1.17にモジュールが必要と言われているようですが、

$ go version
go version go1.13.8 linux/amd64

と出てきます。全然足りないようです。

ここからは、めんどくないGoのバージョンアップ を参考にして作業を進めました。

$sudo apt install golang-go

では、失敗するようです。

やっぱり

$sudo add-apt-repository ppa:longsleep/golang-backports

が重要なようでした。

定番の

$sudo apt autoremove
$sudo apt update
$sudo apt install
$sudo apt upgrade
をやって、再度
$sudo apt install golang-go
を実施したら、
$ go version
go version go1.20.2 linux/amd64
となりました。
-----

2023,江端さんの技術メモ

現在時刻を入れた、agoopデータ形式のcsvファイルをGolangで作る

// C:\Users\ebata\yamaguchi\src_try2\others\main9.csv

/*
	このデータ形式のcsvを作成する

	Dailyid,Year,Month,Day,Hour,Minute,Second,Latitude,Longitude
	14,2017,12,1,8,17,5,33.749583,132.709375
*/

package main

import (
	"encoding/csv"
	"fmt"
	"log"
	"os"
	"time"
)

func main() {

	const STATIONS_PATH string = "test.csv"

	// csvファイル
	csvFile, err := os.Create(STATIONS_PATH)
	if err != nil {
		log.Fatal(err)
	}
	defer csvFile.Close()

	// CSVファイルの中身を読み込み
	w := csv.NewWriter(csvFile)

	//str := "Dailyid,Year,Day,Hour,Minute,Second,Latitude,Longitude"
	str := []string{"Dailyid", "Year", "Day", "Hour", "Minute", "Second", "Latitude", "Longitude"}
	fmt.Println(str)

	if err = w.Write(str); err != nil {
		log.Fatal(err)
	}

	/////

	id := 1
	dt := time.Now()
	year := dt.Year()
	day := dt.Day()
	hour := dt.Hour()
	minute := dt.Minute()
	second := dt.Second()
	latitude := 33.749583
	longitude := 132.709375

	str = []string{fmt.Sprint(id), fmt.Sprint(year), fmt.Sprint(day), fmt.Sprint(hour), fmt.Sprint(minute), fmt.Sprint(second), fmt.Sprint(latitude), fmt.Sprint(longitude)}

	if err = w.Write(str); err != nil {
		log.Fatal(err)
	}

	w.Flush() // バッファに残っているデータを書き込む

}

2023,江端さんの技術メモ

使う度に調べるので、メモしておきます。

1. 40歳から44歳の人の人数を数えろ
=COUNTIFS($B$1:$B$498, ">=45",$B$1:$B$498, "<50" )

2.55歳から59歳の人の不満値の平均値を求めよ
=AVERAGEIFS($J$1:$J$498, $B$1:$B$498, ">=55",$B$1:$B$498, "<60" )

Keywords: COUNTIF, COUNTIFS, AVERAGEIFS

 

2023,江端さんの技術メモ

This program is based on the following page
https://qiita.com/ufoo68/items/9e4ca04578ba0f5fa5ff

This program solves the XOR problem using MLP with one hidden layer.

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
//num of units
#define NUM_INPUT 2
//#define NUM_HIDDEN 20
#define NUM_HIDDEN 2

double sigmoid(double x) {
    return 1/(1+exp(-x));
}

//derivative of sigmoid function
double d_sigmoid(double x) {
    double a = 0.1;
    return a*x*(1-x);
}

int main(void) {
    srand((unsigned)time(NULL));
//train data
    double train_x[4][NUM_INPUT+1] = {{0, 0, -1},{0, 1, -1},{1, 0, -1},{1, 1, -1}};
    double d[4] = {0, 1, 1, 0};
//net
    double w[NUM_HIDDEN+1][NUM_INPUT+1];
    double v[NUM_HIDDEN+1];
    double y[4][NUM_HIDDEN+1];
    double z[4];
    double eta = 0.1;
    int epoch = 1000000;
//other
    int i, j, k, l;
    double tmp = 0;

//update weights using rand()
    for(l=0; l<NUM_HIDDEN+1; l++) {
        for(i=0; i<NUM_INPUT+1; i++) {
            w[l][i] = ((double)rand() / ((double)RAND_MAX + 1));
        }
    }
    for(i=0; i<NUM_HIDDEN+1; i++) {
        v[i] = ((double)rand() / ((double)RAND_MAX + 1));
    }

//tain
    for(k=0; k<epoch; k++) {
        //feedforward
        for(j=0; j<4; j++) {
            //hidden
            for(l=0; l<NUM_HIDDEN; l++) {
                for(i=0; i<NUM_INPUT+1; i++) {
                    tmp += train_x[j][i] * w[l][i];
                }
                y[j][l] = sigmoid(tmp);
                tmp = 0;
            }
            y[j][NUM_HIDDEN] = -1;
            //output
            for(i=0; i<NUM_HIDDEN+1; i++) {
                tmp += y[j][i] * v[i];
            }
            z[j] = sigmoid(tmp);
            tmp = 0;

        //backward
            //output
            for(i=0; i<NUM_HIDDEN+1; i++) {
                v[i] = v[i] - eta * y[j][i] * d_sigmoid(z[j]) * (z[j] - d[j]);
            }

            //hidden
            for(l=0; l<NUM_INPUT+1; l++) {
                for(i=0; i<NUM_HIDDEN+1; i++) {
                    w[i][l] = w[i][l] - eta * train_x[j][l] * d_sigmoid(y[j][i]) * d_sigmoid(z[j]) * (z[j] - d[j]) * v[i];
                }
            }
        }

		/*
        //print detail
        printf("z=");
        for(i=0; i<4; i++) {
            printf("%f ", z[i]);
        }
        printf("epoch:%d\n",k);
		*/
    }

//predict
    for(j=0; j<4; j++) {
        //hidden
        for(l=0; l<NUM_HIDDEN; l++) {
            for(i=0; i<NUM_INPUT+1; i++) {
                tmp += train_x[j][i] * w[l][i];
            }
            y[j][l] = sigmoid(tmp);
            tmp = 0;
        }
        y[j][NUM_HIDDEN] = -1;
        //output
        for(i=0; i<NUM_HIDDEN+1; i++) {
            tmp += y[j][i] * v[i];
        }
        z[j] = sigmoid(tmp);
        tmp = 0;
    }

//print result
    printf("z=");
    for(i=0; i<4; i++) {
        printf("%f ", z[i]);
    }
    printf("epoch:%d\n",k);


    for(i=0; i<NUM_INPUT+1; i++) {
        for(l=0; l<NUM_HIDDEN+1; l++) {
		  printf("w[%d][%d]:%f\n", i, l, w[i][l]);
        }
    }

    for(i=0; i<NUM_HIDDEN+1; i++) {
	  printf("v[%d]:%f\n",i, v[i]);
    }


    return 0;
}

Save this program as a name "mlp.c", compile ">gcc mlp.c" and execute ">./a.exe"

2023,江端さんの技術メモ

Golang 文字列を数値に変換する方法で、文字列→実数なら、これが一番てっとり早そう

dest_lat, err := strconv.ParseFloat(row[2], 64)

strconv.ParseIntstrconv.ParseUnitは、文字列を解析して整数型を返す関数
それぞれ符号付き整数型と符号なし整数型に対応している。

 

2023,江端さんの技術メモ

I hope you could understand the min-max method of Fuzzy reasoning, using the following Go programming list.

package main

import (
	"fmt"
	"os"
)

func max_2(a, b float64) float64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func min_2(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

type condition_MF3 struct { // Base class for condition_MF3
	center  float64
	width   float64
	express string
}

func new_condition_MF3(_center, _width float64, _express string) *condition_MF3 {
	c3 := new(condition_MF3)
	c3.center = _center
	c3.width = _width
	c3.express = _express
	return c3
}

// Class for the membership function (3 mountains) of the former case
func (c3 *condition_MF3) func_X(_x float64) float64 {
		// x,y denote coordinates on the membership function
	x := _x
	y := 0.0 // The value of y is always greater than or equal to 0 and less than or equal to 1

	if c3.express == "LESS" {
		if x <= c3.center-c3.width {
			y = 1.0
		} else if x <= c3.center {
			y = -1.0 / c3.width * (x - c3.center)
		} else {
			y = 0.0
		}
	} else if c3.express == "COMMON" {
		if x <= c3.center-c3.width {
			y = 0.0
		} else if x <= c3.center {
			y = 1.0/c3.width*(x-c3.center) + 1.0
		} else if x <= c3.center+c3.width {
			y = -1.0/c3.width*(x-c3.center) + 1.0
		} else {
			y = 0.0
		}
	} else if c3.express == "MORE" {
		if x <= c3.center {
			y = 0.0
		} else if x <= c3.center+c3.width {
			y = 1.0 / c3.width * (x - c3.center)
		} else {
			y = 1.0
		}
	} else {
		fmt.Println("MF3: wrong expression")
		os.Exit(1)
	}
	return y
}

type condition_MF5 struct { // Base class for condition_MF5
	center  float64
	width   float64
	express string
}

func new_condition_MF5(_center, _width float64, _express string) *condition_MF5 {
	c5 := new(condition_MF5)
	c5.center = _center
	c5.width = _width
	c5.express = _express
	return c5
}

func (c5 *condition_MF5) func_X(_x float64) float64 {
	// Class for the former membership function (5 mountains)
	// x,y are the coordinates on the membership function

	x := _x
	y := 0.0 // The value of y is always greater than or equal to 0 and less than or equal to 1

	if c5.express == "LESSLESS" {
		if x <= c5.center-2.0*c5.width {
			y = 1.0
		} else if x <= c5.center-c5.width {
			y = -1.0/c5.width*(x-(c5.center-2.0*c5.width)) + 1.0
		} else {
			y = 0.0
		}
	} else if c5.express == "LESS" {
		if x <= c5.center-2.0*c5.width {
			y = 0.0
		} else if x <= c5.center-c5.width {
			y = 1.0/c5.width*(x-(c5.center-c5.width)) + 1.0
		} else if x <= c5.center {
			y = -1.0/c5.width*(x-(c5.center-c5.width)) + 1.0
		} else {
			y = 0.0
		}
	} else if c5.express == "COMMON" {
		if x <= c5.center-c5.width {
			y = 0.0
		} else if x <= c5.center {
			y = 1.0/c5.width*(x-c5.center) + 1.0
		} else if x <= c5.center+c5.width {
			y = -1.0/c5.width*(x-c5.center) + 1.0
		} else {
			y = 0.0
		}
	} else if c5.express == "MORE" {
		if x <= c5.center {
			y = 0.0
		} else if x <= c5.center+c5.width {
			y = 1.0/c5.width*(x-(c5.center+c5.width)) + 1.0
		} else if x <= c5.center+2.0*c5.width {
			y = -1.0/c5.width*(x-(c5.center+c5.width)) + 1.0
		} else {
			y = 0.0
		}
	} else if c5.express == "MOREMORE" {
		if x <= c5.center+c5.width {
			y = 0.0
		} else if x <= c5.center+2.0*c5.width {
			y = 1.0/c5.width*(x-(c5.center+2.0*c5.width)) + 1.0
		} else {
			y = 1.0
		}
	} else {
		fmt.Println("MF5 func_X(): wrong expression")
		os.Exit(1)
	}

	return y
}

/////////////////////////////

type action_MF5 struct { // Base class for action_MF5
	center  float64
	width   float64
	express string
	x       float64
	y       float64
}

type action_MF3 struct { // Base class for action_MF3
	center  float64
	width   float64
	express string
	x       float64
	y       float64
}


func new_action_MF5(_center, _width float64, _express string) *action_MF5 {
	a5 := new(action_MF5)
	a5.center = _center
	a5.width = _width
	a5.express = _express

	if a5.express == "LESSLESS" {
		a5.x = a5.center - 2.0*a5.width
	} else if a5.express == "LESS" {
		a5.x = a5.center - a5.width
	} else if a5.express == "COMMON" {
		a5.x = a5.center
	} else if a5.express == "MORE" {
		a5.x = a5.center + a5.width
	} else if a5.express == "MOREMORE" {
		a5.x = a5.center + 2.0*a5.width
	} else {
		fmt.Println("new_action_MF5: wrong scale expression")
		os.Exit(-1)
	}

	a5.y = 0.0

	return a5
}

func new_action_MF3(_center, _width float64, _express string) *action_MF3 {
	a3 := new(action_MF3)
	a3.center = _center
	a3.width = _width
	a3.express = _express

	if a3.express == "LESS" {
		a3.x = a3.center - a3.width
	} else if a3.express == "COMMON" {
		a3.x = a3.center
	} else if a3.express == "MORE" {
		a3.x = a3.center + a3.width
	} else {
		fmt.Println("new_action_MF3: wrong scale expression")
		os.Exit(-1)
	}

	a3.y = 0.0

	return a3
}


// The latter membership function (5 mountains) class
func (a5 *action_MF5) func_Y() float64 {
	return a5.y
}

// The latter membership function (3 mountains) class
func (a3 *action_MF3) func_Y() float64 {
	return a3.y
}

func (a5 *action_MF5) func_Max(b float64) {
	a5.y = max_2(b, a5.y)
}


func (a3 *action_MF3) func_Max(b float64) {
	a3.y = max_2(b, a3.y)
}


func (a5 *action_MF5) func_X() float64 {
	return a5.x
}

func (a3 *action_MF3) func_X() float64 {
	return a3.x
}


func fuzzy_reasoning(temp, humi float64) float64 {

	// Temperature(former)
	Temp_Less := new_condition_MF3(20, 10, "LESS")
	Temp_Common := new_condition_MF3(20, 10, "COMMON")
	Temp_More := new_condition_MF3(20, 10, "MORE")

	// Humidity(former)
	Humi_Less := new_condition_MF3(50, 20, "LESS")
	Humi_Common := new_condition_MF3(50, 20, "COMMON")
	Humi_More := new_condition_MF3(50, 20, "MORE")

	// Switch(前件部)
	Switch_Less := new_action_MF3(0,1,"LESS")
	Switch_Common := new_action_MF3(0,1,"COMMON")
	Switch_More := new_action_MF3(0,1,"MORE")

	// [Rule 01] 
	Rule01 := min_2(Temp_More.func_X(temp), Humi_More.func_X(humi))
	Switch_Less.func_Max(Rule01) // the latters values are overwritten if the value is large enough.
	fmt.Println("Rule01", Rule01)

	// [Rule 02] 
	Rule02 := min_2(Temp_Common.func_X(temp), Humi_More.func_X(humi))
	Switch_Common.func_Max(Rule02) // the latters values are overwritten if the value is large enough.
	fmt.Println("Rule02", Rule02)

	// [Rule 03] 
	Rule03 := min_2(Temp_More.func_X(temp), Humi_Common.func_X(humi))
	Switch_Less.func_Max(Rule03) // the latters values are overwritten if the value is large enough.
	fmt.Println("Rule03", Rule03)

	// [Rule 04] 
	Rule04 := min_2(Temp_Less.func_X(temp), Humi_Less.func_X(humi))
	Switch_More.func_Max(Rule04) // the latters values are overwritten if the value is large enough.
	fmt.Println("Rule04", Rule04)


	// Reasoning calculations
	numerator :=
			Switch_Less.func_X()*Switch_Less.func_Y() +
			Switch_Common.func_X()*Switch_Common.func_Y() +
			Switch_More.func_X()*Switch_More.func_Y() 

	denominator :=
			Switch_Less.func_Y() +
			Switch_Common.func_Y() +
			Switch_More.func_Y() 


	reasoning := numerator / denominator

	return reasoning

}

func main(){

	 fmt.Println(fuzzy_reasoning(27.0, 67.0))
}