# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.5 64-bit (''base'': conda)'
#     name: python385jvsc74a57bd073e03da126b73bfff3642ec5261d56fa25c444ea595de51041687efaa60dda41
# ---

# +
# import sys 
# sys.path.append('~/DL_project/MyProblem/') 
from DTLZ1 import DTLZ1
import geatpy as ea

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torch.utils.data import TensorDataset, DataLoader
from torch.autograd import Variable
import time
import matplotlib.pyplot as plt 
import numpy as np 
import math

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device = "cpu"
seed = 1
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)


# -

class Net(nn.Module):
    def __init__(self, N):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(N, 5000)
        self.fc2 = nn.Linear(5000, 5000)
        self.fc3 = nn.Linear(5000, 3)
#         self.fc4 = nn.Linear(200, 3)
        # self._initialize_weights()

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
#         x = F.relu(self.fc3(x))
        x = self.fc3(x)
        return x



def train(model, data, target, N, epochs, optimizer, criterion):
    model.train()
    train_loss = []
    train_time = []
#     predicts_data = []

    for epoch in range(1, epochs+1):
        print(epoch)
        run_time = time.time()
        optimizer.zero_grad()
        output = model(data)
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()
        run_time = time.time() - run_time
        train_loss.append(loss.item())
        train_time.append(run_time)
        # if epoch % n == 0:
        #     print('epoch:', epoch)
        #     print('Training set: average loss = {:.4f}, total time = {:.4f}'.format(np.mean(train_loss[epoch-n:epoch]), sum(train_time[epoch-n:epoch])))
#     predicts_data.append(output.cpu().detach().numpy())
#     predicts_data = np.hstack(predicts_data)

    fig = plt.figure()
    t = list(range(epochs))
    plt.plot(t, train_loss)
    plt.xlabel('Epoch Number')
    plt.ylabel('Loss Value')
    plt.show()
    
    fig = plt.figure()
    t = list(range(epochs))
    plt.plot(t, train_time)
    plt.xlabel('Epoch Number')
    plt.ylabel('Train Time')
    plt.show()

    return train_loss, train_time #, predicts_data


# +
myProblem = DTLZ1(M=3)  # 实例化问题对象
N = 1000 # 定义样本数

## 以下部分固定不变，是固定写法（该模块用于生成样本）
Encoding = 'RI'
Field = ea.crtfld(Encoding, list(myProblem.varTypes), myProblem.ranges, myProblem.borders)
pop = ea.Population(Encoding, Field, N)
Vars = ea.crtpc(Encoding, pop.sizes, pop.Field)  # 随机生成数值在范围内的随机样本
pop.Phen = Vars
myProblem.aimFunc(pop)  # 计算Objective Value
## 以上部分固定不变，是固定写法

# 至此，pop.ObjV 存储的是目标函数值矩阵，pop.Phen存储的是决策变量矩阵
# print(pop.Phen)
# print(pop.ObjV)

data = torch.as_tensor(pop.Phen, dtype=torch.float32).to(device)
target = torch.as_tensor(pop.ObjV, dtype=torch.float32).to(device)
data_num = data.shape[0]
Dim = data.shape[1]

epochs = 100
model = Net(N=Dim).to(device)
print(model)
criterion = nn.MSELoss() # CrossEntropyLoss() 
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9, weight_decay=0.005)
train_loss, train_time = train(model, data, target, N, epochs, optimizer, criterion)

# -

print(len(train_time))
print(train_time)
sum(train_time)



StarcoderdataJupyterScriptsDedupFiltered