[Plant Simulation Study #20] – Let’s Operate Virtual Factory by ChatGPT Plant Simulation

๋ฐ˜์‘ํ˜•

๐Ÿงช [Plant Simulation Study #20] – Let’s Operate Virtual Factory by ChatGPT Plant Simulation

๐ŸŽฌ Overview

In this study, I demonstrate how to connect Siemens Plant Simulation with OpenAI’s ChatGPT API, enabling an AI to make real-time decisions and control the flow of a virtual factory.

Using Python as a bridge, the simulation continuously sends live data to ChatGPT, receives its response, and dynamically changes simulation variables such as conveyor directions.
This structure shows how AI-powered digital twins can simulate autonomous decision-making systems in a smart factory environment.

 

YouTube : https://youtu.be/mDQOXU9y6mk

 


๐Ÿ’ป Integration Architecture & Key Components

ComponentDescription
Tech Stack Plant Simulation + Python + OpenAI ChatGPT API
Main Goal AI-driven exit strategy logic for production flow
Communication Data sent to ChatGPT every 5 seconds → Response received → Variables applied
Real-Time Loop Python script executes on interval trigger

๐Ÿ”ง Python Core Logic (Simplified)

 
import openai
import pandas as pd

obj = root.EXIT_Seq_SET.Value
set_A = root.Con1_nummu.Value
set_B = root.Con2_nummu.Value

# OpenAI API ํ‚ค ์„ค์ •
api_key = "PUT API HERE"  # ์—ฌ๊ธฐ์— ์‹ค์ œ OpenAI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
openai.api_key = api_key

# ChatGPT API ํ˜ธ์ถœ ํ•จ์ˆ˜
def chat_with_gpt(prompt, model="gpt-4", temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=[
            {"role": "system", "content": "You are a decision-making assistant."},
            {"role": "user", "content": prompt}
        ],
        temperature=temperature
    )
    return response.choices[0].message['content'].strip()

# ํ”„๋กฌํ”„ํŠธ ์„ค์ •
user_input = f"""
Con1 ์œ„ ์ œํ’ˆ ๊ฐœ์ˆ˜ :
{set_A}
Con2 ์œ„ ์ œํ’ˆ ๊ฐœ์ˆ˜ :
{set_B}

0. ์•„๋ž˜ ์กฐ๊ฑด๋“ค์„ ํ™•์ธํ•˜๊ณ  Con1 ๊ณผ Con2 ์ค‘ ํ•œ ๊ณณ์„ ์ถœ๋ ฅํ•ด๋ผ.
1. if {set_A} > {set_B} , Con1 ์ถœ๋ ฅ
2. if {set_A} < {set_B} , Con2 ์ถœ๋ ฅ
2. if {set_A} = {set_B} , Con2 or Con1 50% ํ™•๋ฅ ๋กœ ๋žœ๋ค ์ถœ๋ ฅ
5. ์„ค๋ช… ์—†์ด ์ •ํ™•ํžˆ ๋‘˜์ค‘ ํ•˜๋‚˜์˜ ์ด๋ฆ„๋งŒ ์ ์–ด์ฃผ์„ธ์š”.
"""

# API ํ˜ธ์ถœ ๋ฐ ์‘๋‹ต์„ obj ๋ณ€์ˆ˜์— ์ €์žฅ
try:
    reply = chat_with_gpt(user_input)
    
    print("๐Ÿ”น ChatGPT Answer (์›๋ณธ):\n", reply)

    # ์‘๋‹ต์„ obj ๋ณ€์ˆ˜์— ์ง์ ‘ ์ €์žฅ
    obj = reply  # ๊ธฐ๋Œ€ํ•œ ๋‹จ์ผ ๊ฐ’์ธ A1, A2, A3, A4, A5 ์ค‘ ํ•˜๋‚˜๋ฅผ ์ €์žฅ
    print("๐Ÿ”น done:", obj)

except Exception as e:
    print("API ํ˜ธ์ถœ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:", str(e))

root.EXIT_Seq_SET.value = obj
  • The prompt includes context and current simulation data.
  • The AI’s response is parsed and applied to simulation variables (e.g., Con1, Con2) to alter the model’s flow.

โš™๏ธ Simulation Flow

  1. Python executes every 5 seconds
    (Run Python – 5 Sec Interval


  2. ChatGPT processes the prompt
    • Response appears in the log window
    • Label: “Answered by ChatGPT
  3. Simulation reacts dynamically
    • Conveyor logic and decision paths are changed
    • Line Operated by ChatGPT” appears in the model view


๐Ÿ“ˆ Use Cases & Possibilities

โœ… AI-in-the-loop simulation for smart factory development
โœ… Educational content for AI-powered Digital Twin modeling
โœ… Prototype for decision support systems using LLMs (Large Language Models)
โœ… Expandable to KPI-based optimization, multi-agent decision-making, or RAG-enhanced scenarios


๐Ÿง  Final Thoughts

This demo shows a practical example of autonomous factory control using ChatGPT inside a discrete-event simulation.
No longer is simulation only about modeling logic — it’s about collaborating with AI to make decisions and operate the factory in real time.

๋ฐ˜์‘ํ˜•